A while loop is a simple loop that will run the same code over and over as long as a given conditional is true. The condition is checked at the beginning of each run through the loop ( including the first one ). If the conditional is false for the beginning, the while loop will be skipped all together.
while ( conditional ) {
// loop body
}
Example:
Example:
#include <iostream>
using namespace std;
int main () {
// while loop example
int i=0;
while(i
< 5)
{
cout << "Apple: "
<< i << endl; // output
i++; // increment counter
}
return 0;
}
Output:
No comments:
Post a Comment