0% found this document useful (0 votes)
13 views

Control Structures

The document provides an overview of control structures in computer programming, including sequence, selection, and repetition structures. It defines common control structures like if/else statements, switch statements, while loops, for loops, and more. Code examples are provided to illustrate how each structure works.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Control Structures

The document provides an overview of control structures in computer programming, including sequence, selection, and repetition structures. It defines common control structures like if/else statements, switch statements, while loops, for loops, and more. Code examples are provided to illustrate how each structure works.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

BNJ 20802

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

True True False


Statement Statement Statement

Next step Next step

previous step previous step


if(condition) if(condition)
true statement; true statement;
next step else
false statement;
next step
Previous step
Previous step previous step
Yes Switch (variable)
Condition 1 yes {
Case 1
true statement 1 cade <value 1>:
No Break Statement 1 No statement1;
Yes Condition 2 Yes Case 2 break;
true statement 2 No Break Statement 2 No cade <value 2>:
Yes Condition 3 statement2;
break;
true statement 3 No
No Yes Case n cade <valuen>:
False statement
Break Statement n statement n;
Default statement break;
Next step Break
default:
default statement;
previous step Next step break;
if (condition 1) }
true statement1; nextstep
else if(condition 2)
true statement 2;
else if (condition 3)
true statement3;
else
false statement;
next step 4
Previous step Previous step

Yes No
A < = 10 A < = 10

your numberbelow your number your numberis


correct below correct wrong

Next step Next step

#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

yes Case ‘1’ Yes Lamp = = 1


master room No master room No
Yes Case ‘2’ Yes
Lamp = = 2
kitchen No Kitchen No
Yes Case ‘3’ Yes
Lamp = = 3
living room No Living room No
Yes Case ‘4’ Yes
Lamp = = 4
toilet
outside corridor Toilet No
Break Outside coorridor

Next step Next 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

Previous step previous step


if(condition)
Yes No true statement;
condition next step

True
Statement

Next step

7
Example: Previous step

Yes No
A < = 10

your number below


correct

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

Previous step previous step


if(condition)
true statement;
Yes No
condition else
false statement;
next step
True False
Statement Statement

Next step

• Required for 2 selection of solution.

10
Example: Previous step

Yes No
A < = 10

your number below your number is


correct wrong

Next step

#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) cout<<(a<=10 ? "correct number" : "wrong number");
cout<<"your number below correct"<<endl; }
else
cout<<"your number is wrong";
} 11
12
2.b.2 Nested if / else statement

Previous step

Yes previous step


Condition 1 if (condition 1)
No
true statement 1;
true statement 1
else if(condition 2)
Yes
Condition 2 true statement 2;
else if (condition 3)
true statement 2 No true statement 3;
yes
Condition 3
else
false statement;
true statement 3 No next step

False statement • Used for the selection that are


more than 2 selection of choices

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;

cade <value n>:


No
Yes
statement n;
Case n break;

Break Statement n default:


Default statement default statement ;
Break break;
}
next step
Next step
16
Example switch :
Previous step #include <iostream>
using namespace std;
int main ()
yes {
Case ‘1’
char lamp;
cout << "please push on button = ";
master room No
cin >> lamp;
Yes cout << "lamp area = ";
Case ‘2’
switch (lamp)
kitchen No {
Yes case '1':
Case ‘3’ cout << " master room "<<endl;break;
case '2':
living room cout << " kitchen "<<endl;break;
No
case '3':
Yes
cout << " living room "<<endl;break;
Case ‘4’
case '4':
cout << " toilet "<<endl;break;
toilet
default:
outside corridor cout << " outside corridor "<<endl;break;
}
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

Next step Next step


19
Using switch Using if/else
#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; }
} }
}
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:

enum color { red, green, blue };


void handleColor( color c ) {
switch(c){
case red: doRed(); break;
case green: doGreen(); break;
case blue: doBlue(); break;
}

if-else statements can do something similar:


enum color { red, green, blue };
void handleColor( color c ) {
if ( c == red ) doRed();
else if ( c == green ) doGreen();
else if ( c == blue ) doBlue();
}

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:

enum color { red, green, blue };


void handleTwoPixels( color a, color b ) {
if ( a == red ) doRed();
else if ( b == green ) doGreen();
else if ( a == blue and b == blue ) doBlue();
}

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

Previous step Previous step


while (condition)
No true statement
condition
Next step
Yes

True statement

Next step

23
Different if & while statement

if while
Previous step Previous step

No
Yes No condition
condition
Yes

True statement True statement

Next step Next step

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

Enter no “-5” Enter no “2” Enter no “-5” Enter no “2”

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

Previous step Previous step


do
{
statement
statement;
} while ( condition );
Yes Next step
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

A goto statement provides an unconditional jump from the


goto to a labeled statement in the same function.

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

Next step Yes


Case 2

Yes
Previous step

condition
No
If/else Break Statement 2 No

True Statement False Statement No


Yes
Case n
Next step
Break Statement n
Default statement
Previous step
Break
Yes
Condition 1
Next step
true statement 1 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

You might also like