While loop in c program
While loop in C Program (Statement) :
The simplest of all the looping structures in C is the while statement . We have used while in many of our eariler programs . the basic format of the while statement is
The while is an entry-controlled statement .The fest-condition is evaluated and if the condition is true ,then the body of the loop is execution of the body , the test-condition is once agine evaluated and if it id ture, the body is executed once agine. This process of repeated exceution of the body continues until the test-condition finally becomes false and the control is transferred out of the loop On exit, the porgram continues with the statement immediately after teh body of the loop .
The body of the loop may have one or more statement .The braces are needed only if the body contains two or more statements .However it is a good practice to use braces even if the body has only one statem
Parts of while loop:
Unlike for loop, While does not contain initialization and update part. It contains only two parts - condition and body of loop.
Condition is a boolean expression evaluating an integer value. It is similar to if...else condition and define loop termination condition.
Body of loop contains single or set of statements. It define statements to repeat.
At this point, you might be thinking about loop counter variable-initialization and variable-update part. Where to put these? You are free to initialize loop counter variables anywhere in the program before its use. However, best practice is to initialize all important loop variable just before the loop. Likewise, you can keep your loop update part just before the end of loop.
Flowchart of while loop :
Example while loop in c program :
#include<stdio.h>
int main()
{
/* Declare loop i variable */
int i;
/* Run a loop from 10 to 20 */
i=10;
while(i<20){
/* increment value i */
i++;
/* Print current value of i */
printf("%d ",i);
}
}
output :
10 11 12 13 14 15 16 17 18 19 20
The simplest of all the looping structures in C is the while statement . We have used while in many of our eariler programs . the basic format of the while statement is
n (intilization)
While (Test condition)
{
…………
body
…………..
Incurement / decrement
}
The while is an entry-controlled statement .The fest-condition is evaluated and if the condition is true ,then the body of the loop is execution of the body , the test-condition is once agine evaluated and if it id ture, the body is executed once agine. This process of repeated exceution of the body continues until the test-condition finally becomes false and the control is transferred out of the loop On exit, the porgram continues with the statement immediately after teh body of the loop .
The body of the loop may have one or more statement .The braces are needed only if the body contains two or more statements .However it is a good practice to use braces even if the body has only one statem
Parts of while loop:
Unlike for loop, While does not contain initialization and update part. It contains only two parts - condition and body of loop.
Condition is a boolean expression evaluating an integer value. It is similar to if...else condition and define loop termination condition.
Body of loop contains single or set of statements. It define statements to repeat.
At this point, you might be thinking about loop counter variable-initialization and variable-update part. Where to put these? You are free to initialize loop counter variables anywhere in the program before its use. However, best practice is to initialize all important loop variable just before the loop. Likewise, you can keep your loop update part just before the end of loop.
Flowchart of while loop :
#include<stdio.h>
int main()
{
/* Declare loop i variable */
int i;
/* Run a loop from 10 to 20 */
i=10;
while(i<20){
/* increment value i */
i++;
/* Print current value of i */
printf("%d ",i);
}
}
output :
10 11 12 13 14 15 16 17 18 19 20


No comments