0% found this document useful (0 votes)
0 views49 pages

Loop structure

The document explains looping statements in programming, detailing their purpose, types, and structures, including for loops, while loops, and do-while loops. It highlights the advantages of using loops, such as reducing code length and memory usage, and discusses control statements like break and continue. Additionally, it covers the differences between loop types and their appropriate usage scenarios.

Uploaded by

norainibrahim190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views49 pages

Loop structure

The document explains looping statements in programming, detailing their purpose, types, and structures, including for loops, while loops, and do-while loops. It highlights the advantages of using loops, such as reducing code length and memory usage, and discusses control statements like break and continue. Additionally, it covers the differences between loop types and their appropriate usage scenarios.

Uploaded by

norainibrahim190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

L O O P I N G STATEMENTS

What is Loop?

This refer to a set of statements in


a program executed repeatedly,
either a fixed number of times or
until some conditions in TRUE or
FALSE. It also the control
structures in which a block of
instructions is repeated until a
condition is fulfilled.
Why use Loop?

• Where need repetition


of same code a number
of times at the place
use Loop in place of
writing more than one
statements. The way of
the repetition will form a
circle that’s why
repetition statements
are called loops.
Conditional Code

Flowchart
of Looping Conditio
n
If condition is true

Statement
If condition is false
vantages of Using Looping statement

• Reduce the length of the code


• Take less memory space

Looping structure allows to execute a statement


or group of statements multiple times.
It provides the following types of loops to
handle the looping requirements:

1. For Loop
Looping
Structure 2. While Loop

3. Do . . . While Loop
For Loop Statement

• For loop is very easy to use and implement


because all the declaration for the initializing
value, conditions and increment/decrement is
written in a single line of code.
Here is the flow of control in a for loop −

• Initialization: step is executed first, and this is executed only once when we are
entering into the loop first time. This step allows to declare and initialize any
loop control variables. This is an assignment statement that is used to set a loop
control variable.

• Condition: is next step after initialization step, if it is true, the body of the loop
is executed. If it is false, the body of the loop does not execute, and flow of
control goes outside the for loop. This is a relational expression that determines
when the loop will exit by testing the loop control variable against some value.

• Increment or Decrements: After completion of Initialization and Condition


steps loop body code is executed and then Increment or Decrement steps is
executed. This statement allows to update any loop control variables. It defines
how the loop control variable will change each time the loop is repeated.
Flow Diagram
C++
A for loop is a repetition control structure that allows you
to efficiently write a loop that needs to execute a specific for
number of times. loop
For Loop Example
For Loop Example

Output
Example: For
loop

When you know exactly how many times you want to loop through a block of code, use
the for loop
C++ Nested For Loop
In C++, we can use for loop inside another for loop, it is known as nested for
loop. The inner loop is executed fully when outer loop is executed one time.
So, if outer loop and inner loop are executed 4 times, inner loop will be
executed 4 times for each outer loop i.e., total 16 times.
In C++, we can use while loop inside another while loop,
it is known as nested while loop. The nested while loop is
executed fully when outer loop is executed once.

Nested While Loop Example Output


While loop
C++ while loop

A while loop statement repeatedly executes a target statement


if a given condition is true.

Here, statement(s) may


be a single statement or
a block of statements.
The condition may be
any expression, and true
is any non-zero value.
The loop iterates while
When the condition becomes false, the condition is true.
program control passes to the line
immediately following the loop.
Flow Diagram

Here, key point of


the while loop is that the
loop might not ever run.

When the condition is


tested and the result is
false, the loop body will be
skipped and the first
statement after the while
loop will be executed.
When the code is compiled and executed, it
Example produces the following result −
Example Output
The major difference between a for-loop and
while loop is how they are invoked: A for-
loop contains three statements when
declared (variable, condition and
increment/decrement), whereas the
invocation of a while loop contains only the
conditional, with the variable and the
increment/decrement addressed elsewhere.
•while loop: Initialization and iteration (increment/decrement) are
done outside the loop structure, and only the condition is inside.
•for loop: Initialization, condition, and iteration are all included inside
the loop structure, making it more compact and self-contained.

2.Usage
•while loop is generally used when you don’t know the number of
iterations ahead of time, or when the condition could change during
the loop execution.
•for loop is often used when the number of iterations is known or
controlled by a loop counter, such as iterating over an array or a fixed
range.

3.Flexibility
•while loop: More flexible because the initialization and iteration
steps can be placed anywhere before or inside the loop.
•for loop: More structured and concise for loops that involve a known
number of iterations, especially when you can easily combine
while loop: Best when you don't know how many iterations, you'll
need in advance or when the loop condition may change unpredictably.

for loop: Ideal for situations where you know how many times you
need to iterate, such as counting or iterating over a range of values.
C++ Do…while loop
C++ do...while loop

• Unlike for and while loops, which test the loop condition at the top
of the loop, the do...while loop checks its condition at the bottom
of the loop.

• A do...while loop is like a while loop, except that a do...while loop is


guaranteed to execute at least one time.
Notice that the conditional
expression appears at the end of the
loop, so the statement(s) in the loop
execute once before the condition is
tested.

If the condition is true, the flow of


control jumps back up to do, and the
statement(s) in the loop execute
again. This process repeats until the
given condition becomes false.
Flow Diagram
Example of do - while loop
Example of do - while loop
C++ Nested do-while Loop

• In C++, if you use do-while loop inside another do-while loop, it is


known as nested do-while loop. The nested do-while loop is
executed fully for each outer do-while loop.
Loop Control Statements

• Loop control statements change execution from its normal


sequence. When execution leaves a scope, all automatic objects
that were created in that scope are destroyed.
C++ break statement

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

• If you are using nested loops (i.e., one loop inside another loop), the break
statement will stop the execution of the innermost loop and start executing the
next line of code after the block.
Flow Diagram
Example
C++ continue statement

• 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.
Flow Diagram
Example
The Infinite Loop

• A loop becomes infinite loop if a condition never becomes false.


The for loop is traditionally used for this purpose. Since none of the
three expressions that form the ‘for’ loop are required, you can
make an endless loop by leaving the conditional expression empty.
In C++, for, while, and do-while are three types of loops used to
repeatedly execute a block of code. Let's explore each one, their syntax,
and the key differences between them.

1. for Loop The for loop is used when the number of iterations is known
beforehand. It consists of three parts: Initialization: Sets the starting point.
Condition: Checks whether the loop should continue. Update: Modifies the
loop variable after each iteration.
Key Points: The loop is controlled by the condition in the middle.
Initialization happens once at the start, and the update occurs at the end
of each iteration. Best used when the number of iterations is known.

while Loop

The while loop is used when the number of iterations is not known
beforehand and depends on a condition.
Key Points:

•The condition is checked before each


iteration.

•If the condition is false initially, the loop will


not execute at all.

•Best used when the loop should continue


running based on a condition.
do-while Loop

The do-while loop is like the while loop, but the key difference is that it
executes the code block at least once, regardless of the condition.

Key Points:

•The condition is checked


after the code block
executes.

•The loop always runs at


least once, even if the
condition is false initially.

•Best used when the loop


must execute at least once.

You might also like