[C language learning notes]: while statement

Why does C language need loop control?

Most C language applications will contain loop structures. Loop structures, sequential structures, and selection structures are the three basic structures of structured programming. They are the basic building blocks of various complex programs, and the problems handled by the program often need to be repeated. deal with.

C language while statement

General form
while (expression) statement

Things to note about while statement in C language

  • The statement is the loop body, which can be a simple statement or a compound statement. The number of times the loop body is executed is controlled by the loop condition. This loop condition is the "expression" in the general form above, also known as the loop condition expression.

  • The while loop can be simply written as, as long as the loop conditional expression is true (that is, the given condition is established), the loop body statement will be executed.

  • The characteristics of while loop are: first judge the conditional expression, and then execute the loop body statement.

C language uses the while statement to find the sum of 1+2+3+...+10

#include<stdio.h>//Header file
int main()//Main function
{   int i=1,sum=0;//Define variable   while(i<11)//Loop condition and   {     sum=sum+i ;//and     i=i+1; //increment   }   printf("%d",sum);//output result   return 0;//function return value is 0 }








Compile and run results:

55
--------------------------------
Process exited after 0.09865 seconds with return value 0
Please press any key to continue. . .

Guess you like

Origin blog.csdn.net/Jiangziyadizi/article/details/129508932