Lecture 5 - Algorithms and Control Structure
Lecture 5 - Algorithms and Control Structure
1
The if - else statement
2-way branch of action
if(expression 1)
statement 1;
else
statement 2;
if(expression 2)
{
statement block 1
}
else
{
statement block 2
} 2
Practice : Identify the output of the following programs
3
The nested if-else
//example :
//k will be equal to 1 and
//m will be equal to 0
int x=9, y=7, k=0, m=0;
if(x > y)
k++;
else
m++;
4
The nested if-else
//example :
//k will be equal to 0 and
//m will be equal to 1
int x=5, y=7, k=0, m=0;
if(x > y)
k++;
else
m++;
5
Practice : Identify the output of the following programs
6
The nested if-else
//example :
//k will be equal to 0 and
//m will be equal to 0
//j will be equal to 1
int x=5, y=7, z=2, k=0, m=0, j=0;
if(x > y)
if(y < z)
k++;
else
m++;
else
j++; 7
The if – else if statement
multi-way branch of action
Form of nested if-else
if(expression1) if(expression1)
{ statement;}
{statement 1;} else
{
else if(expression2) if(expression2)
{ statement;}
{statement 2;} else
{
else if(expression3) if(expression3)
{ statement;}
{statement 3;} else
{
…… ……..
}
else }
{statement 4;} }
Each branch may contain a single statement or a block of statements 8
8
encloses within braces:{ statement; statement; statement;}
The if – else if statement (Example)
int day = 3;
if(day==1)
cout<<“Saturday\n”;
else if(day==2)
cout<<“Sunday\n”;
else if(day==3)
cout<<“Monday\n”;
else if(day==4)
cout<<“Tuesday\n”;
else if(day==5)
cout<<“Wednesday\n”;
else if(day==6)
cout<<“Thursday\n”;
else if(day==7)
cout<<“Friday\n”;
else 9
cout<<“Sorry! Invalid day number\n.”; 9
The if – else if statement (Example)
int grade = 81;
if(grade>=90)
cout<<“A”;
else if(grade>=80)
cout<<“B”;
else if(grade>=70)
cout<<“C”;
else if(grade>=60)
cout<<“D”;
else
cout<<“F”;
10
10
The if – else if statement (Example)
int rank = 3;
if (rank==1 || rank==2)
cout << "Lower division \n";
else if (rank==3 || rank==4)
cout << "Upper division \n";
else if (rank==5)
cout << "Graduate student \n";
else
cout << "Invalid rank \n";
11
11
Exercise 1
Continue this C++ program in order to check which is larger (x or y) and
display the larger number
12
Answer
13
Exercise 2
Continue this C++ program in order to check which is smaller (x or y) and
display the smaller number
14
Exercise 3
Continue this C++ program in order to check which is largest (x or y or z)
and display the largest number
15
Exercise 4
Continue this C++ program in order to check which is smallest (x or y or z)
and display the smallest number
16
Solve Practice in Page 126
In problems 1 through 7, draw a flowchart to perform
the steps indicated.
Then give the corresponding C++ statements.
Assume that the objects have been declared and
have reasonable values.
17
Exercise
time = 10;
if (time > 15.0 )
++time;
18
Solve Practice in Page 126
In problems 1 through 7, draw a flowchart to perform
the steps indicated.
Then give the corresponding C++ statements.
Assume that the objects have been declared and
have reasonable values.
1a. If time is greater than 15.0, increment time by 1
20
Solve Practice in Page 126
3. If the difference between volt_1 and volt_2 is
larger than 10.0, print the values of volt_1 and
volt_2
if (abs(volt_1 – volt_2) > 10.0)
cout << volt_1 << ‘ ‘ << volt_2;
24
Exercise
26
Exercise
What is the new value of time after executing
the following statements?