c++ basics tutorial 6
Post on 22-Feb-2016
42 Views
Preview:
DESCRIPTION
TRANSCRIPT
C++ BasicsTutorial 6Operators
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
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
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
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);
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
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
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.
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.
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.
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)
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.
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)
Other??
• There are other operators that we will go on later when we get to pointers and specific object oriented programming.
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
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.
top related