for, while, do-while difference


Now, we are going to see the following in detail.
difference between while and do while
difference between for and do while
difference between for and while

Difference between while and do-while loop
while loopdo-while loop
Expression is evaluated at the beginning in while loop.

Example:
     while (expression) {
            statement;
     }
Expression is evaluated at the end in do-while loop

Example:
     do {
            statement;
      } while (expression);
while loop executes the statement inside the loop only if the truth value of the expression is not 0.
do-while loop executes the statement inside the loop at least once, regardless of whether the expression is true or not.
Variable used in expression needs to be initialized at the beginning of the loop or before the loop

Example 1:
     // initialization - outside loop
     int i = 0;
     while (i < 5) {
          printf("Hello world\n");
          i = i + 1;
     }

Example 2:
  // initialization at the start of loop
     while ((ch = getchar()) != '\n') {
          printf("Hello world\n");
     }

Variable used in expression can be initialized inside loop or before the loop or at the end of the loop.

Example:
     int i, j = 0;
     do {
            printf("Hello World\n");
            if (j == 0) {
                 // initialization - inside loop
                 i = 0;
                 j = j + 1;
            } else {
                 i++;
            }
     } while (i < 5);


Difference between for and do-while loop

for loopdo-while loop
Expression is evaluated at the beginning in for loop.
Expression is evaluated at the end in do-while loop
for loop executes the statement inside the loop only if the truth value of the expression is not 0
do-while loop executes the statement inside the loop at least once, regardless of whether the expression is true or not.
Variable used in expression can be initialized at the beginning of the loop or before the loop

Example 1:
(initialization at the start of loop)
     for (i = 0; i < 5; i++) {
          printf("Hello wold");
     }

Example 2:
(initialization - before loop)
    int i = 0;
    for ( ; i < 5; i++) {
         printf("Hello world");
    }





Variable used in expression can be initialized inside inside the loop or before the loop or at the end of the loop.

Example 1:
(initialization inside loop)
    do {
          ch = getchar();
     } while (ch != '\n');

Example 2:
(initialization before the loop)
     int i = 0;
     do {
          printf("Hello world\n");
          i = i + 1;
      } while (i < 5);

Example 3:
(initialization at the end of the loop)
     do {
          printf("Hello world\n");
     } while ((ch = getchar()) != '\n');

Difference between for and while loop
for loop
while loop
Initialization, expression evaluation and iteration can be performed in single line

Initialization, expression evaluation and iteration cannot be performed in single line.

Mostly, for loop is preferred for known iterations

While loop is preferred for unknown iterations.

Below is the syntax for for loop:
     for (initialization;
              expression; iteration) {
                      statement;
     }

Below is the syntax for while loop.
     while (expression) {
          statement;
     }

Let us write a simple C program that illustrates while and do-while loop construct difference.

  #include <stdio.h>
  int main() {
        int i, j, k;
        j = k = 0;
        // variable(j) in expression is initialized outside the loop
        while (j < 5) {
                printf("While loop\n");
                j = j + 1;
        }

        printf("\n");
        do {
                printf("Do-While loop\n");
                if (k == 0) {
                        i = 0;
                        k = k + 1;
                } else {
                         //variable in expression is initialized inside loop
                        i = i + 1;
                }
 

Comments

Popular posts from this blog

textcolor in c

wherex in c

traffic light program in c, traffic light simulation