+1-617-874-1011 (US)

  +44-117-230-1145 (UK)

The while statement

The simplest of all looping structure in C is the While Statement. The general format of the While Statement is:

while (test condition)
{
body of the loop
}

Here the given test condition is checked and if the condition is true then the body of the Loop is executed. After the execution of the body, the test condition is once again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body continues until the test condition finally becomes false and the control is transferred out of the Loop.

On exit, the program continues with the statements immediately after the body of the Loop. The body of the Loop may have one or more statements. The braces are needed only if the body contained two are more statements.

#include <stdio.h>
#include <conio.h>
int main()
{
int x=0;
char ch;
printf("Enter a phrase whose length has to be found(terminated by enter key): \n");
while(ch!= '\n')
{
ch=getchar();
x++;
}
printf("Length is: %d",x-1);
getch();
return;
}

The above program returns the number of characters of a phrase or statement which is terminated by return key.

Below is the output of program :

while statement
While Statement Assignment Help

Topics in C Programming