This blog is under construction

Saturday 28 December 2013

for, while, do-while difference

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

Difference between while and do-while loop
while loop do-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 loop do-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;
                }
        } while (i < 5);

        printf("\nValue of i is %d\n", i);


        /*
         * value of i is 5 by this time.  So, the truth value
         * of expression in both while and do-while will be 0.
         * Statement inside while loop won't be executed.  Whereas,
         * statement inside do-while will be executed once
         * irrespective of truth value of the expression
         */
        while (i < 0) {
                printf("Truth value of my expression is 0 - while loop\n");
        }

        do {
                printf("Truth value of my expression is 0 - do-while loop\n");
        } while (i < 0);

        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  While loop
  While loop
  While loop
  While loop
  While loop

  Do-While loop
  Do-While loop
  Do-While loop
  Do-While loop
  Do-While loop
  Do-While loop

  Value of i is 5
  Truth value of my expression is 0 - do-while loop



2 comments:

  1. Dell Laptop Service center are giving repair service at the door. We should high quality Dell out of warranty Laptop Repair, removal of virus, screen removal, wireless network set up, battery removal, motherboard replacement to several other are offered at budget friendly price and it’s Negotiable. We can fix them all in time by our well experience and certified technicians. If you want to repair your laptop in front of your eyesight, than you may call us: 7217871051

    ReplyDelete
  2. are searching for the security companies in stockport then I will just say visit right now leisureguardsecurity.co.uk and get the service at cheap rates.

    ReplyDelete