C language-cyclic structure and break, continue, and breakcontinue

Source: Internet
Author: User

C language-cyclic structure and break, continue, and breakcontinue
Loop Structure

--------------------------

-- 1 -- Structure Loop1.1 while LOOP1.2 do... While Loop1.3 for Loop-- 2 -- break and continue2.1 break keywords2.2 continue keywords

--------------------------

 

 

[Written at the beginning :]

Cycle in life:


 

Cycle in C language:

Loop structure is a very important structure in the program. It is characterized by repeatedly executing a program segment when a given condition is set until the condition is invalid.

The C language provides multiple loop statements:

1) The goto statement and if constitute a loop (abandoned)

2) while statement

3) do-while statement

4) for statement

The following conditions constitute a loop:

Generally, the following components are required for a loop:

1) cycle control conditions

Main Basis for loop exit

2) loop body

A code block that is repeatedly executed during a loop.

3) statements that can end a loop (increment, decrease, true, false, etc)

 

-- 1 -- Structure cycle 1.1 while LOOP

1) syntax format

While (condition)

{

Statement 1;

Statement 2;

...

Statement n;

}

 

2) while loop execution features

The program body is always executed when the condition is set.

 

3) Example

Enter n on the keyboard to calculate 1 + 2 + 3... + N value.

// define variables
     int i = 1, n = 0, sum = 0;

     // Accept keyboard input n and assign it to variable n
     printf ("Please enter the accumulation limit: \ n");
     scanf ("% d", & n);

     // while loop condition is less than n
     while (i <= n) {
         sum + = i; // accumulate
         i ++;
     }

     printf ("The value accumulated from 1 to% d is:% d \ n", n, sum);

 

4) Supplement

Scanf principle: If the input buffer is not empty, the user will not input the content again, but will continue to obtain it directly in the buffer.

The simplest endless loop: while (1 );

1.2 do... While Loop

1) syntax format

Do {

Statement 1;

Statement 2;

...

Statement n;

} While (condition );

 

2) do... While loop features:

Whether or not the while condition is true, the loop body is executed at least once.

 

3) while and dowhile have different loop types.

A while LOOP is a type-1 loop. when conditions are met, the loop body is executed;

Do... The while loop is a type loop. The loop body is executed first, and then the condition is determined until the condition ends when the condition is false.

 

4) do... while example

 

int x = -3;
     do {
         printf ("do ... while \ n");
         x--;
     } while (x> 0); //do...while loop The loop body will execute once regardless of whether the condition is met

 

1.3 for Loop

1) for syntax format

In C Language, The for statement is the most flexible to use, and can replace the while statement.

General format:

For (expression 1; expression 2; expression 3 ){

Statement block;

}

 

2) for Loop Execution Process


Execution sequence:

1) Evaluate expression 1 first and initialize I

2) solution expression 2. If the value is true (not 0), execute the embedded statement specified in the for statement --> Expression 3.

3) Execution expression 4

--> After completing the loop, perform the second step, and then proceed to the next step.

Note: Step 1) execute only once in the entire Loop

 

3) Other forms of for Loop

1) expression 1 is omitted

 int i = 0;
    for (; i < 5; i++){
        printf("%d", i);
    }

 

2) Expressions 1 and 3 are omitted

 int i = 0;
    for (; i < 5;){
        
        printf("%d", i);
        
        i++;
        
    }

 

3) Expressions 1, 2, and 3 are omitted.

 int i = 0;

    for(;;){
        
        if (i < 5){
            
            printf("%d", i);
            
            i++;
            
        } else{
            
            break;
            
        }
        
    }

 

4) The simplest for Loop

  for (; ; );

 

5) nested use of for Loop

Print Image

*****

*****

*****

*****

*****

 

Analysis:

Print a row in the inner loop, and print 5 rows in the outer loop five times.

for (int j = 0; j <5; j ++) {
         for (int i = 0; i <5; i ++) {// inner print line
             printf ("* \ t");
         }
         printf ("\ n");
     }

 

 

-- 2 -- break and continue2.1 break keywords

Both break and continue can be used in a loop to exit or end a loop.

Break usage

Break statements are usually used in loop and switch statements.

 

Note:

1) break does not work for if-else.

2) break is used to exit the current loop (end the current loop ).

3) break affects the current cycle, and the external layer loop does not work.

 

Example

Calculate the area of the circle from r = 1 to r = 10 until the area is greater than 100

Analysis:

Area of the Circle = 3.14 * r;

// Calculate the area of the circle when r = 1 to r = 10, until the area is greater than 100
     for (int r = 1; r <= 10; r ++) {
         double area = 3.14 * r * r;
         if (area> 100) {
             break; // The area is greater than 100 to end the loop
         }
        
         printf ("area =% .2f \ n", area);
     }

 

2.2 continue keywords

Continue is used to end this loop and continue the next loop

Continue is used only in loop bodies such as for, while, and do-while. It is often used together with the if statement to accelerate the loop.

 

Example

Output the number that cannot be divisible by 3 between and.

Analysis:

The cycle ends when it is divided by 3 and enters the next time.

// Output numbers between 100-200 that are not divisible by 3.
     for (int i = 100; i <= 200; i ++) {
        
         if (i% 3 == 0) {
             continue; // divide out of this loop
         }
         printf ("i =% d \ n", i);
     } 

 

Summary:

We can see from the above:

The loop ends after the break jumps out of the loop, and the next loop continues after the continue jumps out of the loop. This is the biggest difference.

 

 

[Written at the end :]

"It's raining, just give a few answers. My favorite one is listening to the rain ...』


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.