C-Language loop control statements

Source: Internet
Author: User

First, what is a cycle?

Executes a code snippet repeatedly when the condition is met

What are the conditions that make up a cycle?

1) Conditions for circular exit

2) loop body (repeated code snippet)

3) Let the loop normal exit condition meet the Code

What kinds of loops are there in C language?

While loop

Do...while Cycle

For loop

Two, 1, while loop

Format:

while (expression) {

Statement Block 1;

}

2. Use

int i=0;

while (i<5) {

printf ("%d\n", I);

i++; 1 2

}

3, Trap while dead loop (loop condition is always true) problem

int a=1;

while (a) {

printf ("xx\t");

}

4, while loop to use the attention point:

1) while (not 0 of the number) dead loop

2) while (3==num)

3) can omit curly braces (while loop body only a sentence)

Special note:1> after the IF, while and for statements, if followed by a line of statements, the same plus {}

The difference between the 2>do while and while is that the while is to judge the condition after the execution of the statement block, do is executed first after the judgment, so when the condition is false, while performing 0 loop body Dowhile will execute 1 cycles of the body

Third, for loop use

1,for Loop format

The first form of the 1>for cycle

for (int i=n;i>=1; i--) {

Ji=ji*i;

}

2>, second form: omit expression 1

int m=n;

for (; n>=1;n--) {

Ji *= N;

}

3>, third form: ellipsis expression 1 and expression 3

int m=n;

for (; n>=1;) {

Ji*=n;

n--;

}

4>, for (;;) {} This is a dead loop

2,for Loop Nesting issues:

For loop nesting the output of the first level control row, the second row controls the output of the column

Four: Break and continue use

Break is used only in the loop structure to indicate a stop loop (in a multi-layered loop, a break statement jumps only one layer out)

The Continue is used only in the loop structure, indicating that the loop is closed and the next loop is resumed.

C-Language loop control statements

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.