Assignment and logicalarithmetic operators Slides created by salah

Assignment and logical/arithmetic operators Slides created by salah. Image ref: http: //www. bing. com/images/search? q=arithmetic+operators&FORM=HDRSC 2#view=d etail&id=63701 EF 9509 F 028 D 2 F 376 D 8 C 357888 FD 55 BE 8 B 52&selected. Index=4

Assignment operator is = Assignment • Assignment in programming is very important and you will often find yourself carrying out an assignment operation in more or less every program you create. • Assignment is simply assigning a value to a variable. For example we can assign the value 10 to the variable number 1 like this: int number 1 = 5; • Yes, that’s right, its that easy and simple. The int is there because as we learnt previously, each variable needs to hold a data type. • The part at the left of the assignment operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value).

Assignment(=) • We can also assign Arrays and functions to values. For example this line will assign the first of Aarray to number 2. • Aarray[0] = 2; • Now you try and assign location 2 of Aarray to Aarray[1] your answer = 9; here the value 9. Write • Now move the square to see if you are correct.

Logical and arithmetic operators • Lets start of with arithmetic operators. There are five main arithmetic operators that the C++ language supports which are: • - Subtraction • % modulo • + Addition • / Division • * Multiplication

Arithmetic Operators • There is not much to know about these as you would already know about them from maths. The addition adds numbers and subtraction subtracts numbers etc. However you may not be familiar with the percentage sign (modulo) which gives the remainder of a division of two values. An example of this could be: Arithmetic 1 = 11 % 3; • The variable Arithemtic 1 will contain the answer 2 as that is the remainder of diving these two numbers.

Arithmetic Operators • So how do we use these operators in the program. Lets use this example: • area = ((a+b)/2) * h; • Here we are using three of the arithmetic operators in one statement. ‘Area’ is the variable which will hold the answer of this calculation. Other than this, there is not much to it really. So lets move on to logical operators.

Logical Operators. • There are three main logical operators which are: • And, Or, Not. These operators can be written in symbolic form like this. And = &&. Not = ! Or = || • The Not operator has the one job of inversing the value of it. So for example !true would be false and not true is false. • !(100 == 100) is false as the expression on the right of the sign is true. Therefore not true is false.

Logical Operators. • We can use the logical operators, Or and And when you have two expressions and wish to obtain a single result. For example the And operator will show true if and only if both of the expressions are true. The or operator will show true if at least one of its expressions are true. We can then use the final true or false value to for instance, do a certain output in our program.
- Slides: 8