0% found this document useful (0 votes)
7 views23 pages

Lecture-03-04 (1) .

The document provides an introduction to C++ operators and expressions, detailing various types of operators including arithmetic, relational, logical, conditional, assignment, and increment/decrement operators. It includes examples of each operator type and explains their syntax and usage in programming. Additionally, it briefly covers common errors in C++ programming.

Uploaded by

haiderralii11223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

Lecture-03-04 (1) .

The document provides an introduction to C++ operators and expressions, detailing various types of operators including arithmetic, relational, logical, conditional, assignment, and increment/decrement operators. It includes examples of each operator type and explains their syntax and usage in programming. Additionally, it briefly covers common errors in C++ programming.

Uploaded by

haiderralii11223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to C++

Programming
Lecture# 04-05

Operators &
Expressions
Operators in C++

1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Conditional ternary operator ( ? )
5. Assignment operators
6. Increment and decrement operators
Arithmetic Operators
a=9, b=3
Operation Operator Syntax Result

Addition + a+b 12

Subtraction - a–b 6

Multiply * a*b 27

Divide / a/b 3

Modulus % a%b 0
Example
Relational Operators

Operator Meaning Example


== Is Equal To 3 == 5 gives us false
!= Not Equal To 3 != 5 gives us true
> Greater Than 3 > 5 gives us false
< Less Than 3 < 5 gives us true
>= Greater Than or Equal To 3 >= 5 give us false
<= Less Than or Equal To 3 <= 5 gives us true
Example
Logical Operators

Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Example
Conditional ternary operator
(?)
• condition ? result1 : result2

• If condition is true, the entire expression evaluates to


result1, and otherwise to result2.

7==5 ? 4 : 3 // evaluates to 3, since 7 is not equal to 5.


7==5+2 ? 4 : 3 // evaluates to 4, since 7 is equal to 5+2.
5>3 ? a : b // evaluates to the value of a, since 5 is
greater than 3.
a>b ? a : b // evaluates to whichever is greater, a or b.
Example
// conditional operator
#include <iostream> Output?
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c ;
}
Assignment Operator

 An assignment statement changes the value of a variable


 The assignment operator is the = sign

int count, num;


char ltr;
Memory
count = 0; count: 0
num = 55; num: 55
ltr = ‘A’; ltr: A

 The value on the right is stored in the variable on the left


 Any value that was in the variable is overwritten

11
Compound Assignment
Operators
• Compound assignment operators are operators which
provide a shortcut when adding, subtracting, multiplying,
or dividing a number to/by itself.
• Operators: += -= *= /= %=

A op= B is the same as A = A op B


A += B A=A+B
A -= B A=A-B
A *= B A=A*B
A /= B A=A/B
A %= B A=A%B

12
Increment/Decrement Operators
• The increment operator (++) adds one to the value of the variable. The decrement operator (--)
subtracts one from the value of the variable.
x++;
++x; // both increase x by 1

x--;
--x; // both decrease x by 1

• When used alone with one variable, the following three statements are equivalent:
x++;
++x;
x = x + 1;

13
Increment/Decrement Operators
• When used within an expression, however, the
placement matters.

• ++ (pre-increment operator) when the ++ is


placed on the left-hand side of a variable (++x).

Variable is incremented BEFORE the rest of


expression is evaluated:

A = ++B * Num; is equivalent to: B = B + 1;


A
= B * Num;

14
Increment/Decrement Operators

-- (pre-decrement operator)
When the -- is placed on the left-hand side of a
variable (--x).

Variable is decremented BEFORE the rest of


expression is evaluated:

A = -- B * Num; is equivalent to: B = B - 1;

A = B * Num;

15
Increment/Decrement Operators

• ++ (post-increment operator) when the ++ is placed on the right-hand


side of a variable (x++).

Variable is incremented AFTER the rest of expression is evaluated:

A = B++ * Num; is equivalent to A = B * Num;


B = B + 1;

16
Increment/Decrement Operators

• -- (post-decrement operator) when the -- is placed on the right-


hand side of a variable (x--).

Variable is decremented AFTER the rest of expression is evaluated:

A = B-- * Num; is equivalent to A = B * Num;


B = B - 1;

17
Examples

•Suppose that a=2, b=3 and c=6, then:

•(a == 5)
•(a*b >= c)
•(b+4 > a*c)
•((b=2) == a)
Example 01
#include <iostream>
using namespace std;

int main() {
Output?
int x = 5;
int y = 3;
cout << (x > 3 && x < 10);
}
Example 02
using namespace std;

int main() {
int x = 5;
Output?
int y = 3;
cout << (x > 3 || x < 4);
return 0;
}
Example 03
#include <iostream>
using namespace std;

int main() {
Output?
int x = 5;
int y = 3;
cout << (!(x > 3 && x < 10));
return 0;
}
Q1. Write a C++ program to
generate the results as shown
below:
Results: =======Quizzes===============
Enter the score of the first quiz: 90
Enter the score of the second quiz: 75
Enter the score of the third quiz: 91
=======Mid-term==============
Enter the score of the mid-term: 80
=======Final=================
Enter the score of the final: 89
Quiz Total: 256
Mid-term : 80
Final : 89
……………………
Total: 425
Reading Assignment

Errors in C++
1. syntax Errors
2. Run-time Error
3. Linker Errors
4. Logical Errors
5. Semantic errors

You might also like