0% found this document useful (0 votes)
53 views7 pages

Loops

Loops allow blocks of code to be executed multiple times. There are three common types of loops in programming: while loops, do-while loops, and for loops. While loops check the loop condition at the top of the loop before each iteration. Do-while loops check the condition at the bottom, so the code block runs at least once. For loops allow initializing and updating a loop counter variable within the loop syntax. Loops can also be nested, with one loop inside another, to repeat code multiple times in an nested, nested fashion.

Uploaded by

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

Loops

Loops allow blocks of code to be executed multiple times. There are three common types of loops in programming: while loops, do-while loops, and for loops. While loops check the loop condition at the top of the loop before each iteration. Do-while loops check the condition at the bottom, so the code block runs at least once. For loops allow initializing and updating a loop counter variable within the loop syntax. Loops can also be nested, with one loop inside another, to repeat code multiple times in an nested, nested fashion.

Uploaded by

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

LOOP

 There may be a situation, when you need to execute a block


of code several number of times.
 A loop statement allows us to execute a statement or group of
statements multiple times
GENERAL FROM OF A LOOP STATEMENT
WHILE LOOP

 Repeats a statement or group of statements while a given


condition is true. It tests the condition before executing the
loop body.
int a = 10;
while( a < 20 )
{
System.out << "value of a: " << a << endl;
a++;
}
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 similar to a while loop, except that a
do...while loop is guaranteed to execute at least one time.
int a = 10;
do {
System.out.println << "value of a: " << a << endl;
a = a + 1;
}
while( a < 20 );
 Execute a sequence of statements multiple times
and abbreviates the code that manages the
loop variable.
General syntax
for ( init; condition; increment )
{
statement(s);
}

FOR LOOP
for( int a = 10; a < 20; a = a + 1 )
{
system.out. << "value of a: " << a << endl;
}

FOR LOOP…
NESTED LOOPS

 A loop can be nested inside of another loop. C++ allows at


least 256 levels of nesting.
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
} statement(s); // you can put more statements.
}

Same with while loop

You might also like