C while loop
As long as the given condition is true, in C languagewhilethe loop statement will repeatedly execute a target statement.
Syntax
In C languagewhileThe syntax of loops:
while(condition)
{
statement(s);
}
Here,statement(s)It can be a single statement or a block of code composed of several statements.
conditionIt can be any expression, and any non-zero value is true. When the condition is true, the loop is executed. When the condition is false, the loop is exited, and the program flow will continue with the next statement immediately following the loop.
Flowchart
Here,whileA key point of the loop is that the loop may not be executed even once. When the condition is false, the loop body is skipped, and the next statement immediately following the while loop is executed directly.
Example
Example
int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of 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 的值: 19other extensions