Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C while Loop

C while Loop

C Loops C Loops

As long as the given condition is true, the C language's while the loop statement repeatedly executes a target statement.

Syntax

In the C language while The syntax of the do...while loop:


while(condition)
{
   statement(s);
}

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

condition can be any expression, and any non-zero value is treated as true. When the condition is true, the loop is executed. When the condition is false, the loop is exited and program flow continues with the statement immediately following the loop.

Flowchart

Here,while The key point of the loop is that it may not execute even once. When the condition is false, the loop body is skipped and execution proceeds directly to the statement immediately after the while loop.

Example

Example

#include

 

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 code above is compiled and executed, it produces the following result:


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

C Loops C Loops