0% found this document useful (0 votes)
21 views

The Do-While Loop

The do-while loop executes a statement at least once even if the condition is not met. The for loop allows initialization, condition checking, and increment/decrement in one statement and is useful for repetitive tasks with counters. The break statement can be used to exit a loop early regardless of its normal condition.

Uploaded by

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

The Do-While Loop

The do-while loop executes a statement at least once even if the condition is not met. The for loop allows initialization, condition checking, and increment/decrement in one statement and is useful for repetitive tasks with counters. The break statement can be used to exit a loop early regardless of its normal condition.

Uploaded by

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

The do-while loop

Its format is
do statement while (condition);
Its functionality is exactly the same as the while loop, except that condition in
the do-while loop is evaluated after the execution of statement instead of before,
granting at least one execution of statement even if condition is never fulfilled.
For example, the following example program echoes any number enter until
enter 0.
number echoer
#include <iostream>
using namespace std;
int main ()
{
unsigned long n;
do {
cout<< "Enter number (0 to end): ";
cin>> n;
cout<< " entered: " << n << "\n";
} while (n != 0);
return 0;
} Enter number (0 to end): 12345
entered: 12345
Enter number (0 to end): 160277
entered: 160277
Enter number (0 to end): 0
entered: 0 Edit & Run
The do-while loop is usually used when the condition that has to
determine the end of the loop is determined within the loop statement itself, like
in the previous case, where the user input within the block is what is used to
determine if the loop has to end. In fact if never enter the value 0 in the
previous example can be prompted for more numbers forever.
The for loop
Its format is:
for(initialization;condition;increase)statement;
and its main function is to repeat statement while condition remains true, like
the while loop. But in addition, the for loop provides specific locations to
contain an initialization statement and an increase statement. So this loop is
specially designed to perform a repetitive action with a counter which is
initialized and increased on each iteration.
It works in the following way:
1. initialization is executed. Generally it is an initial value setting for a
counter variable. This is executed only once.
2. condition is checked. If it is true the loop continues, otherwise the loop
ends and statement is skipped (not executed).
3. statement is executed. As usual, it can be either a single statement or a
block enclosed in braces { }.
4. finally, whatever is specified in the increase field is executed and the loop
gets back to step 2.
Here is an example of countdown using a for loop:
// countdown using a for loop
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n--) {
cout<< n << ", ";
}
cout<< "FIRE!\n";
return 0;
} 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
The initialization and increase fields are optional. They can remain
empty, but in all cases the semicolon signs between them must be written. For
example we could write: for (;n<10;) if we wanted to specify no initialization
and no increase; or for (;n<10;n++) if we wanted to include an increase field but
no initialization (maybe because the variable was already initialized before).
Optionally, using the comma operator (,) we can specify more than one
expression in any of the fields included in a for loop, like in initialization, for
example. The comma operator (,) is an expression separator, it serves to
separate more than one expression where only one is generally expected. For
example, suppose that we wanted to initialize more than one variable in our
loop:
for ( n=0, i=100 ; n!=i ; n++, i-- )
{
// whatever here...
}
This loop will execute for 50 times if neither n or i are modified within the loop

n starts with a value of 0, and i with 100, the condition is n!=i (that n is not
equal to i). Because n is increased by one and i decreased by one, the loop's
condition will become false after the 50th loop, when both n and i will be equal
to 50.
The break statement
Using break we can leave a loop even if the condition for its end is not
fulfilled. It can be used to end an infinite loop, or to force it to end before its
natural end. For example, we are going to stop the count down before its natural
end (maybe because of an engine check failure?):
// break loop example
#include <iostream>
using namespace std;
int main ()
{
int n;
for (n=10; n>0; n--)
{
cout<< n << ", ";
if (n==3)
{
cout<< "countdown aborted!";
break;
}
}
return 0;
} 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!

You might also like