The difference between while and do while in C language

First briefly introduce the while and do while statements in C language

while loop

grammar

Syntax of while  loop in C language  :

while(condition)
{
   statement(s);
}

Here, statement(s)  can be a single statement or a code block composed of several statements.

condition  can be any expression, and it is true when it is any non-zero value. Execute the loop while the condition is true. When the condition is false, the loop is exited and program flow continues with the next statement immediately following the loop.

flow chart

Here, the key point of the while  loop is that the loop may not execute even once. When the condition is false, the body of the loop is skipped and the next statement following the while loop is executed directly.

#include <stdio.h>
 
int main ()
{
   /* 局部变量定义 */
   int a = 10;

   /* while 循环执行 */
   while( a < 20 )
   {
      printf("a 的值: %d\n", a);
      a++;
   }
 
   return 0;
}

 

When the above code is compiled and executed, it produces the following result:

Value of a: 10 
Value of a: 11 
Value of a: 12 
Value of a: 13 Value of a 
: 14 Value 
of a: 15 Value of a: 16 
Value 
of a: 17 
Value of a: 18 
Value of a: 19

do...while loop

Unlike  for  and  while  loops, they test the loop condition at the head of the loop. In C, a do...while  loop checks its condition at the end of the loop.

The do...while  loop is similar to the while loop, but the do...while loop ensures that the loop is executed at least once.

grammar

Syntax of do...while  loop in C language  :

do
{
   statement(s);

}while( condition );

Note that the conditional expression appears at the end of the loop, so the statement(s) in the loop will be executed at least once before the condition is tested.

If the condition is true, the control flow jumps back to the do above, and then re-executes the statement(s) in the loop. This process is repeated until the given condition becomes false.

flow chart

example

#include <stdio.h>
 
int main ()
{
   /* 局部变量定义 */
   int a = 10;

   /* do 循环执行,在条件被测试之前至少执行一次 */
   do
   {
       printf("a 的值: %d\n", a);
       a = a + 1;
   }while( a < 20 );
 
   return 0;
}

 

When the above code is compiled and executed, it produces the following result:

Value of a: 10 
Value of a: 11 
Value of a: 12 
Value of a: 13 Value of a 
: 14 Value 
of a: 15 Value of a: 16 
Value 
of a: 17 
Value of a: 18 
Value of a: 19

The difference between while and do while in C language

While and do while are both loop statements in C language, the main difference between them is the order in which the loop body is executed.

The while loop first checks the loop condition and executes the loop body only if the condition is true. Therefore, if the condition is false to begin with, the body of the loop will never execute. And if the condition is always true, then the loop will continue to execute.

while: You owe me money, I'm walking, and there's a person in front of me, I'll check to see if this person is you, and if not, I won't hit you.

do while: You owe me money, I am walking, there is a person in front, I don't care if this person is you, call first and then talk.

The following is an example program of a while loop:

example

#include <stdio.h>

int main() {
    int i = 0;
    while (i < 5) {
        printf("i = %d\n", i);
        i++;
    }
    return 0;
}

 

The output is:

i = 0
i = 1
i = 2
i = 3
i = 4

The main difference between a do while loop and a while loop is the order in which they execute the loop body. A do while loop first executes the loop body and then checks the loop condition. Therefore, even if the condition is false to begin with, the body of the loop will be executed at least once.

The following is an example program of a do while loop:

example

#include <stdio.h>

int main() {
    int i = 0;
    do {
        printf("i = %d\n", i);
        i++;
    } while (i < 5);
    return 0;
}

The output is:

i = 0
i = 1
i = 2
i = 3
i = 4

You can see that even though the initial value of i is 0, the do while loop still executes the loop body 5 times because they execute the loop body first and then check the loop condition.

In general, a while loop is good for checking a condition before looping, whereas a do while loop is good for executing the body of the loop at least once regardless of whether the condition is true or not.

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/131599294