Control Structures
Improve accuracy and results in error resistant and
maintainable code.
They also control the execution of a program.
There are 3 basic control structures
Sequence Structure (straight)
Selection structure (branching){If, if…else, switch
statements }
Loop structure (iteration and repetition){For, while, do-
while statements}
• jump (break, goto, return, continue) statements
Monday, June 9, 2025 By Mr C . ZANO
If..statement
true
condition? statement
false
Monday, June 9, 2025 By Mr C . ZANO
Selection or Conditional Structures
Improve accuracy and results in error resistant and
maintainable code.
They also control the execution of a program.
There are 3 basic control structures
Sequence Structure (straight)
Selection structure (branching){If, if…else, switch
statements }
Loop structure (iteration and repetition){For, while, do-
while statements}
• jump (break, goto, return, continue) statements
Monday, June 9, 2025 By Mr C . ZANO
If …else statement
true false
condition?
statement-1 statement-2
Monday, June 9, 2025 By Mr C . ZANO
Switch …case statement
switch
Test variable for multiple values
Series of case labels and optional default case
switch ( variable ) {
case value1: // taken if variable == value1
statements
break; // necessary to exit switch
case value2:
case value3: // taken if variable == value2
or == value3
statements
break;
default: // taken if variable matches no
other cases
statements
break;
}
Monday, June 9, 2025 By Mr C . ZANO
Switch …case statement
true
case a case a action(s) break
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)
Monday, June 9, 2025 By Mr C . ZANO
Loop Structures
These can be implemented using a for statement, while
and do …..while loop.
true
condition? statement
false
Monday, June 9, 2025 By Mr C . ZANO
The for …statement
This is an entry controlled loop and is used
when one action is to be repeated for a
predetermined number of times.
Syntax
for (initial value; test condition; increment)
{
Action 1;
}
Action 2;
Monday, June 9, 2025 By Mr C . ZANO
The while …statement
This is also an entry controlled loop structure used
when one action is to be repeated for a predetermined
number of times.
Syntax
while (condition )
{
Action 1;
}
Action 2;
– Action1 will be executed as long as the condition is true, the
control moves to the execution of action2.
Monday, June 9, 2025 By Mr C . ZANO
The do..while …statement
This is an exit controlled loop structure used based on condition.
Control is transferred back to a particular point in the program.
Syntax
do
{
statement
Action1;
Action2;
}
while (condition ); true
condition
Action 3;
false
The statement is executed at least once.
Monday, June 9, 2025 By Mr C . ZANO
The break and continue Statements
• Break
– Causes immediate exit from a while, for,
do/while or switch structure
– Program execution continues with the first
statement after the structure
– Common uses of the break statement:
• Escape early from a loop
• Skip the remainder of a switch structure
Monday, June 9, 2025 By Mr C . ZANO
The break and continue Statements
• Continue
– Skips the remaining statements in the body of a while,
for or do/while structure and proceeds with the next
iteration of the loop
– In while and do/while, the loop-continuation test is
evaluated immediately after the continue statement is
executed
– In the for structure, the increment expression is
executed, then the loop-continuation test is evaluated
Monday, June 9, 2025 By Mr C . ZANO
The break and continue Statements
• Causes an immediate jump to the loop test
int next = 0;
while (true){
scanf(“%d”,&next;
if (next < 0)
break;
if (next % 2) //odd number, don’t print
continue;
printf(next,”\n”);
}
printf(“negative num so here we are! \n” );
Monday, June 9, 2025 By Mr C . ZANO