The while loop is a for loop stripped of the initialization and update parts; it has just a test condition and a body:
while (test-condition) body
If the expression evaluates to true, the program executes the statement(s) in the body.
The following code shows how to use while loop to read user input.
#include <iostream>
int main()/*from ww w . jav a2 s .co m*/
{
using namespace std;
char ch;
int count = 0; // use basic input
cout << "Enter characters; enter # to quit:\n";
cin >> ch; // get a character
while (ch != '#') // test the character
{
cout << ch; // echo the character
++count; // count the character
cin >> ch; // get the next character
}
cout << endl << count << " characters read\n";
return 0;
}
The code above generates the following result.
The following loop cycles through each character in a string and displays the character and its ASCII code.
#include <iostream>
const int ArSize = 20;
int main()/* ww w . j a v a2 s .co m*/
{
using namespace std;
char name[ArSize];
cout << "Your first name, please: ";
cin >> name;
cout << "Here is your name, verticalized and ASCIIized:\n";
int i = 0; // start at beginning of string
while (name[i] != '\0') // process to end of string
{
cout << name[i] << ": " << int(name[i]) << endl;
i++;
}
return 0;
}
The code above generates the following result.
The followng code shows how to use clock() and the ctime header to create a time-delay loop.
#include <iostream>
#include <ctime> // for clock() function, clock_t type
int main()
{/*from w w w.j a v a 2s . c o m*/
using namespace std;
cout << "Enter the delay time, in seconds: ";
float secs;
cin >> secs;
clock_t delay = secs * CLOCKS_PER_SEC; // convert to clock ticks
cout << "starting\a\n";
clock_t start = clock();
while (clock() - start < delay ) // wait until time elapses
; // note the semicolon
cout << "done \a\n";
return 0;
}
The code above generates the following result.
The following code uses a loop that terminates if the array is full or if you enter non-numeric input.
#include <iostream>
using namespace std;
const int Max = 5;
int main()/*from w w w .j av a 2 s .co m*/
{
double fish[Max];
cout << "You can enter up to " << Max
<< " numbers <q to terminate>.\n";
cout << "fish #1: ";
int i = 0;
while (i < Max && cin >> fish[i]) {
if (++i < Max)
cout << "#" << i+1 << ": ";
}
double total = 0.0;
for (int j = 0; j < i; j++)
total += fish[j];
if (i == 0)
cout << "No value\n";
else
cout << total / i << " = average of " << i << "\n";
return 0;
}
The code above generates the following result.
The do while loop is different from the other two because it's an exit-condition loop.
If the condition evaluates to false, the loop terminates; otherwise, a new cycle of execution and testing begins.
Such a loop always executes at least once.
Here's the syntax for the do while loop:
do body while (test-expression);
The body portion can be a single statement or a brace-delimited statement block.
#include <iostream>
using namespace std;
int main() { /*from ww w . java 2s .co m*/
int n;
cout << "Enter numbers in the range 1-10 to find ";
cout << "my favorite number\n";
do
{
cin >> n; // execute body
} while (n != 7); // then test
cout << "Yes, 7 is my favorite.\n" ;
return 0;
}
The code above generates the following result.