C Programming

while loop

Posted on 07th June 2013

Loops are used to execute a set of statements repetitively. The number of times to repeat the loop is decided by a condition. C provides three kinds of looping statements. The simplest among them is the while loop.

while loop is written in the form

while (condition)
{
	statements;
}

while loop will evaluate a condition at the beginning of the loop. If the condition is true, the block of statements between the curly braces is executed. The condition is evaluated again and the loop is repeated if the condition is still true. This process continues until the condition evaluates to false or 0.

Example

Here is an example of a program that prints the numbers from 1 to 10 using while loop.

#include <stdio.h>

int main() {
int num;
num=1;
while (num <= 10) {
printf("%d \\n",num);
num++;
}

return 0;
}

Post a comment

Comments

Nothing yet..be the first to share wisdom.