Monday, May 23, 2011

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 < 5)
    {
        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<5 is true. Since the condition is true, the body of while is executed. The body of while loop contains two statement: one that displays the name and other that increments the value of i by 1. After executing the increment statement, now the new value of i is 1. The while loop will again check the condition and since 1 is less than 5 the body of while will execute again. This will go on until the value of i is incremented to 5. Once the value of i is 5, the condition will become false as a result of which the body of loop won't execute and will terminate.

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 < 5 will also be always true. It means your loop will never stop, it will go on and on. This is called infinite loop. Try this by removing i++; from above program. Compile and run it again. Press ctrl+c to terminate the program.

/* 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 < 10)
    {
        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.

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

9 comments:

  1. hi how to do this.... enter if:10 prints statement per loops

    ReplyDelete
  2. what about we let the user decide how many numbers they want to input.. pls help thanks :)

    ReplyDelete
  3. What is the function of Return 0; ?

    ReplyDelete
  4. list out the merits and demerits of while loop and do while loop .

    ReplyDelete
  5. what are the different base of constructing multiway if else structure ?

    ReplyDelete
  6. This is great, I may need your help clearing up some things so I can ace my final project for school

    ReplyDelete
  7. ​I'm proud of you, sir! This is a very interesting point of view. I appreciate your great work, and would like to add that our services are effective in writing excellent projects for high schools, colleges, Universities, and other educational establishments! Check it out by following http://dissertationwriting.services/! Happy browsing!

    ReplyDelete