"C-Cycle structure"

Source: Internet
Author: User

The C language provides a variety of looping statements that can form a variety of different forms of loop structure:

    • The loop is formed by the Goto statement and the IF statement;
    • Use the while statement;
    • Use the Do-while statement;
    • with a for statement;

One, goto statement

A goto statement is an unconditional transfer statement similar to a goto statement in BASIC. The goto statement is used in the following format:
Goto statement label;

Where the label is a valid identifier, this identifier, together with a ":" appears somewhere inside the function, after the goto statement is executed, the program jumps to the label and executes the subsequent statement.

The goto statement is usually not used, mainly because it will make the program level unclear, and difficult to read, but in the multi-layered nesting exit, with a goto statement is more reasonable.

Second, while

The general form of a while statement is:
while (expression) statement
Where the expression is a loop condition and the statement is a loop body.
The semantics of the while statement is to execute the Loop body statement when the value of the expression is evaluated, and the values are true (not 0).

Exercise: Count the number of characters entered in a line from the keyboard.

  1. #include <stdio.h>
  2. int main(void){
  3. int n=0;
  4. printf("Input a string:\ n");
  5. while (getchar()! ='\ n') n+ +;
  6. printf("%d", n);
  7. return 0;
  8. }

Third, Do...while

The general form of the Do-while statement is:
Do
Statement
while (expression);
This loop differs from the while loop in that it executes the statements in the loop first, then determines whether the expression is true, resumes the loop if true, and terminates the loop if it is false. Therefore, the Do-while loop executes at least one loop statement.

Iv. for

In the C language, the For statement uses the most flexible, and it can completely replace the while statement. The general form of it is:
for (expression 1; expression 2; expression 3) statement
It performs the following procedures:

    1. The expression 1 is solved first.
    2. Solve Expression 2, if its value is true (not 0), execute the inline statement specified in the FOR statement, and then perform step 3rd below), and if its value is False (0), end the loop and go to step 5th).
    3. Solve an expression 3.
    4. Back to the 2nd above).
    5. The loop ends with the execution of a statement below the for statement.

Attention:
1) "Expression 1 (loop variable assignment initial value)" In the For Loop, "Expression 2 (Loop condition)" and "Expression 3 (loop variable increment)" Are all selections, that is, you can default, but the semicolon (;) cannot be defaulted.

2) omitted "Expression 1 (the cyclic variable assigned initial value)", indicating that the loop control variable is not assigned an initial value.

3) "Expression 2 (cyclic condition)" is omitted, then it becomes a dead loop when no other processing is done.

4) Omit "Expression 3 (increment of loop variable)", then do not operate the loop control variable, you can add the statement that modifies the loop control variable in the statement body.

5) "Expression 1 (cyclic variable assignment initial value)" and "Expression 3 (loop variable increment)" are omitted.

6) 3 expressions can be omitted. For example:
for (;;) Statement
Equivalent:
while (1) statement

7) Expression 1 can be an assignment expression that sets the initial value of a loop variable, or it can be another expression.

8) expression 1 and expression 3 can be a simple expression or a comma-expression.

9) Expression 2 is generally a relational expression or a logical expression, but also a numeric expression or a character expression, as long as its value is nonzero, the loop body is executed.

V. Break out of the loop

Break statements are typically used in loop statements and switch statements. When break is used in switch statements, the program can jump out of a switch and execute a switch statement;

When the break statement is used in a do-while, for, and while Loop statement, the program terminates the loop and executes the statement following the loop, usually the break statement is always associated with the IF statement, which jumps out of the loop when the condition is met.

Vi. continue end of the cycle

The Ontinue statement acts by skipping the remaining statements in the loop body and forcing the next loop to be executed. The continue statement is used only in the loop body of for, while, Do-while, and often with an if condition statement to speed up the loop.

Exercise: Find all primes between 100 and 200.

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main(void){
  4. int M, I, K, n=0;
  5. For(M=101; M<=; M=m+2){
  6. K=sqrt(M); //M- Radical
  7. For(i=2; I<=k; I+ +)
  8. If(M%i= =0) break ;
  9. If(i>=k+1){
  10. printf("%d\ n", M);
  11. n=n+1;
  12. }
  13. }
  14. printf("\ n");
  15. return 0;
  16. }

Exercise 2: Use the following formula to find π:

Analysis: First, the denominator of an item with a positive coefficient is 4n-3 (n is the number of items for a positive item), the denominator of the item with negative numbers is 4n-1 (n is the number of items of a negative item), that is, the denominator is 1, 3, 5, 7 ... the odd series, the denominator of the nth item is 2n-1 10000-1.

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main(void)
  4. {
  5. Double P=0, J=1;
  6. int i;
  7. for (i=1; I<10000; I+ + ) //Here I is the number of items
  8. {
  9. J=Pow(-1.0, I+1)/(2*i-1); //pow (x, y ) to the Y power of x
  10. P+=j;
  11. printf("%lf\ n",4*p); //Output value of each item
  12. }
  13. printf("%lf\ n",4*p); //Output final pi value
  14. return 0;
  15. }

"C-Cycle structure"

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.