Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)

Similar presentations


Presentation on theme: "C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)"— Presentation transcript:

1 C++ Basics Tutorial 6 Operators

2 What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..) Increment and decrement(++, --) Relational and equality(==, !=, >, =, <=) Logical(!, &&, ||) Conditional(?:) Comma operator(,) Bitwise operator(&, |, ^, ~, >) sizeof() Precedence

3 Assignment operator(=) Assign a value to variable Example: x=23; Right to left rule. Assigns always the right part to left. C++ allows to use ‘=‘ as rvalue too Example x=23+(y=53); is same as y=53; x = 23+y; Multiple assignment operator is also valid a=b=c= 23 assigns 23 to all a, b and c lvalue rvalue

4 Arithmatic operators (+, -, /, *, %) + is addition, - is subtraction, * multiplication, / is division and % is modulo. When % is used between two numbers it will give the remainder of the division between two numbers. Example: 15%4 = 3

5 Compound Assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) Changing the value of a variable, using the current value stored in the same variable, compound assignment become useful Example: a+=5 is same as a = a+5 (add 5 more to whatever is in a and replace the old value of a to a+5). Similar to a-=5; or a/=5, a*=b%2 is same as a = a*(b%2);

6 Increment and Decrement (++, --) ++: Increment the value in variable by one unit Same as +=1; Example: a++, a+=1; both increase the value of a by one. --: Decrement the value stored in variable by one unit Same as -=1; Example a--, a-=1; both decrease the value of a by one. Can be used and prefix or suffix Example a++ or ++a In case result of this operation and evaluated and stored, writing a++ or ++a makes a big difference. Example: a=5; b=a++; gives us a=6 and b=5 But a = 5; b=++a; gives a=6 and b=6

7 Relational and equality operators (==, !=, >, =, <=) == Equal to? (not same as ‘=‘) != Not equal to? > greater than? < smaller than? >= greater than or equal to? <= smaller than or equal to? Example: (23==53) is false (23>=23) is true Or using variable: (a>b) returns true if a is bigger than b (a!=b) returns true if a is not equal to b

8 Logical operators (&&, ||, ! ) && and || are used to obtain a relational result between two expressions. Example: If we have two expression a and b, then we can use as (a && b) or (a||b). Using && will give result as true only of both expression are true and false otherwise whereas || will give false only if both expression are false and true otherwise. && corresponds to boolean logic AND || corresponds to boolean logic OR ! Corresponds to boolean logic NOT Is located on the left of operand. Inverses the value of operand Example (!(23==23)) is returned as false.

9 Conditional operator( ? : ) It evaluates an expression and returns a result. Condition ? (Return If True) : (Return If False) Example: (7>5)?10:20  Will return 20 because 7 is greater than 5. (7<5)?10:20 will return 20 because 7 < 5 is a false statement. (!(5==5))?1:2 will return 2 because 5 is equal to 5 but ! will change that true to false statement hence 2 is returned.

10 Comma Operator (,) Used when two or more expressions is included where only one is expected Example: x = (y=27, y+23); will first assign 27 to y and store 50 (y+23=27+23=50) to x.

11 Bitwise operators (&, |, ^, ~, >) Used only when working with bits of data on low level. Currently we are only working on bytes of data. & is bitwise AND | is bitwise OR ^ is bitwise XOR ~ is bitwise NOT << shift left (SHL) >> shift right (SHR)

12 Explicit type casting operators Type casting: converting a data type from one to the other. Example: int a; float b = 6.67; a = (int)b (or, a = int(b);) will have a = 6.

13 sizeof() Return size in bytes for a data type or variable itself. Example SizeOfChar = sizeof(char) will store 1 to variable SizeOfChar because the size of char data type is 1 bytes (we talked about it in previous videos)

14 Other?? There are other operators that we will go on later when we get to pointers and specific object oriented programming.

15 Precedence Order in which the operators are performed according to the priority. Priority order is defined for each operators. For basic leve: Level 1 is :: (called scope) Level 2: () or [] Level 3: ++, -- Level 4: Type casting (type) Level 5: * / % (Goes from left to right) Level 6: +, - (Left to right) Level 7:, = relational (left to right) Level 8: ==, != Level 9: bitwise operators Level 10: && Logical AND (left to right) Level 11: || Logical OR (left to right) Level 12: ?: conditional (right to left) Level 13: =, /= … Assignment operators (Right to left) Level 14: comma(,) left to right

16 Please rate, comment and subscribe Next Video: Basic input and output Ends the basics Then we will program, learn and implement all we learned in basics Please visit www.LearnFromSuzzett.com for more materials to learn  Quizzes, challenges, articles and news.www.LearnFromSuzzett.com


Download ppt "C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)"

Similar presentations


Ads by Google