1 / 16

C++ Basics Tutorial 6

C++ Basics Tutorial 6. Operators. Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..) Increment and decrement(++, --) Relational and equality(==, !=, >, <. >=, <=) Logical(!, &&, ||) Conditional(?:) Comma operator(,)

wayne
Download Presentation

C++ Basics Tutorial 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ BasicsTutorial 6 Operators

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

  3. lvalue • 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 rvalue Assignment operator(=)

  4. + 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 Arithmatic operators (+, -, /, *, %)

  5. 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); Compound Assignment(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

  6. ++: 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 Increment and Decrement (++, --)

  7. == 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 Relational and equality operators (==, !=, >, <, >=, <=)

  8. && 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. Logical operators(&&, ||, ! )

  9. 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. Conditional operator( ? : )

  10. 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. Comma Operator (,)

  11. 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) Bitwise operators(&, |, ^, ~, <<, >>)

  12. 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. Explicit type casting operators

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

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

  15. 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 Precedence

  16. 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. Please rate, comment and subscribe

More Related