You are on page 1of 33

C++ Control Structures

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)

3. Repetition structure (Loop or Iteration)


Conditional (Selection) Structure:-
 In C++ these structures are used to make decisions in a program based on whether
certain conditions are true or false.

 They are also known as Decision-Making Statements.

 Conditional structures provide the flexibility to control the flow of your program
based on specific conditions.

 The primary conditional structures in C++ are:-


 if Statement
 if-else Statement
 if-else if-else Statement
 switch Statement
Conditional (Selection) Structure:-
 if Statement:- The ‘if’ statement is used to execute a block of code only if a
specified condition is true.
Conditional (Selection) Structure:-
 if-else Statement:- The ‘if-else’ statement extends the if statement by
providing an alternative block of code to be executed if the condition is false.
Conditional (Selection) Structure:-
 If-else If-else Statement:- This structure is an extension of the if-else
statement, allowing you to check multiple conditions and execute different blocks of
code accordingly.
Conditional (Selection) Structure:-
 switch statement:- The ‘switch’ statement
is used when you have a variable or
expression that can take different values,
and you want to execute different blocks of
code based on those values.
Conditional (Selection) Structure:-
Nested-Conditional (Selection) Structure:-
 Nested conditional structures in C++
involve placing one or more conditional
structures inside another.

 This allows for more complex decision-


making logic where certain conditions are
checked only if the outer conditions are
satisfied.
Nested-Conditional (Selection) Structure:-
Nested-Conditional (Selection) Structure:-
Repetition structure (Loop or Iteration)
 Repetition structures, also known as loops, in C++ are used to execute a block of code
multiple times until a certain condition is met.

 In computer programming, a loop constitutes a series of instructions that iterates until a


specific condition is met.

 There are mainly two types of loops:

 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.

 The loop enables us to perform n number of steps together in one line.

Syntax

for (initialization expr; test expr; update expr)

// body of the loop

// statements we want to execute

}
Repetition structure (Loop or Iteration)
 Explanation of the Syntax:

• Initialization statement: This statement gets executed only once, at the


beginning of the for loop.

• These are declaration of multiple variables of one type, such as int x = 0,


a = 1, b = 2.

• These variables are only valid in the scope of the loop.

• 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.

 The loop execution is terminated on the basis of the test condition.

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.

• Update Expression: After executing the loop body, this expression


increments/decrements the loop variable by some value.
Repetition structure (Loop or Iteration)
Repetition structure (Loop or Iteration)
• do-while loop- similar to the while loop, but it guarantees that the code block is
executed at least once before the condition is checked.

• 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.

• Update Expression: After executing the loop body, this expression


increments/decrements the loop variable by some value.
Repetition structure (Loop or Iteration)
Jump Statements
 Jump statements- are employed to control the program flow based on certain conditions.

 They are utilized to conclude or extend the execution of a loop within a program or to
halt the execution of a function.

 Types of Jump Statements in C++:- In C++, there is four jump statement

 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.

 This statement can be incorporated within a for, while, do-while loop.


Jump Statements
Jump Statements
 Break- The C++ break statement is used to terminate the whole loop if the condition is
met.

 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.

 Labels can be placed anywhere in the program; there is no requirement to position


them before or after the corresponding goto statement.
Jump Statements
Jump Statements
 Return-The return statement is responsible for transferring control out of the function.

 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

You might also like