Cyclic structure For,while,do. While

Source: Internet
Author: User

Loop structure

Overview of the circular structure: when a given condition is established, the procedure is executed repeatedly until the condition is not established.

Looping structure: The loop executes the same block of code again and again

Using the While statement

Use the Do-while statement

Using the For statement

Second, the composition of the cyclic structure

1) Cyclic control conditions

The main basis of the circular exit, to control the loop when the end of the exit

2) Circulation body

A code block that repeats during a loop

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

Can let the loop condition be false basis, otherwise exit the loop

While loop structure Introduction and format

1. The format of the while loop

while (conditional expression) {

Statement 1;

Statement 2;

。。。

}

If the expression is true, the statement is executed

0 (FALSE)

Statement

Loop structure: When the condition is met, the program repeats the code block.

Conditions constituting the cyclic structure: 1) cyclic control condition 2) Loop Body 3) Let the loop control condition be false control

#include "stdio.h"

void Main ()

{

int i=1;

Cyclic control conditions

while (i<=10) {

Loop body

printf ("hello\n");

Allows the loop control condition to be false, thus not causing a dead loop

i=i+1;

}

}

2. The trap of the while loop

Dead loop: Always executes code in the loop body, does not exit

while (condition) condition is always true

Exercise: Calculating the 1+2+3+......+100 and

#include <stdio.h>

void Main ()

{

Define the variable i and sum.

int i=1,sum=0;

while (i<=100) {

With the value of sum plus the value of I

Sum=sum+i;

I's self-increment

i++;

}

printf ("1+2+3+...+100=%d\n", sum);

return 0;

}

3.

1) Dead loop: Any value is true or False

while (-1) Dead loop

2) When a variable is associated with a constant = = or! = When the constants are usually written in front

int num=3;

while (3==num) {}

3) If there is only one statement after the while, it can omit the curly braces

1, empty statement it is a statement

2. The semicolon cannot be written directly after the while parenthesis

4) Scope: variables defined in internal code blocks cannot be accessed in the outside code

5) Scope disorder

while (1<3)

int num=5

Do-while Circular structure Introduction and format

1. Do-while Cycle Structure

do{

Statement

Statement 1;

Statement 2;

An expression

}while (conditions);

0 (FALSE)

The difference from the while

When the condition is met, then the loop body is executed.

Until loop: The loop body is executed first, then the condition is judged until the condition is false.

while Compare with Do-while

#include "stdio.h"

void Main ()

{

int i=5;

while (i<=3) {

printf ("While:%d times I love you \ n", i);

i++;

}

do{

printf ("Do-while:%d times I love you \ n", i);

i++;

}while (i<=3);

return 0;

}

1) when the loop control condition is first judged not to be false: while and Do-while execute the same number of times

2) when the loop control condition is first judged false: while the loop body executes 0 times, Do-while executes 1 times

**for introduction and format of circular structure **

for (expression 1; expression 2; expression 3) {statement;}

0 (FALSE)

Not 0 (true)

Execution procedure: 1) First solve the expression 1.

2) to solve the expression 2, if its value is true, then execute the inline statement specified in the FOR statement, and then perform step (3), if "false", then the end loop goes to step (5).

3) Solving the expression 3

4) Proceed back to step (2) above

5) End of loop, execute statement below for statement

/* Loop Print I love you 10 times */

#include "stdio.h"

void Main ()

{

Cyclic control conditions: i<10

Loop body: printf ("I love you for%d Times", i+1);

The ability to control a proud sword for a fake operation: i+1

int i=0;

for (i;i<10;i++) {

printf ("%d times I Love You", i+1);

}

}

Note: Initialization statements are only executed once

Expression omitted (three expressions can be omitted)

for (;;) Statement is equivalent to while (1)

Nesting of loops

Several forms of legality

(1) while () {

while () {}

}

(2) do{

Do{}while ()

}while ()

(3) for (;;) {

for (;;) {}

}

(4) while () {

Do{}while ();

}

(5) for (;;) {

while () {}

}

(6) do{

for (;;) {}

}while ();

Comparison of several cycles

1. In the while loop and the Do...while loop, only the loop condition is specified within the parentheses after the while, so that in order for the loop to end normally, the loop body should contain statements (such as i++ or i=i+1, etc.) that tend to end the loop.

2. The For loop can include an action that tends to end the loop in Expression 3, or even all the actions in the loop body can be placed in Expression 3. So the For statement is more powerful and can be accomplished with a while loop.

3. When looping with while and do...while, the operation of the loop variable initialization is done before the while and Do...while statements. The For statement can implement the initialization of the loop variable in expression 1

Whlie's N-S flowchart

For-N-S flowchart

Do...while's N-S flowchart

Cyclic structure For,while,do. While

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.