Latest web development tutorials

C while loop

C cycle C cycle

As long as the given condition is true, C languagewhile loop will repeat a target statement.

grammar

C languagewhile loop syntax:

while(condition)
{
   statement(s);
}

Here, statement (s) may be a single statement, it can also be a block composed of a few statements.condition can be any expression, when an arbitrary non-zero values are true. Execution of the loop when the condition is true.

When the condition is false, program flow will continue with the next statement followed the loop.

flow chart

C while loop

Here, the key pointswhileloop is a loop may not execute. When the condition is false, the loop body will skip directly to the next statement immediately while loop.

Examples

#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 results:

a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 15
a 的值: 16
a 的值: 17
a 的值: 18
a 的值: 19

C cycle C cycle