What is While loop in C Programming Language ?

while loop in c

In this article we will going to discuss about while loop (while statement) of C language in detail in which following topics are included like,

  • While loop in C
  • Syntax of while loop in C
  • Example of while loop in C

While loop in C

The while is an entry controlled loop (statement). The test condition is evaluated and if the condition is true, the body of the loop is executed. After the execution of the body, the test condition is once again is 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 become false and controlled transferred out of the loop. On exit, the program continue with statement immediately after the body of the loop.

Note:-

The body of the loop may have one or more statements. The braces are needed only if the body contains two or more statements. However, it is good practice to use braces even if body has only one statement.

Syntax of while loop in C

The basic format of while loop (statement) is 

while( test-condition)
{
   body of the loop ;
}

Example of while loop in C


/* Program to find the factorial of any number*/

#include<stdio.h>
int main( )
{
int n, num;
long fact=l;
printf ("Enter the number:")
scanf("%d" ,&n);
num=n;
if(n<0)
printf ("No factorial of negative number\n");
else
{
while(n>1)
{
fact*=n;
n- - ;
}
printf ("Factorial of %d "=%ld \ n", num , fact) ;
}
return 0;
}

_____________________________________________________________

example of while loop
_____________________________________________________________

Conclusion:

In this article we have learned about while loop in C in detail i hope after reading this article you have no any doubt remaining related to this topic but if you any doubt then feel free comment below

_____________________________________________________________



Post a Comment (0)
Previous Post Next Post