Operators & Decision Statements
Operators & Decision Statements
Operators & Decision Statements
x + y + 17 = 0;
Part of an expression
a = 17;
b = 17 + a;
c = 5 + 2 * numberOfFriends - 4 / 10;
Expression I/O
cin >> a;
cin >> b;
Expression I/O
Expressions can:
1. Shorten the length of a program
2. Reduce the need for some variables
3. Sometimes make it easier to follow the logic
Operators
1. Unary Operators
2. Binary Operators
3. Ternary Operators
Unary Operators
cout<<x++<<endl;//Output -> 5
cout<<x<<endl; //Output -> 6 Beware of its behaviour
Pre-increment operator
1. Arithmetic
2. Relational
3. Logical
4. Bitwise
5. Assignment
Arithmetic Operator
1. Parenthesis.
2. Exponents.
3. Multiplication or Division. [Whichever comes first]
4. Followed by Addition or Subtraction. [Whichever comes
first]
So, 7/5 = 1
If you want a decimal number then you need to convert one of the operands to
floating-point e.g : 7.0/5 = 1.4
Relational Operator
}
Logical Operators Precedence
!
&&
||
Shorthand Operators
Combine addition and assignment : +=
Combine multiplication and assignment : *=
Combine other arithmetic operators and assignment : -=, /=, %= …
E.g:
x = x + 17; is equivalent to x += 17;
x = x - 17; is equivalent to x -= 17;