Control Structures
Control Structures
Computer Programming
Lecture Note
FTK
1
Chapter 4 Control Structures
No. Title Page
1. if 4
2. if/else & nested if/else 7
3. switch 13
4. while 19
5. do/while 23
6. for 26
7. Continue 30
8. Goto 32
9. Break 34
2
3 control structures:
1. Sequence structure
1. Programs executed sequentially by default
2. Selection structures
a) if (select true statement or proceed next line)
b) if/else ( with/no range, with/no fix result)
i. If/else (more than 2 selection, true or false)
ii. Nested if/else (more than 3 selection, true or false)
c) switch (more than 3 selection, true or false, no range, fix result)
3. Repetition structures
a) while (selection true statement or skip next line)
b) do/while (do statement and then selection next line or do
statement again)
c) for (initialize value and then selection skip nest line or do statement
and increment/decrement)
3
Previous step Previous step
Yes No Yes No
condition condition
Yes No
A < = 10 A < = 10
#include <iostream>
#include <iostream> using namespace std;
using namespace std;
int main ()
int main () {
{ int a;
int a; cout<<"please insert number = ";
cout<<"please insert number = "; cin>>a;
cin>>a;
if (a<=10)
if (a<=10) cout<<"your number below correct"<<endl;
cout<<"your number below correct"<<endl;
else
} cout<<"your number is wrong";
}
5
Previous step Previous step
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
int main ()
int main ()
{
{
char lamp;
int lamp;
cout << "please push on button = ";
cout << "please push on button = ";
cin >> lamp;
cin >> lamp;
cout << "lamp area = ";
cout << "lamp area = ";
switch (lamp)
{
{
if (lamp == 1)
case '1':
cout << " master badroom "<<endl;
cout << " master room "<<endl;break;
else if (lamp == 2)
case '2':
cout << " kitchen "<<endl;
cout << " kitchen "<<endl;break;
else if (lamp == 3)
case '3':
cout << " living room "<<endl;
cout << " living room "<<endl;break;
else if (lamp == 4)
case '4':
cout << " toilet "<<endl;
cout << " toilet "<<endl;break;
else
default:
cout << " outside corridor "<<endl;
cout << " outside corridor "<<endl;break;
}
}
}
} 6
2.a if Selection Structure
True
Statement
Next step
7
Example: Previous step
Yes No
A < = 10
Next step
#include <iostream>
using namespace std;
int main ()
{
int a;
cout<<"please insert number = ";
cin>>a;
if (a<=10)
cout<<"your number below correct"<<endl;
}
8
9
2.b.1 if/else statement
Next step
10
Example: Previous step
Yes No
A < = 10
Next step
Previous step
Next step
13
Example if/else:
Previous step #include <iostream>
using namespace std;
Yes
int main ()
Grade > = 90
{
No
int grade;
A
cout << "please insert your mark = ";
Yes cin >> grade;
Grade > = 80
cout << "grade is = ";
if ( grade >= 90 ) // 90 and above
B No
cout << " A";
Yes else if ( grade >= 80 ) // 80-89
Grade > = 70 cout << " B";
else if ( grade >= 70 ) // 70-79
C No cout << " C";
Yes else if ( grade >= 60 ) // 60-69
Grade > = 60 cout << " D";
else // less than 60
D cout << " F";
F }
Next step
14
15
2.c switch statement
Previous step previous step
Switch (variable)
yes {
Case 1 cade <value 1>:
statement 1;
Break Statement 1 No
break;
Yes
Case 2
cade <value 2>:
Break Statement 2 No statement 2;
break;
Next step
17
18
Using switch Using if/else
Previous step Previous step
yes Yes
Case ‘1’ Grade > = 90
master room No
A No
Yes
Case ‘2’ Yes
Grade > = 80
kitchen No
B No
Yes
Case ‘3’ yes
Grade > = 70
living room No
Yes C No
Case ‘4’
Yes
Grade > = 60
toilet
outside corridor D
Break F
switch (lamp) {
{ if (lamp == 1)
case '1': cout << " master badroom "<<endl;
cout << " master room "<<endl;break; else if (lamp == 2)
case '2': cout << " kitchen "<<endl;
cout << " kitchen "<<endl;break; else if (lamp == 3)
case '3': cout << " living room "<<endl;
cout << " living room "<<endl;break; else if (lamp == 4)
case '4': cout << " toilet "<<endl;
cout << " toilet "<<endl;break; else
default: cout << " outside corridor "<<endl;
cout << " outside corridor "<<endl;break; }
} }
}
20
Using switch Using if/else
Output = same
21
A switch statement is useful when you want to do different things based on the value
of a single variable, most commonly an enum. For example:
Note that in some cases compilers can do a better job of optimizing switch
statements than if-else statements. A compiler could set up a binary search of the
values rather than testing each value one after the other.
But if-else can also be used in more complex situations where the different logical
branches depend on the values of more than one variable:
Note that this is a hypothetical case and doesn’t really make much sense, but in real
programming you run into things like this quite frequently.
3.1 While statement
Repetition structure
• Action repeated while some condition remains true
• While loop repeated until condition becomes false
True statement
Next step
23
Different if & while statement
if while
Previous step Previous step
No
Yes No condition
condition
Yes
previous step
Previous step
if(condition)
while (condition)
true statement;
true statement
next step
Next step
24
while
if Previous step
Previous step No
a < =5
Yes No Yes
a<=5
(++a – 1) loop
(++a – 1) loop
Next step
Next step
#include <iostream>
using namespace std;
#include <iostream>
using namespace std; int main()
{
int main () int a;
{ cin >> a;
int a;
cin>>a; while (a <= 5)
{
if (a<=5) cout<< ++a - 1 <<" loop"<<endl;
cout<< ++a - 1<<" loop"<<endl; }
} } 24
if while
26
3.2 do/While statement
The do/while repetition structure is similar to the while structure,
• Do the statement 1st and then test/check the condition
No
Next step
27
#include <iostream>
Previous step
using namespace std;
int main ()
a = 0;
{
++a
int a = 0;
do
{
Yes
a<5
cout<< ++a <<endl;
}
No while ( a < 5);
cout<< "done" <<endl;
Next step }
28
29
3.3 for statement
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number of
times.
Previous step Previous step
for ( initial value ; condition ; increment/decrement )
Initial value statement
Next step
No
condition
Yes
statement
Increment/
decrement
Next step
30
Using C++
Previous step #include <iostream>
using namespace std;
a=1
int main()
No
a < 10
{
Yes int a;
a LOOP for ( a = 1 ; a < 10 ; ++a )
{
++a cout<< a <<“ LOOP" << endl;
}
}
Next step
OUTPUT
31
#include <iostream> #include <iostream>
#include <iomanip> using namespace std;
using namespace std;
int main()
int main() {
int a = 0;
{ for ( int b = 1 ; b < 10 ; ++b )
int a; {
for ( a = 1 ; a < 10 ; ++a ) a = a + b;
{ cout<< a << endl;
cout<< setw(11-a)<< a << setw(a+a-1)<< a << endl; }
} }
}
#include <iostream>
using namespace std;
int main()
{
int a,x;
cin >> x ;
for ( a = 1 ; a < 10 ; ++a )
{
a = a + x;
cout<< " a = "<< a << endl;
}
}
32
#include <iostream>
using namespace std;
int main()
{
int a,b,x;
cin >> x ;
for ( a=1,b=2 ; a+b<20 ; ++a,b++ )
{
cout<< " a + b = "<<a+b<<" a= "<<a<<" b = "<<b<< endl;
}
}
33
4. Continue
• The continue statement works somewhat like the break
statement. Instead of forcing termination, however,
continue forces the next iteration of the loop to take
place, skipping any code in between.
• For the for loop, continue causes the conditional test and
increment portions of the loop to execute. For the while
and do...while loops, program control passes to the
conditional tests
34
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main () int main ()
{ {
int a=0; int a=0;
for(a=1;a<5;++a) for(a=1;a<5;++a)
{ {
cout<<"a1= "<<a<<endl; cout<<"a1= "<<a<<endl;
cout<<"a2= "<<a<<endl; cout<<"a2= "<<a<<endl;
cout<<"a3= "<<a<<endl; continue;
cout<<"a4= "<<a<<endl; cout<<"a3= "<<a<<endl;
continue; cout<<"a4= "<<a<<endl;
} }
cout<<"a5 = "<<++a<<endl; cout<<"a5 = "<<++a<<endl;
} } #include <iostream>
using namespace std;
int main ()
{
int a=0;
for(a=1;a<5;++a)
{
continue;
cout<<"a1= "<<a<<endl;
cout<<"a2= "<<a<<endl;
cout<<"a3= "<<a<<endl;
cout<<"a4= "<<a<<endl;
}
cout<<"a5 = "<<++a<<endl;
}
35
5. goto
36
#include <iostream>
using namespace std;
int main()
{
int x;
tryAgain:
cout << "Enter number";
cin >> x;
if (x < 10)
goto tryAgain;
cout << "The sum of = " << x << " with 2 = " << x+2 <<endl;
return 0;
}
37
6. break
The break statement has the following two usages in C++:
• When the break statement is encountered inside a loop, the loop is
immediately terminated and program control resumes at the next
statement following the loop.
• It can be used to terminate a case in the switch statement (covered in the
next chapter)
38
Terminate the loop
39
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main () int main ()
{ {
int a = 1; int a = 1;
do do
{ {
cout << "value of a: " << a << endl; cout << "value of a: " << a << endl;
a = a + 1; a = a + 1;
}while( a < 10 );
} if( a > 7)
{
break;
}
}while( a < 10 );
}
40
if
Previous step
Previous step
yes no
condition
yes
True Statement
Case 1
Break Statement 1 No
Yes
Previous step
condition
No
If/else Break Statement 2 No
Yes
Condition 2 switch
true statement 2 No
Yes
Condition 3
true statement 3 No
False statement
Nested
Next step
If/else 41
while
Previous step
No
condition
Yes
True statement
Next step
do/while
Previous step
statement
Yes
condition
No
Next step
for
Previous step
Initial value
No
condition
Yes
statement
Increment/ decrement
Next step 42