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;
}
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 10Sample 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).
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