Loop control
4.1 For Loop
1#include <stdio.h>2 3 intMain ()4 {5 inti =0;6 intn =1;7 intFact =1;8printf"factorial calculation, please enter the calculation number \ n");9scanf"%d", &n);Ten One for(i =1; I <= N; i++ ){ AFact *=i; - } - the -printf"the factorial of%d is%d", N, fact); - - return 0; +}
In the first run, the. exe stopped running error, after searching for a search and no solution, thought it was a system failure, and then according to the recommendations and experimental debugging functions, each step of debugging, after entering the value of the statement after the SIGSEGV hint, after careful observation, only to find that the original is careless in the statement did not enter & Assignment, which causes the program to fail, remember to pay attention to the exact writing of each character, debugging function is very useful, the first experiment debugging function, and harvested a solution to the problem.
Have a fixed number of times with for
Must do once with Do-while
Other cases with while
Break breaks the loop straight end
Continue jump out of the loop, make statements, re-cycle, initial-loop-statement
= = is equal to = is assigned
! Non-&& | | Or
4.2 Loop nesting
Number of prime numbers within 100 filter
1#include <stdio.h>2 3 intMain ()4 {5 intx =2;6 inti =2;7 8 9 Ten for(x =2; X < -; X + +){ One intIsPrime =1; A - for(i =2; i < x; i++ ){ - if(x% i = =0){ theIsPrime =0; - Break; - } - } + if(IsPrime = =1 ){ -printf"%d", x); + } A at } -printf"\ n"); - return 0; -}
1) Tradition: The relay break jumps out of the loop control
2) Alternative (less): Within the inner cycle of the Goto out
Ruturn above write out:
4.3 Cycle calculation
Calculating sum plus minus plus minus fractions note floating point number available 1.0*
Plus or minus can set the variable sign every round of sign =-sign
WY C Language Introduction (4)