Lecture-03-04 (1) .
Lecture-03-04 (1) .
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
&& Logical AND
|| Logical OR
! Logical NOT
Example
Conditional ternary operator
(?)
• condition ? result1 : result2
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: += -= *= /= %=
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.
14
Increment/Decrement Operators
-- (pre-decrement operator)
When the -- is placed on the left-hand side of a
variable (--x).
A = B * Num;
15
Increment/Decrement Operators
16
Increment/Decrement Operators
17
Examples
•(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