while loop in c++ with examples

Image result for c++Going forward with this C/C++ programming language tutorial today we will learn to use while loop. This needs a boolean statement. Statement will continue to run repeated times till given boolean expression evaluates to true. Lets look into this by an example given below. Again we will print square of numbers from 1 to 10 using while loop.
#include <stdio.h>

int
main() {
int
i=1;
int
sq;
while
(i<=10) {
sq = i*i;
printf("square of %d is %d.\n", i, sq);
i = i+1;
}


scanf("%d", &i);
return
1;
}

In the example given above initial value of i is 1. then boolean expression inside while loop is i<=10 which evaluates to true because i is 1, Hence all statements inside loop will be executed. See that we are increasing value of i inside loop by 1. Again boolean statement will be true because i is 2. Similarly program will continue. When value of i will reach 11, boolean statement will be false and program will go out of while loop. Try to run this program to see the output.

In next topic we will learn to use string and see some interesting examples.
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.