Loop structure
Loop structure
What is Loop?
Flowchart
of Looping Conditio
n
If condition is true
Statement
If condition is false
vantages of Using Looping statement
1. For Loop
Looping
Structure 2. While Loop
3. Do . . . While Loop
For Loop Statement
• 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.
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.
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.
• 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
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 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: