Pages

Friday, March 18, 2011

While Loop

while loop

                Syntax:
                                While(expression)
                                                {
                                                //loop body
                                                //what instruction to be executed
                                                }

                Evaluates the expression (condition) first before executing the statement under it (block of statement which is inside the curly braces { }.

Example:

                To execute the problem that was stated above, consider the following fragment of codes:
                                int a = 1;
                                while(a<4)
                                                {
                                                cout<<”Good Morning!”;
                                                a++; // or ++a;
                                                }

                In this example, the variable a is declared as integer and were initialized to 1. The next line refers now to the while loop structure. The expression on the while statement evaluate if the value of a is still less than 4. It means that variable a is expected to have a value of 1 to 3. The loop will check this from time to time if the value of a is still within this range and will continue to execute what is inside the loop body. Notice that there is a statement a++ (read as: increment the value of variable a by one after this line) in the loop body. This is to make sure that the value of variable a will not remain the same value (like for example, the initial value of variable a is 1). If this is remove, the loop will not be terminated since the value of variable a is 1, means the condition in the while loop is still true, therefore the loop will continue to execute the statement under it. This is what we called infinite loop.


Notes:
                This is just a fragment of codes. It is not complete. In order for you to run this code, put these codes inside your main function.
                ++a is equivalent to the expression: a = a + 1


Sample Problem 1:
                Write a program that displays the value from 1 to 10 horizontally separated by space.

                #include<iostream>
                using namespace std;

                main ( )
                {
                                int a = 1;

                                while(a <=10)
                                                {
                                                cout<<a<<” “;
                                                a++;
                                                }
                                return 0;
                }



In this example, if you notice that variable a is initialize to 1. This variable serves as a counter where what it does is just to increment its value. The expression(condition) inside the while statement is checking from time to time if the value of variable a is still less than or equal to 10 then it will continue to execute whatever inside the it (for this example, cout<<a<<” “ and a++). The expression a++ is to make sure that the value of variable a is incrementing. If this value is no longer in the range where the condition evaluates to false (where the value of variable a exceeds 10), this marks that the loop will terminate itself and will proceed to the next instruction (after the closing curly brace}) which is return 0.


Sample output:
                1 2 3 4 5 6 7 8 9 10

Sample Problem 2:
                Write a program that simulates a guessing game where a computer asks the user to guess the right number. If a user guess a number that is higher than the right number, the computer will response to the user to lower the guessed number. However, if the user guess a number that is lower than the right number, the computer will response to the user to lower the guessed number. And finally, if the user guessed the right number, the computer will response that “Congratulation! You have guessed the right number!”. The program will continue to ask the user until the user guess the right number.

#include<iostream>
using namespace std;

main( )
{
                int rightnumber = 25;
                int guess;

                while(guess!=rightnumber)
                {
                cout<<”What is the right number: “;
                cin>>guess;

                if(guess>rightnumber)
                                cout<<”Lower!”);

                else if(guess<rightnumber)
                                cout<<”Higher!”;
                else if(guess==rightnumber)
                                cout<<”Congratulation! You have guessed the right number!”;
                }
return 0;
}

Notes:
       Here the right number is assigned a value of 25. You may change this value if you want so that it will have variety. But you know that there is another to implement this by assigning a random numbers where values are assigned randomly by the computer. (srand and rand).

No comments:

Post a Comment