Difference between while & do while loops in C

C  Loop control statement

Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.

Types of loop control statements in C:

There are 3 types of loop control statements in C language. They are,
    1. for
    2. while
    3. do-while

Output:

0 1 2 3 4 5 6 7 8 9

Example program (while loop) in C:

      In while loop control statement, loop is executed until condition becomes false.

Output:

3 4 5 6 7 8 9

Example program (do while loop) in C:

      In do..while loop control statement, while loop is executed irrespective of the condition for first time. Then 2nd time onwards, loop is executed until condition becomes false.

Output:

Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4

Difference between while & do while loops in C:

S.no
while
do while
1 Loop is executed only when condition is true. Loop is executed for first time irrespective of the condition. After executing while loop for first time, then condition is checked.

3 comments:

  1. #include
    main()
    {
    int w=1;
    while(w<=7)
    {
    printf("%d\n",w);
    w++;
    }
    }

    ReplyDelete
  2. #include
    main()
    {
    int k;
    for(k=1;k<=8;k++)
    {
    printf("%d\n",k);
    }
    }

    ReplyDelete
  3. #include
    main()
    {
    int m=4;
    do
    {
    printf("The value of is %d\n",m);
    m++;
    }
    while(m<=7);
    }

    ReplyDelete