Loops And Its Type In Cpp

Loops:

There are three looping structures available in C++, all of which are used to execute a specific code block multiple times. Just as with the conditional if statement, the curly brackets for the loops can be left out if there is only one statement in the code block.

Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.

The while loop:

The while loop runs through the code block only if its condition is true, and will continue looping for as long as the condition remains true. Bear in mind that the condition is only checked at the start of each loop.

Syntax
while (expression) statement

Flow Chart :

flow chart of while loop in c++

Its functionality is simply to repeat statement while the condition set in expression is true.

For example, we are going to make a program to countdown using a while-loop:

Example :

CODE/PROGRAM/EXAMPLE
// custom countdown using while
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << n << ", ";
--n;
}
return 0;
}

O/P : Enter the starting number > 8
8, 7, 6, 5, 4, 3, 2, 1

When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop begins, if the value entered by the user fulfills the condition n>0 (that n is greater than zero) the block that follows the condition will be executed and repeated while the condition (n>0) remains being true.

The whole process of the previous program can be interpreted according to the following script (beginning in main):

  • 1. User assigns a value to n
  • 2. The while condition is checked (n>0). At this point there are two posibilities:
  • * condition is true: statement is executed (to step 3)
  • * condition is false: ignore statement and continue after it (to step 5)
  • 3. Execute statement:
    cout << n;
    --n;

    (prints the value of n on the screen and decreases n by 1)
  • 4. End of block. Return automatically to step 2
  • 5. Continue the program right after the block: print FIRE! and end program. When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide within the block some method to force the condition to become false at some point, otherwise the loop will continue looping forever. In this case we have included --n; that decreases the value of the variable that is being evaluated in the condition (n) by one - this will eventually make the condition (n>0) to become false after a certain number of loop iterations: to be more specific, when n becomes 0, that is where our while-loop and our countdown end. Of course this is such a simple action for our computer that the whole countdown is performed instantly without any practical delay between numbers.

The do-while loop:

The do-while loop works in the same way as the while loop, except that it checks the condition after the code block. It will therefore always run through the code block at least once. Notice that this loop ends with a semicolon.

Syntax
do statement while (condition);

Flow chart :

flow chart do while loop in c++

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 you enter until you enter 0.

Example :

CODE/PROGRAM/EXAMPLE
#include <iostream>
using namespace std;
int main ()
{
unsigned long n;
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
} while (n != 0);
return 0;
}

O/P : Enter number (0 to end): 12345
You entered: 12345
Enter number (0 to end): 160277
You entered: 160277
Enter number (0 to end): 0
You entered: 0

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 you never enter the value 0 in the previous example you can be prompted for more numbers forever.

The for loop:

The for loop is used to run through a code block a specific number of times. It uses three parameters. The first one initializes a counter and is always executed once before the loop. The second parameter holds the condition for the loop and is checked before each iteration. The third parameter contains the increment of the counter and is executed at the end of each loop.

Syntax
for (initialization; condition; increase) statement;
flow chart of for loop in c++

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.
CODE/PROGRAM/EXAMPLE
//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 << 

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:

CODE/PROGRAM/EXAMPLE
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:

#loops_and_its_type_in_c++ #loop_in_cpp #loop_in_c++ #while_loop_in_c++ #do-while_loop_in_c++ #for_loop_in_c++ #example_of_for_loop_in_c++ #example_of_while_loop_in_c++ #example_of_do_while_loop_in_c++

(New page will open, for Comment)

Not yet commented...