Syntax of using while loop:
- The statements within the while loop would keep in getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop.
- The condition being tested may use relational or logical operators as shown in the following examples:
while(x>=20 && x<=50)
while(x>=10 || (i>=10 && i<=20)||y==15)
- Almost always, the while must test a condition that will eventually become false, otherwise the loop would be executed forever,indefinitely.
- It is not necessary that a loop counter must only be an int. It can even be a float.
- Example of while loop:
One or more loop which require execute again and again then these loop and loops place in separate block is known as nested loop.
Example of nested while loop:
while(c<=2) /*inner loop*/
printf("r=%d c=%d sum=%d\n",r,c,s);