[Study Notes] [C Language] loop structure-while, learning note structure-while

Source: Internet
Author: User

[Study Notes] [C Language] loop structure-while, learning note structure-while
1. Easy to use

While (condition)
{
Statement 1;
Statement 2;
....
}

If the condition is true, the statement in the loop body will be executed ("loop body" is the content in braces {} After while ). Then judge the condition again, repeat the above process, and end the while loop until the condition is not true.

While loop features: if the conditions in while are not true at the beginning, the statements in the loop body will never be executed.

 

The braces {} can be omitted, but only the first statement after the while clause is affected. We do not recommend that you omit braces.

While (condition)
Statement 1;

2. Code
1 # include <stdio. h> 2 3/* 4 if (condition) 5 {6 7} 8 9 while (condition) 10 {11 loop body 12} 13 14 14 Operating Principle 15 1. if the initial condition is not true, the loop body 16 will never be executed. 2. if the condition is true, a loop body is executed, and the execution is completed. Then, the system checks whether the condition is true ...... 17 18 break19 ends the entire while LOOP 20 21 continue22 ends the current loop body and enters the next loop body execution 23 24 */25 26 int main () 27 {28 // 1. first confirm the operation to be repeated 29 30 // 2. then determine the constraints 31 32 // define the number of times a variable record is made 33 int count = 0; 34 35/* 36 while (count <50) 37 {38 + + count; 39 40 if (count % 10! = 0) 41 {42 printf ("do % d push-up \ n", count); 43} 44} */45 46/* 47 while (count <50) 48 {49 + + count; 50 51 if (count % 10 = 0) 52 {53 // directly end this cycle and enter the next cycle 54 continue; 55} 56 57 printf ("do % d push-up \ n", count); 58} */59 60 while (count <50) 61 {62 + + count; 63 64 printf ("do % d push-ups \ n", count); 65 66 if (count = 20) 67 {68 break; 69} 70} 71 72 73 return 0; 74}

Exercise

1/* 2 prompt the user to enter a positive integer n, calculate 1 + 2 + 3 +... + N and 3 */4 5 # include <stdio. h> 6 7 int main () 8 {9 // 1. prompt to enter 10 printf ("Please enter a positive integer: \ n"); 11 12 // 2. receive input 13 // define variable save user input integer 14 int n; 15 scanf ("% d", & n); 16 17 if (n <= 0) 18 {19 printf ("illegal input \ n"); 20 return 0; 21} 22 23 // 3. calculate 24 // (1 + n) * n/2; 25 // define the Save variable and 26 int sum = 0; 27 int number = 0; // The default value 28 29 while (number <n) 30 {31 number ++; 32 sum + = number; // accumulate 33} 34 35 printf ("% d \ n", sum); 36 37 return 0; 38}
1/* 2 Question: computing 1 ~ Number of multiples of all 3 in 100 3 */4 5 # include <stdio. h> 6 7 int main () 8 {9 // number of multiples of record 3 10 int count = 0; 11 12 // record the value currently checked 13 int number = 0; 14 15 while (number <100) 16 {17 number ++; 18 19 // It indicates that number is a multiple of 3 20 if (number % 3 = 0) 21 {22 count ++; 23} 24} 25 26 printf ("1 ~ Number of multiples of 3 in 100: % d \ n ", count); 27}

3. Notes

1 # include <stdio. h> 2 3 int main () 4 {5/* 6 while (10) 7 {8 printf ("HAHAHA \ n "); 9} */10 11/* 12 int a = 10; 13 // while (a> 0); infinite loop 14 while (a> 0) 15 {16 --; 17 printf ("HAHAHA \ n"); 18} */19 20 // The simplest endless loop 21 // while (1); 22 23 return 0; 24}

 

 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.