WY C Language Introduction (4)

Source: Internet
Author: User

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)

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.