1. Simple to use
while (condition)
{
Statement 1;
Statement 2;
....
}
If the condition is true, the statement in the loop body is executed (the "loop Body" is the contents of the curly brace {} after the while). Then judge the condition again and repeat the process until the condition is not set to end the while loop
While loop features: If the condition in the while is not in the beginning, then the statement in the loop body will never be executed.
You can omit the curly braces {}, but only the first statement that follows the while is affected. Omitting curly braces is not recommended.
Statement 1;
2. Code
1#include <stdio.h>2 3 /*4 if (condition)5 {6 7 }8 9 While (condition)Ten { One Loop Body A } - - Operating principle the 1. If the initial condition is not established, the loop body will never be executed - 2. If the condition is set up, a loop body is executed, the execution is completed, and the condition is determined again . - - Break + directly ends the entire while loop - + Continue A end the current loop body and go to the next loop body execution at - */ - - intMain () - { - //1. First identify the actions that need to be performed repeatedly in - //2. Re-determination of constraints to + //defines the number of times a variable record is made - intCount =0; the * /* $ While (COUNT<50)Panax Notoginseng { - ++count; the + if (count%10! = 0) A { the printf ("Do%d push-ups \ n", count); + } - }*/ $ $ /* - While (COUNT<50) - { the ++count; - Wuyi if (count%10 = = 0) the { - //end this cycle directly and enter the next cycle Wu continue; - } About $ printf ("Do%d push-ups \ n", count); - }*/ - - while(Count < -) A { +++count; the -printf"do the first%d push-ups \ n", count); $ the if(Count = = -) the { the Break; the } - } in the the return 0; About}
Practice
1 /*2 prompts the user to enter a positive integer n, computes the 1+2+3+...+n and3 */4 5#include <stdio.h>6 7 intMain ()8 {9 //1. Prompt for inputTenprintf"Please enter a positive integer: \ n"); One A //2. Receive input - //define variables to hold the integer entered by the user - intN; thescanf"%d", &n); - - if(n<=0) - { +printf"illegal input \ n"); - return 0; + } A at //3. Calculation - //(1 + N) * N/2; - //define variable saving and - intsum =0; - intNumber =0;//the default value to be added - in while(Number <N) - { tonumber++; +sum + = number;//Accumulate - } the *printf"%d\n", sum); $ Panax Notoginseng return 0; -}
1 /*2 Title: Calculates the number of multiples of all 3 in a 1~1003 */4 5#include <stdio.h>6 7 intMain ()8 {9 //record the number of multiples of 3Ten intCount =0; One A //record the value of the current check - intNumber =0; - the while(Number < -) - { -number++; - + //Description number is a multiple of 3 - if(number%3==0) + { Acount++; at } - } - -printf"number of multiples of 3 in 1~100:%d\n", count); -}
3. Attention Points
1#include <stdio.h>2 3 intMain ()4 {5 /*6 While (Ten)7 {8 printf ("haha \ n");9 }*/Ten One /* A int a = ten; - //while (a>0); dead Loop - While (a>0) the { - a--; - printf ("Hahaha \ n"); - }*/ + - //The simplest cycle of death + //while (1); A at return 0; -}
"Learning note", "C language" cyclic structure-while