One. Format:
1: while (condition)
2:
3: {
4:
5: loop body
6:
7: }
8:
Two. Operating principle
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.
Three. While common two statements: break and Continue
1: int count = 0;
2:
3: while (count<50)
4:
5: {
6:
7: ++count;
8:
9: if (count%10! = 0)
Ten:
One : {
:
: printf ("Do%d push-ups \ n", Count);
:
: }
:
: }
:
1.break, directly ending the entire while loop
1: int count=0;
2:
3: while (Count < 50)
4:
5: {
6:
7: ++count;
8:
9: printf ("Do%d push-ups \ n", Count);
Ten:
One : If (count = = 20)
:
: {
:
: break;
:
: }
:
: }
:
: return 0;
:
: }
:
2.continue, end the current loop body and go to the next loop body execution
1: int count=0;
2:
3: while (count<50)
4:
5: {
6:
7: ++count;
8:
9: if (count%10 = = 0)
Ten:
One : {
:
: //end this cycle directly and enter the next cycle
:
: continue;
:
: }
:
: printf ("Do%d push-ups \ n", Count);
:
: }
:
Four. The common dead loop of the while statement
1. The simplest cycle of death
1: while (1);
2. Never-ending "hahaha"
1: while (10)
2:
3: {
4:
5: printf ("haha \ n");
6:
7: }
8:
Five. While application small example
1: /*
2:
3: Prompts the user to enter a positive integer n, computes the 1+2+3+...+n and
4:
5: * /
6:
7: #include <stdio.h>
8:
9: int main ()
Ten:
One : {
:
//1. Prompt for input
:
: printf ("Please enter a positive integer: \ n");
:
+ : //2. Receive input
:
+ : //define variable save user input integer
:
: int n;
:
At : scanf ("%d", &n);
:
: if (n<=0)
£ º
: {
:
: printf ("illegal input \ n");
:
: return 0;
:
: }
:
: //3. Calculation
£ º
Panax notoginseng ://(1 + N) * N/2;
:
: //define variable saving and
Max :
In : int sum = 0;
£ º
Number : int//value added by default
:
: while (number < n)
:
: {
:
£ number++;
:
Wuyi: //cumulative
:
: }
Wu :
: printf ("%d\n", sum);
:
£ º return 0;
:
: }
:
Dark Horse programmer--"Dark Horse video Notes" The use of the C language Foundation while