The while
statement provides an iterative loop. statement is executed repeatedly as long as expression is true. The test on expression takes place before each execution of statement.
while (expression) { //statement (code to be executed) }
As with the if-else statement, the while loop can execute multiple statements as a block by enclosing them in braces.
Let's see the simple program of while loop that prints table of 1.
#include<stdio.h> int main() { int i = 1; while (i <= 10) { printf("%d \n", i); i++; } return 0; }
#include<stdio.h> int main() { int i = 1, number = 0, b = 9; printf("Enter a number: "); scanf("%d", & amp; number); while (i <= 10) { printf("%d \n", (number * i)); i++; } return 0; }
#include<stdio.h> void main () { int j = 1; while(j+=2,j<=10) { printf("%d ",j); } printf("%d",j); }
#include<stdio.h> void main () { while() { printf("hello rookienerd"); } }
#include<stdio.h> void main () { int x = 10, y = 2; while(x+y-1) { printf("%d %d",x--,y--); } }
If the expression passed in while loop results in any non-zero value or when always evaluates to true then the loop will run the infinite number of times.
while(1){ //statement }
while(true){ //statement }