0% found this document useful (0 votes)
39 views25 pages

Tle Group 3

Uploaded by

Della Vxyi
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)
39 views25 pages

Tle Group 3

Uploaded by

Della Vxyi
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/ 25

• Loop Control

• Types of Loop
• Declaring
Loop Control
• The while
loop/do while
loop by: Group 3
LOOP
CONTROL
LOOPS
Loop Control in C++
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.

C++ break and continue statements There are two special statements that
can be used inside loops: break and continue. See examples below.
◊ Using “break”
statement: The break
statement will break the
loop and continue
executing the code that
follows after the loop (if
any). See the following
example.
◊ Using “continue”
statement : The continue
statement will break the
current loop and continue
with the next value. See
the example below.
TYPES OF
LOOP
LOOPS
There are three types of loops used in
C++ programming:

• for loop
• while loop
• do while loop
1.) for loop in C++

A for loop is a control structure that enables a set of instructions to get


executed for a specified number of iterations. It is an entry-controlled
loop.
SYNTAX:
for (initialization; condition; increment/decrement) {
// code to be executed
}
return 0;
}
EXAMPLE 1 EXAMPLE 2
2.) while loop in C++
While the loop is also an entry-controlled loop, we verify the condition specified
before running the loop. The difference is that we use For loops when we know
the number of times the body of the loop needs to run, whereas we use while
loops in circumstances when beforehand we do not know the precise number of
times the body of the loop needs to run. The execution of the loop is terminated
based on the test condition.

SYNTAX :
while (test_expression) {
// statements to execute in the loop body
update_expression;
}
EXAMPLE 1 EXAMPLE 2
3.) Do while loop
The do-while loop iterates a section of the C++ program several times.
In the do-while loop, a test expression is added at the bottom of the
loop. The loop body comes before the test expression. That’s why the
loop body must execute for once, even when test expression evaluates
to false in the first test.
SYNTAX:

do{
//code
}while(condition)
;
The condition is test expression. It must
be true for the loop to execute. The
{ and } mark the body of do while loop. It
comes before the condition. Hence, it is
executed before the condition.
EXAMPLE 1 EXAMPLE 2
Declaring
Loop Control
LOOPS
The variable that controls a for loop is needed only for
the purposes of the loop and is not used elsewhere.
When this is the case, it is possible to declare the
variable inside the initialization portion of the for.
When you declare a variable inside a for loop, there is
one important point to remember: the scope of that
variable ends when the for statement does. (That is, the
scope of the variable is limited to the for loop.)
EXAMPLE 1 EXAMPLE 2
While Loop

LOOPS
A while loop is used for executing a
statement repeatedly until a given condition
returns false. Here, statements may be a
SYNTAX:
single statement or a block of statements.
while(test_expression) {
The condition may be any expression, and
// statements to execute in the loop
true is any nonzero value. While loop checks
body
the condition written inside '()' is true or not.
If the condition is true, then statements update_expression;
written in the body of the while loop i.e., }
inside the braces { are executed again
condition is checked the process repeats until
the condition is false.
EXAMPLE 1 EXAMPLE 2
Do While
Loop
LOOPS
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 SYNTAX:
will repeat the loop as long as the do {
specified condition is true. Consequently, //code to be executed
if the condition is false, the body will }
execute exactly once, the code is while(condition);
executed before the condition is tested.

Do-while loop is also known as exit


controlled or post tested loop.
EXAMPLE 1 EXAMPLE 2
The Difference between while and
1. "While" loop: 2. The "do-while" loop
doiswhile -loop
- The loop condition The code block is executed first,
evaluated first, and the code followed by the evaluation of the
block is executed loop condition.
afterwards. - The code block will always run at
- If the condition is met, the least once, regardless of the
code block will execute. condition.
- If the condition is false, - If the condition is met, the loop will
the loop does not run at all. continue to execute.
- Also called Entry- - If the condition is false, the loop
Controlled Loop. will terminate.
- Also known as Exit Controlled
Loop
thank
you
did you learn something?

You might also like