You are on page 1of 47

CS-114

Fundamentals of Computer Programming

Lecture 6
Operators in C++

Course Instructor:
Aisha Shabbir NUST
Institute of Geographical Institute Systems
Information of Civil
Engineering (NICE)
Road Map for Today
• RECAP
• Operators
o Stream Operators
o Decision Making – Equality and Relational
Operators
o Assignment/ Combined Operators
o Unary Operators
o Logical Operators
o Conditional operator
Operators in C++

Institute of Geographical Information


Systems
Arithmetic Operators

Institute of Geographical Information


Systems
Arithmetic Operators

Binary
operators

Institute of Geographical Information


Systems
Decision Making –
Equality & Relational Operators

Institute of Geographical Information


Systems
Equality and Relational Operators
• Used to define the conditions for making
some sort of decision

 2003 Prentice Hall, Inc. All rights reserved.


Equality and Relational Operators

 2003 Prentice Hall, Inc. All rights reserved.


Assignment/ Combined
Operators
(=, +=, -=, *=, /=, %=)

Institute of Geographical Information


Systems
Assignment/Combined Operators

variable = variable operator expression;

variable operator= expression;

Institute of Geographical Information


Systems
Arithmetic Assignment Operators

?
Unary Operators

Institute of Geographical Information


Systems
Unary Operators

C++ provides two types of unary operators


• unary increment operator (++)
• for adding 1 to the value of a numeric
variable
• unary decrement operator (--)
• subtracting 1 from the value of a numeric
variable
Unary Operators

When is the unary operation carried out


depends on where (before or after the
variable) the operator has been placed.

• Prefix Unary Operator


• Postfix Unary Operator
A PREFIX is added
before a word to
make a new word.

Institute of Geographical Information


Systems
Prefix Unary Operators

An increment or decrement operator that’s


placed before (prefixed to) a variable is
referred to as the prefix increment or prefix
decrement operator, respectively.
++variable - prefix increment
--variable - prefix decrement
A SUFFIX/POSTFIX is
added to the end
of a word
to make a new word.
Institute of Geographical Information
Systems
Postfix Unary Operators

An increment or decrement operator that’s


placed after (postfixed to) a variable is
referred to as the postfix increment or
postfix decrement operator, respectively.
variable++ - postfix increment
variable-- - postfix decrement
Unary Operators

Institute of Geographical Information


Systems
Preincrement - Example
#include <iostream>
using namespace std;
Output
int main() 2
{ 3
int var = 1; 4
5
cout << ++var << endl; 6
cout << ++var << endl;

cout << ++var << endl;


cout << ++var << endl;
cout << ++var << endl;

return 0;
}
Institute of Geographical Information
Systems
Postincrement - Example
#include <iostream>
using namespace std;
Output
int main() 1
{ 2
int var = 1; 3
4
cout << var++ << endl; 5
cout << var++ << endl;
cout << var++ << endl;
cout << var++ << endl;
cout << var++ << endl;

return 0;
}

Institute of Geographical Information


Systems
Predecrement- Example
#include <iostream>
using namespace std;
Output
int main() 3
{ 2
int var = 4; 1
0
cout << --var << endl;
cout << --var << endl;
cout << --var << endl;
cout << --var << endl;

return 0;
}

Institute of Geographical Information


Systems
Postdecrement- Example
#include <iostream>
using namespace std;
Output
int main() 4

?
{ 3
int var = 4; 2
1
cout << var-- << endl;
cout << var-- << endl;
cout << var-- << endl;
cout << var-- << endl;

return 0;
}

Institute of Geographical Information


Systems
Exercise
#include <iostream>
using namespace std;
Output
int main() 23

?
{ 22
int a = 21; 22
int c ; 24
c = ++a;
cout << ++c << "\n";
cout << a<< "\n";
cout << a++<< "\n";
Cout<<++a<< "\n";

return 0;
}

Institute of Geographical Information


Systems
Exercise
#include <iostream>
using namespace std;
Output
int main() 64

?
{
int x = 5, y = 5;
cout << ++x << --y << endl;

return 0;
}

Institute of Geographical Information


Systems
Common Mistakes – No intervening spaces
The unary increment and decrement operators
should be placed next to their operands, with no
intervening spaces.

+ + variable variable --

Incorrect Incorrect
Common Mistakes – Syntax Error
Attempting to use the increment or decrement
operator on an expression other than a variable
name is a syntax error.

++(x+2)

Incorrect
Caution
Be VERY CAREFUL using these operators.
C++ allows these operators to be used in the middle
of a larger expression.

Suppose num contains the value 3, the statement


someNum = num++ * 4;
yields a different result than the statement
someNum = ++num * 4;

What do you think the two results are?


Logical Operators
(!, &&, ||)
Logical Operators
So far we’ve studied only simple conditions,
such as:
counter <= 10
total > 1000
number != someValue

We expressed these conditions in terms of


the relational operators.
Logical Operators
C++ provides logical operators that are used
to form more complex conditions by
combining simple conditions.

The logical operators are:


• && (logical AND)
• || (logical OR)
• ! (logical NOT, also called logical negation)
&& – Logical AND
Creates a Compound/Composite Condition
(condition1 && condition2)
&& – Logical AND
(age >= 21) && (age <= 60)

Truth table
&& – Logical AND – Example

…the age is between 1 and 70 (inclusive)…

In case of a nested IF condition


IF age >= 1 then
IF age <= 70 then
perform operations
ENDIF
ENDIF
&& – Logical AND – Example
|| – Logical OR – Example
Executes if one or both conditions are true
(condition1 || condition2)

Truth table
|| – Logical OR – Example
…if age is 15, 31, or 45 then give 10% discount…

In case of an IF condition
IF age == 15 then
give discount
ENDIF
IF age == 31 then
give discount
ENDIF
|| – Logical OR – Example
…if age is 15, 31, or 45 then give 10% discount…

In case of an IF condition
IF age == 45 then
give discount
ENDIF
|| – Logical OR – Example
…if age is 15, 31, or 45 then give 10% discount…
! – Logical NOT (logical negation)
Used to reverse a condition’s meaning
!(condition)

Truth table
! – Logical NOT – Example
…if age is NOT more than 30 then give 5% discount…

if (!(age > 30))


give discount

…if day is not Saturday or Sunday, trigger alarm…


string day;
day="saturday";
if (!(day == "Saturday" || day == "Sunday"))
trigger alarm
conditional Operator
(?:)
Conditional Operator (?:)
C++ also provides an operator to deal with if…
else scenarios.

This is called Conditional Operator (?:)


• It takes three arguments
– Ternary Operator
Conditional Operator (?:)
The syntax of using conditional operator is:

(condition)?expression1:expression2;

Condition is Result is Result is


evaluated expression1 if expression2 if
condition is condition is
true false
Conditional Operator (?:)
#include <iostream>
using namespace std;

tp u t
Ou
int main ()

value of x: 40
{

?
// variable declaration
int x, y = 10;

x = (y < 10) ? 30 : 40;


cout << "value of x: " << x << endl;

return 0;
}
y = ax + bx + c 2
1. [5 marks] Convert the above given algebraic
expression to C++ expression and also number
the precedence order in which the operators will
be evaluated.

Institute of Geographical Information


Systems
Institute of Geographical Information Systems

You might also like