Lecture # 3
Lecture # 3
statements
Lecture # 3
Course Instructor:
Dr. Afshan Jamil
Outline
Boolean The && The ||
Truth tables
expression operator operator
Simple
Comparison Precedence Flow of
branching
operators rules control
mechanism
Common
Nested if
mistake to
else Examples
avoid
Boolean expression
1.if(condition)
2. {
3. //code to be executed
4. }
Example
1. #include <iostream>
2.using namespace std;
3.
4.int main ()
5. {
6. int num = 10;
7. if (num % 2 == 0)
8. {
9. cout<<"It is even number";
10. }
11. return 0;
12.}
If-else statement
1. if(condition)
2. {
3. //code if condition is true
4. }
5. Else
6. {
7. //code if condition is false
8. }
Example
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num = 11;
5. if (num % 2 == 0)
6. {
7. cout<<"It is even number";
8. }
9. else
10. {
11. cout<<"It is odd number";
12. }
13. return 0;
14.}
Multiway if-else
1. if(condition1)
2. {
3. //code to be executed if condition1 is true
4. }else if(condition2)
5. {
6. //code to be executed if condition2 is true
7. }
8. else if(condition3)
9. {
10.//code to be executed if condition3 is true
11.}
12....
13.else{
14.//code to be executed if all the conditions are false
15.}
Example
#include <iostream> else if (num >= 70 && num < 80)
using namespace std; {
int main () { cout<<"B Grade";
int num; }
cout<<"Enter a number to check gra else if (num >= 80 && num < 90)
de:"; {
cin>>num; cout<<"A Grade";
if(num >= 0 && num < 50) }
{ else if (num >= 90 && num <= 100)
cout<<"Fail"; {
} cout<<"A+ Grade";
else if (num >= 50 && num < 60) }
{ else
cout<<"D Grade"; {
} cout<<“wrong number";
else if (num >= 60 && num < 70) }
}
{
cout<<"C Grade";
}
Common mistake to avoid