WHILE AND DO WHILE LOOP in C

While loop in C

  • The operation is slightly different than the for loop, but the same operation can be implemented by for, while or do-while loops
  • The operation of the while loop is such that, first the condition is checked.if the condition is true, then the statement is executed the condition is again checked and this keeps on repeating until the condition is false.
While loop in c
While Loop in C
  • The operation of while loop is such that first the condition is checked
  • If the condition is true, then the statements are executed. Once the statements are executed, the condition is again checked and this keeps on repeating until the condition is false
  • If the condition is false, the statements inside the loop are not executed, instead, the control directly comes out of the loop
  • It is often the case in programming that you want to do something a fixed number of times
  • The statements within the while loop would keep on getting executed until the condition being tested remains true
  • When the condition becomes false, then control passes to the first statement that follows the body of the while loop
  • This is called as an entry controlled loop, as the entry inside the loop is possible only if the condition is true
  • The initializations and the updating is not a part of the syntax
SYNTAX of while loop
while(condition)
{
-
statements;
-
}

Do-while loop in C

In this case, the operation is slightly different i.e first the statements are executed and then the condition is checked.if the condition is true the statement is executed again. If the condition is false, the statement is not executed again.

do while
do while in C
  • One major point to be noted is that in the case of the do-while loop, the statements are executed at least once even if the condition is not true for the first statement
  • On the other hand, in case of for loop and while loop, even for the first time the statements are executed only if the condition is true
  • This is called an exit controlled loop, as the entry inside this loop is sure i.e. no condition is checked to enter inside the loop. Not the exit is possible only if the condition is false
  • The initializations and the updating is not a part of the syntax
SYNTAX OF DO-WHILE LOOP
do
{
-
statement
-
}while(condition);

1.Display the reverse digit of a user-entered number

#include<stdio.h>
#include<conio.h>
void main()
{
int n,rev=0,digit;
clrscr();
printf("enter the number:");
scanf("%d",&n);
while(n!=0)
{
digit =n%10;
rev=rev*10+digit;
n=n/10;
}
printf("the reverse of number is:%d",rev);
getch();
}
OUTPUT:
enter the number:1234
the reverse number is:4321

1.Display the factorial of the number

#include <stdio.h>
int main()
{
    int number;
    long long factorial;
    printf("Enter an integer: ");
    scanf("%d",&number);
    factorial = 1;
    // loop terminates when number is less than or equal to 0
    while (number > 0)
    {
        factorial *= number;  // factorial = factorial*number;
        --number;
    }
    printf("Factorial= %lld", factorial);
    return 0;

}

Output
Enter an integer: 5
Factorial = 120
Translate »