Home> Common Problem> body text

Usage of while statement in c language

藏色散人
Release: 2021-04-20 10:00:38
Original
80146 people have browsed it

The usage syntax of while statement in c language is such as "while(condition) {statement(s);}". The statement can be a single statement or a statement block, and its condition can be any expression. true is any non-zero value and the loop iterates when the condition is true.

Usage of while statement in c language

The operating environment of this article: Windows 7 system, version C11, Dell G3 computer.

The while loop in C programming will execute the target statement repeatedly as long as the given condition is true.

The syntax of while loop in c language

while(condition) { statement(s); }
Copy after login

Here, the statement can be a single statement or a block of statements. The condition can be any expression, and true is any non-zero value. The loop iterates when the condition is true.

When the condition is false, program control is passed to the line immediately following the loop.

Flowchart

Usage of while statement in c language

The key point to note here is that the while loop may not execute at all. When the condition test result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Usage example:

#include  int main () { int a = 10; while( a < 20 ) { printf("value of a: %d\n", a); a++; } return 0;}
Copy after login

Output:

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
Copy after login

Recommended related C language video tutorial: "C Tutorial

The above is the detailed content of Usage of while statement in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!