C language-cyclic structure and break, continue

Source: Internet
Author: User

Loop structure

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

--1--structureLoops1.1 While Loop1.2 Do..... While loop 1.3 for Loop--2--break and Continue2.1The break keyword2.2ContinueKey Words

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

"Write at the beginning:"

The cycle of life:

Loops in the C language:

The cyclic structure is a very important structure in the program. The characteristic is that when a given condition is established, a program segment is executed repeatedly until the condition is not established.

There are a number of looping statements in the C language:

1) goto statement and if form Loop ( has been discarded )

2)while statement

3)Do-while statement

4)for statement

Several conditions that constitute a cycle:

In general, a cycle requires the following components

1) cyclic control conditions

Circular Exit main basis

2) Circulation body

A block of code that repeats during the loop.

3) statements that allow the loop to end (increment, decrement, true, false, etc.)

--1--Structure Loop 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 established.

3) Example

Enter a number n from the keyboard to calculate the value of the 1+2+3...+n.

    //Defining Variables    inti =1, n =0, sum =0; //accepts the keyboard input n and assigns a value to the variable nprintf"Please enter an accumulation limit: \ n"); scanf ("%d",&N); //While loop condition is less than n     while(I <=n) {sum+ = i;//Accumulatei++; } 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 is not allowed to enter the content again, and the direct flush buffer continues to acquire.

The simplest dead loop:while (1);

1.2 Do..... While loop

1) syntax format

do{

Statement 1;

Statement 2;

...

Statement N;

}while ( condition);

2) features of the D o...whi le loop:

The loop body is executed at least once, regardless of whether the condition in the while is true.

3) while Unlike the dowhile cycle type

While loop is a type of loop, then executes the loop body when the condition is satisfied;

Do... the while loop is until the loop, which executes the loop body before judging the condition until the condition is false.

4) Do...while Example

    int x =-3;      Do {        printf ("do...while\n");        X--;    }  while 0 // Do...while Loop The loop body executes once, regardless of whether the condition is satisfied

1.3 for Loop

1) For syntax format

the use of the For statement in the C language is the most flexible and can completely replace the while statement

General form:

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

statement block;

}

2) forLoop execution procedure

Execution order:

1) First expression 1, to I initialization

2) solve expression 2, if its value is true (not 0), execute the inline statement specified in the FOR statement---expression 3

3) Execution Expression 4

--cycle through the second step, then down

Note: Step 1) executes only once in the entire loop

3) F other forms of the or loop

1) expression 1 omitted

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

2) Expression 1,3 omitted

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

3) Expressions 1,2,3 omitted

    int 0 ;      for (;;) {                if5) {                        printf ("%d", i);                        I++                    ; Else {                        break;                    }            }

4) The simplest for loop

   for (; ; );

5) nested use of For loops

Print graphics

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

Analysis:

Inner Loop print one line, outer loop 5 times print 5 rows

     for (int05; j + +)        {for (int05// The inner layer prints the line            printf ("*\t");        }        printf ("\ n");    }

--2--break and Continue2.1 Break Keywords

Break and continue can all be used in loops to jump out of the loop / end loop

Break usage

Break statements are typically used in loops and switch statements.

Attention:

1) Break hasno effect on if-else .

2) break is used to jump out of the current loop (to end the current loop).

3) break affects the current loop, and the outer layer loop does not work

Example

Calculates the area of the circle at R=1 to r=10 until the area is greater than

Analysis:

Area of Circle = 3.14 * R * r;

    //calculates the area of the circle at R=1 to r=10 until the area is greater than 100     for(intR =1; R <=Ten; r++) {        DoubleArea =3.14* R *R; if(Area > -) {             Break;//area greater than 100 end loop} printf ("Area =%.2f\n", area); }

2.2 ContinueKey Words

Continue used to end this cycle and continue the next cycle

Continue is used only inloop bodies such as for, while, Do-while, and is often used with the IF statement to speed up the loop

Example

The 100-200 can not be divisible by 3 of the number output.

Analysis:

when divisible by 3 ends this cycle into the next

    // output of a number that cannot be divisible by 3 between 100-200     for (int, i++) {                if30) {             Continue// divide out of this loop         }        printf ("i =%d \ n", I);    }

Summarize:

As can be seen from the above:

The break jumps out of the loop and the loop ends, and continue jumps out of the loop and continues the next loop. That's the big difference.

"Write at the end:"

"Rain, tick-tock, childhood favorite, is to listen to the sound of rain ..."

C language-cyclic structure and break, continue

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.