Chapter-2 - Control Structure
Chapter-2 - Control Structure
Habtamu W.
Outlines
Control Structures in C++
Conditional Structures
Repetition Structures
Jump statements
Control Structures: Flow of Control
A statement is a part of your program that can be executed.
In C++, the flow of control refers to the sequence in which statements in a program
are executed.
It determines the order in which the statements are executed based on conditions and
loops. C++ program is written in three basic structures. void my_function() {
int a ;
1. Sequential structure a=1 ;
int b;
}
2. Conditional structure (Selection)
Conditional structures provide the flexibility to control the flow of your program
based on specific conditions.
Entry Controlled loops: In this type of loop, the test condition is tested before
entering the loop body. E.g. for and While Loops.
Exit Controlled Loops: In this type of loop the test condition is tested or evaluated at
the end of the loop body. Therefore, the loop body will execute at least once,
irrespective of whether the test condition is true or false. the do-while loop is exit
controlled loop.
Repetition structure (Loop or Iteration)
for loop- is a repetition control structure that allows us to write a loop that is executed
a specific number of times.
Syntax
}
Repetition structure (Loop or Iteration)
Explanation of the Syntax:
• Variable defined before the loop with the same name are hidden during execution of
the loop.
• Condition: This statement gets evaluated ahead of each execution of the loop
body, and abort the execution if the given condition get false.
Repetition structure (Loop or
Iteration)
• Iteration execution: This statement gets
executed after the loop body, ahead of the next
condition evaluated, unless the for loop is aborted
in the body.
Repetition structure (Loop or Iteration)
Repetition structure (Loop or Iteration)
Repetition structure (Loop or Iteration)
while loop- is a repetition control structure that is used when the number of iterations
is not known in advance, and the loop continues as long as a certain condition is true.
Syntax
while (test_expression)
{
// statements
update_expression;
}
Repetition structure (Loop or Iteration)
Explanation of the Syntax:
• Test Expression: tests the condition if it is true. If the condition evaluates to true
then execute the body of the loop and go to update expression. Otherwise, exit from
the while loop.
• Unlike the while loop, the do-while loop guarantees that the block of code is executed
at least once before checking the loop condition.
Syntax
do
{
// loop body
update_expression;
}
while (test_expression);
Repetition structure (Loop or Iteration)
Explanation of the Syntax:
• Test Expression: tests the condition if it is true. If the condition evaluates to true
then it execute the body of the loop and go to the update expression. Otherwise, it
will exit from the while loop.
They are utilized to conclude or extend the execution of a loop within a program or to
halt the execution of a function.
continue
break
goto
return
Jump Statements
continue- The continue statement in C++ is employed to execute the remaining parts
of a loop while bypassing specific sections declared within the condition.
Instead of halting the loop, it proceeds to the next iteration of the same loop.
Unlike the continue statement after the condition is met, it breaks the loop and the
remaining part of the loop is not executed.
The break statement is used with decision-making statements such as if, if-else,
or switch statement which is inside the loop which can be for loop, while
loop, or do-while loop.
Jump Statements
Jump Statements
goto- The goto statement in C++ is employed to directly jump to a designated section
of the program.
Each goto statement is linked with a label, guiding it to the specific part of the program
where it is called.
Its purpose is to conclude the entire function's execution either after the function has
completed its tasks or when a specific condition is met.
Each function typically includes a return statement with a value to be returned, except
for void functions.
Even void functions may incorporate a return statement to prematurely conclude the
function's execution.
Jump Statements