Lecture 3
Operators & Decision Statements
Expressions
C++ allows us to create complex mathematical
expressions by combining variables and literals with
operators
An expression is a statement that has a value
An expression never appears on the left hand side of
an assignment statement
x + y + 17 = 0;
Part of an expression
An expression can be a combination of the following
elements
Literal : 17, 2.54, ‘C’, “hello”
Variable : x, numberOfFriends
Operators : +, -, <, >
a = 17;
b = 17 + a;
c = 5 + 2 * numberOfFriends - 4 / 10;
Expression I/O
cout will output the value of an expression:
cout << 17 * 3 - 9 << endl;
It displays 42
We can also do chained output of expressions:
cout << "Its " << 9-1 << “pm”;
It displays Its 8 pm
Expression I/O
Extractions on cin can also be chained to request more than
one datum in a single statement.
cin >> a >> b;
This is equivalent to:
cin >> a;
cin >> b;
Expression I/O
We can not use expression in the same way with cin.
cin >> 17;
cin >> x + 17;
What is the problem?
Advantage of using expression
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
An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations. We can
divide operator in C++ into 3 main categories-
1. Unary Operators
2. Binary Operators
3. Ternary Operators
Unary Operators
1. Operators that operates or works with a single operand
are unary operators.
2. For example: (++ ,--)
Post-increment operator
Addition of 1 to a variable is done so frequently in programs that we have a special
operator for that purpose
x = x + 1; is equivalent to x++;
x = x - 1; is equivalent to x--;
int x = 5;
cout<<x<<endl; //Output -> 5
cout<<x++<<endl;//Output -> 5
cout<<x<<endl; //Output -> 6 Beware of its behaviour
Pre-increment operator
Addition of 1 to a variable is done so frequently in programs that we have a special
operator for that purpose
x = x + 1; is equivalent to ++x;
x = x - 1; is equivalent to --x;
int x = 5;
cout<<x<<endl; //Output -> 5
Beware of its behaviour
cout<<++x<<endl;//Output -> 6
cout<<x<<endl; //Output -> 6
Binary Operator
Operators that operates or works with two operands are
binary operators.
1. Arithmetic
2. Relational
3. Logical
4. Bitwise
5. Assignment
Arithmetic Operator
Operators used to perform mathematical operations.
1. For addition ‘+’
2. For multiplication ‘*’
3. For subtraction ‘-’
4. For division ‘/’
5. For modulus ‘%’
PEMDAS Order
Please Excuse My Dear Aunt Sally
1. Parenthesis.
2. Exponents.
3. Multiplication or Division. [Whichever comes first]
4. Followed by Addition or Subtraction. [Whichever comes
first]
So let us look at some math: (7+(3*15-8)*2/6-4)/9
Division Operator
Results of an algebraic or arithmetic operation depends on the data type of the operands
This becomes problematic for division operations in particular
int / int -> int
double / double -> double
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
Relational operators are used for comparison of the values
of two operands.
Logical Operator
Logical Operators are used to combine two or more
conditions/constraints or to complement the evaluation of
the original condition in consideration. The result of the
operation of a logical operator is a boolean value either
true or false.
Logical Operators
&& AND New relational expression is true if both
expressions are true.
Binary operator
|| OR New relational expression is true if either
expression is true.
Binary operator
! NOT Reverses the value of an expression – true
expression becomes false, and false becomes
true.
Unary operator
&&
#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"Enter the x value: ";
cin >> x;
cout<<"Enter the y value: ";
cin>> y;
if (x ==5 && y == 7)
{cout<<"Today We have first class of csc 101";}
else {cout << "No class"<<endl;}
return 0;
}
&&
#include<iostream>
using namespace std;
int main()
{
string day; string hour;
cout<<"Day";
cin >> day;
cout<<"Hour";
cin>> hour;
if (day =="monday" && hour == "1.40pm")
{cout<<"Today there is a CSC class";}
else {cout << "Don’t know"<<endl;}
return 0;
}
Logical Operators Precedence
! has highest precedence, followed by &&, then ||
!
&&
||
Always use parentheses if you are unsure about precedence
Assignment Operator
Assignment operators are used to assign value to a variable.
The left side operand of the assignment operator is a
variable and right side operand of the assignment operator
is a value.
A few more operators
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;