Kamis, 12 Januari 2012

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:


// Program 1

#include

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.



// Program 2

#include

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.


// Program 3

#include

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.


// Program 4

#include

using namespace std;

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

    return 0;
}


// Program 5

#include

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.


View the original article here



Peliculas Online

0 komentar:

Posting Komentar