while loop in C Language
In C programming language, while loop is used to repeatedly run a block of statements until the control condition returns the result as false. The condition of the while loop is checked before the body of the loop is executed, hence it is called a pre-tested loop or entry-controlled loop.
Essential components required for a loop
- In while loop, a expression is used to check the condition. The block of statements defined inside the while loop will execute repeatedly until the given condition returns false.
- The condition will return 0, if it is true. The condition will return any non-zero number, if it is false.
- In while loop, the conditional expression is mandatory and there can have more than one conditional expressions.
- If while loop contains single statement, then the braces are optional.
- A while loop may run without any statement. E.g. while(condition);
while(condition)
{
//Statement(s) to be executed repeatedly
}
// Program to Print Multiples of a Given Number #include<stdio.h> #include<conio.h> void main() { int no, mul, i=1; printf("Enter a number : "); scanf("%d",&no); while(i<=10) { mul=i*no; printf("\n%d X %d = %d",&i,&no, &mul); i++; } getch(); } Output : Enter a number : 5 1 X 5 = 5 2 X 5 = 10 3 X 5 = 15 4 X 5 = 20 5 X 5 = 25 6 X 5 = 30 7 X 5 = 35 8 X 5 = 40 9 X 5 = 45 10 X 5 = 50
Flow Chart of while Loop
ADVERTISEMENT