Home >Common Problem >Usage of while statement in c language

Usage of while statement in c language

藏色散人
藏色散人Original
2019-02-22 13:57:0680343browse

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);
}

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 <stdio.h>
 int main () {
  
   int a = 10;

   while( a < 20 ) {
      printf("value of a: %d\n", a);
      a++;
   }
 
   return 0;}

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

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!

Statement:
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