Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

LOOP CONTROL

ICT-GROUP 3
WHAT IS LOOP CONTROL?
C++ Loops are used to execute the same block of code a
specified number of times or while a specified condition is
true. Very often when we write code, we want the same block
of code to run over and over again in a row. Instead of
adding several almost equal lines in a code we can use
loops to perform a repetitive task like this.

A control loop is a system made up of all the hardware


components and software control functions needed for the
measurement and adjustment of a variable that controls an
individual process.
IN C++ THERE ARE MAINLY THREE DIFFERENT KINDS OF
LOOPS:
while loop– loops through a block of code while
a specified condition is true.
do-while loop– a variant of the while loop which
will always be executed at least once, even if
the condition is false, because the code is
executed before the condition is tested.
for loop– loops through a block of code a
specified number of time
The C++ for loop is used to iterate a part
of the program several times. If the number of
iteration is fixed, it is recommended to use for loop
than while or do-while loops.

The C++ for loop is same as C/C#. We can initialize


variable, check condition and increment/decrement
value.

for(initialization; condition; incr/decr){ //code to be


executed }
In programming, loop control refers to the ability to modify the flow of
a loop, such as prematurely exiting the loop or skipping certain iterations.

To declare loop control, you typically use control statements such as


"break" or "continue". Here's what each statement does:

"break" statement: immediately exits the loop, regardless of the loop


condition. The program continues execution after the loop. "continue"
statement: immediately skips the current iteration of the loop and moves
to the next iteration. The loop condition is still checked before each
iteration.
DECLARING LOOP CONTROLS
BREAK CODING EXAMPLE
CODE OUTPUT
BREAK CODING EXAMPLE
CODE OUTPUT
CONTINUE CODING EXAMPLE
CODE OUTPUT
CONTINUE CODING EXAMPLE
CODE OUTPUT
THE SYNTAX OF FOR-LOOP
syntax of for-loop is: for
(initialization; condition; update) { //
body of-loop }

Here,
initialization - initializes variables and is
executed only once
condition - if true, the body of for loop is
executed if false, the for loop is
terminated
update - updates the value of initialized
variables and again checks the condition
THE WHILE LOOP
AND DO-WHILE
DO-WHILE
LOOP PROGRAM
DIFFERENCE
WHAT IS WHILE LOOP?

The while loop is used when we want the


loop to execute and continue executing while the
specified condition is true.

As the condition is checked at the beginning of the


loop, it is also called entry controlled loop.
A "While" Loop is used to repeat a specific block of code an unknown
number of times, until a condition is met. For example, if we want to ask
a user for a number between 1 and 10, we don't know how many times
the user may enter a larger number, so we keep asking "while the number
is not between 1 and 10".

How does a while loop work?


The while loop checks the condition first, and if it returns true, the
code within it runs. The loop continues until the condition provided
returns false, then stops. Alternatively, the do while loop runs its code
once before checking the condition and runs again only if the condition is
true.
FLOWCHART OF
FOR-LOOP IN C++
WHILE LOOP CODING EXAMPLE
CODE
WHILE LOOP CODING EXAMPLE
OUTPUT
WHILE LOOP CODING EXAMPLE
CODE OUTPUT
WHAT IS DO-WHILE LOOP?
The do-while loop is a variant of the while loop. This loop
will always execute a block of code at least once, and then it
will repeat the loop as long as the specified condition is true.
This loop will always be executed at least once, even if the
condition is false, because the code is executed before the
condition is tested. As the condition is checked at the end of
the loop, it is also called exit controlled loop.

The do while loop is an exit controlled loop, where even if


the test condition is false, the loop body will be executed at
least once. An example of such a scenario would be when you want
to exit your program depending on the user input.
DO- WHILE LOOP CODING EXAMPLE
CODE OUTPUT
DO-WHILE LOOP CODING EXAMPLE
CODE OUTPUT
WHAT IS THE DIFFERENCE OF
WHILE LOOP AND DO-WHILE LOOP?
The difference between while and do while loop is that in the
while loop the condition is checked prior to executing any
statements whereas in the case of do while loop, statements are
run at least once, and then the condition is verified.

The main difference between a while loop and do while loop is


that while loop check condition before iteration of the loop. On
the other hand, the do-while loop verifies the condition after
the execution of the statements inside the loop. Furthermore,
the while loop is known as the entry-controlled loop.
Conversely, the do while loop is called the exit controlled
loop.
Structure
– The while loop is the most basic looping structure used in programming and is used where
the number of iterations are unknown. The while loop is used to execute a block of code until
the condition is true, meaning the loop keeps running until the required condition is met. The
do-while loop is very similar to the while loop except it performs the statements inside the
loop exactly once before evaluating the condition of the loop and it runs at least once,
regardless of whether the condition is met.
Condition
– The do-while loop is almost identical to the while loop except the condition is always
executed after the body of the loop. In a while loop, the body is executed only if a certain
condition is met and it terminates when the condition is false. That could happen on the first
try or the twenty-fifth try. The do-while loop, on the other hand, guarantees that the body is
always executed at least once, regardless of whether the condition is false during the first try.
Unlike the while loop, the condition to stop the loop does not tested until after the statements
in the loop have executed.
USE OF BREAK STATEMENT
The break statement ends execution of the nearest
enclosing loop or conditional statement in which it
appears. Control passes to the statement that follows
the end of the statement, if any. The break statement is
frequently used to terminate the processing of a
particular case within a switch statement. Lack of an
enclosing iterative or switch statement generates an
error. Within nested statements, the break statement
terminates only the do, for, switch, or while statement
that immediately encloses it. You can use a return or go
to statement to transfer control elsewhere out of the
nested structure.
The break statement ends execution of the nearest
enclosing loop or conditional statement in which it
appears. Control passes to the statement that follows
the end of the statement, if any.

The break statement is used with the conditional switch


statement and with the do, for, and while loop
statements. In a switch statement, the break statement
causes the program to execute the next statement outside
the switch statement.
THANK YOU FOR
LISTENING!
I hope you can get useful
knowledge from this presentation.
Good luck !

You might also like