0% found this document useful (0 votes)
31 views27 pages

Chapter 5

The document discusses control statements, specifically focusing on loops in programming. It explains different types of loops including for loops, while loops, do...while loops, and nested loops, detailing their structure and execution flow. Examples are provided to illustrate how to use these loops effectively in code.

Uploaded by

munaaliss391
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)
31 views27 pages

Chapter 5

The document discusses control statements, specifically focusing on loops in programming. It explains different types of loops including for loops, while loops, do...while loops, and nested loops, detailing their structure and execution flow. Examples are provided to illustrate how to use these loops effectively in code.

Uploaded by

munaaliss391
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

Control Statements

Loops
Chapter 5
Loops

A loop statement allows the repetition of the


execution of a single statement or a group of
statements multiple times until a certain condition
is met.
A loop is a sequence of instructions that is repeated until a certain condition is reached.
Loop type: for loop

firstly initializes, then,


condition check, execute
body, update.
Illustrates the way a loop works
Flow chart of for loop
Loop type: for
Suppose we want to print “Hello World” 5 times.
Example of hiding declared variables before a loop
But if you want to use the already declared variable and not hide it, then must not redeclare that variable.
This example will only print even values between 0 and 10:
This example will only print values from 10 to 19:
Loop type: while

Repeats a statement or while (expression)


group of statements
while a given condition is
{
true. It tests the condition statement;
before executing the loop
body. }
The while loop loops through a block of code as long as a specified condition is true
Loop type: while
Loop type: while
Loop type: while
Loop type: while
Loop type: while
Loop type: while
Loop type: do … while

The do/while loop is a different of do {


the while loop. This loop will execute
the code block once, before checking if statement;
the condition is true, then it will repeat
the loop as long as the condition is }
true.
while(condition);
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.

The C++ do-while loop is executed at least once


because condition is checked after loop body.
Loop type: do … while
Loop type: do … while
Loop type: do … while

Do not forget to increase the variable used in the condition, otherwise the loop will never end!
Loop type: do … while
Loop type: do … while
Loop type: nested loops

for ( init; condition;increment ) {


You can use one or more loop for ( init; condition; increment ) {
inside any another ‘while’, statement(s);
‘for’ or ‘do ... while’ loop.
}
statement(s);
Loop inside a loop (any loops)
// you can put more statements.
}
Loop type: nested loops
Loop type: nested loops

You might also like