Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Repetition Statement: The While Loop in c++

Repetition Statement: The While Loop in c++


The fundamental to programming are control statements. You must learn to have good command over control statement, if you want to program.
Sequence, selection and repetition are three types of control statement. It specifies the order in which the statement are executed.

(1) Sequence Structure: The sequence structure is built into c++. The c++ statement are executed in sequence, that is one after the other if not directed otherwise. Hope you know this. If not then remember from now on.

(2) Selection Statement: Selection statement are of three types: if, if-else and switch statement. We have already learned these in previous chapters.

(3) Repetition Statement: Repetition statement are of three types: while loop, for loop and do-while loop. We have to learn about these in this and coming chapters.

In programming, often the situation arises in which you have to repeatedly execute a particular code or set of codes. Suppose you want to print your name to the console five times. What you will do? If you have no knowledge of repetition statement, you will write your name in cout 5 times. Something like below:

/* A c++ Program example that should have been written using repetition statements */


// Program 1

#include <iostream>

using namespace std;

int main ()
{
    cout << "Mohammed Homam" << endl;
    cout << "Mohammed Homam" << endl;
    cout << "Mohammed Homam" << endl;
    cout << "Mohammed Homam" << endl;
    cout << "Mohammed Homam" << endl;

    return 0;
}

But what if you have to print it 100 or 1000 times? It would be bothersome and also the code will be too lengthy if we use the above method. To simplify such task we use repetition statement.

The While Loop


/* A c++ program example that uses while loop to print the name 5 times using incrementation in the loop */


// Program 2

#include <iostream>

using namespace std;

int main ()
{
    int i = 0;
    while (i     {
        cout << "Mohammed Homam" << endl;
        i++;
    }


    return 0;
}

Now lets analyse that what's happening in the above program. Look at the program while reading the each statement of explanation. We declared an integer i and intialised it to 0. After that while loop checks whether i is less than 5. Currently the value of i is 0 and hence the condition i
What's the use of increment statement in the above program?
If you don't increment the value of i in the above program, then value of i will always be 0 and hence the condition i

/* A c++ program example that uses while loop to print the name 5 times using decrementation in the loop */


// Program 3

#include <iostream>

using namespace std;

int main ()
{
    int i = 5;
    while (i > 0)
    {
        cout << "Mohammed Homam" << endl;
        i--;
    }

    return 0;
}


The above program does the same thing as its above program. The logic used is different. Here we gave the intial value to i as 5. Now the condition is, that i must be greater than 0. As the body of while loop is executed each time, the value of i is decremented by 1. The loop terminates when the value of i becomes 0.

/* A c++ program example that uses while loop to print number from 0 to 9 using incrementation in the loop */


// Program 4

#include <iostream>

using namespace std;

int main ()
{
    int i = 0;
    while (i     {
        cout << i << endl;
        i++;
    }

    return 0;
}

/* A c++ program example that uses while loop to print number from 0 to 9 using decrementation in the loop */


// Program 5

#include <iostream>

using namespace std;

int main ()
{
    int i = 9;
    while (i >= 0)
    {
        cout << i << endl;
        i--;
    }

    return 0;
}

The above program were simplest one's to get started into looping. In the next chapter we will learn about the for loop.


This post first appeared on C++ Programming, please read the originial post: here

Share the post

Repetition Statement: The While Loop in c++

×

Subscribe to C++ Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×