Dark Horse Programmer---C-base 4 "loop structure" "While loop" "Do While "for loop" "Break keyword" "Continue keyword"

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

"Loop Structure"

1 , circular structure Introduction:

Loops are executing the same block of code again and again

2. Several conditions constituting the cyclic structure:

cyclic control conditions;

Loop Body: code block

 The statement that can let the loop end (increment, decrement, true, false); exit loop

"While Loop"

1. The format of the while loop :

When the condition is satisfied, the loop body is executed again;

while (conditional expression)

{

Statement 1;

Statement 2

······

}

Xxx

If the expression is a true execution code block, the false is the end;

if the condition is false at the beginning, it will not be executed;

2. The trap of the while loop :

Dead loop: Is the execution of the Loop body code will not exit;

while (condition);

#include <stdlib.h>

Arc4random_uniform (n-m+1) a random number between +m;n and M;

3, the use of attention;

dead Loop: Any value is true or false;

while (1);

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

If only one statement can omit the curly brace after the while

The variables defined in the internal code block cannot be accessed in the outer code block;

Scope disorder

"Do While

1. Format:

Until the loop, the loop body is executed first and then judged until the condition is false on the end;

Do{

Statement 1

Statement 2

·····

} while (condition);

2. Usage:

The loop body is executed first, then the condition is determined, and the loop body is executed again;

Regardless of whether the condition is established, the loop body will be executed once;

3, do Comparison of while and while:

While judgment is performed again;

Do... While a second judgment is performed first;

&NBSP;

While

N

0

• do• while

N

1

"For Loop"

1. For loop syntax format:

It can be used not only when the number of cycles has been determined, but also in cases where the cycle times are uncertain and only the loop end condition is given, which can completely replace the while.

2. General format :

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

Circulation body;

}

1 intMainintargcConst Char*argv[]) {2//Insert code here ...3     //There are three conditions for forming a cycle4     //Control conditions i<10;5     //the Loop body printf ("love%d\n", i+1);6     //can let the control condition be false operation i++;7 for(intI=0; i<Ten; i++) {8printf"love%d\n", i+1);9     }Tenprintf"Hello, world!\n."); Onereturn 0; A}

1), first give the loop variable initialization int i=0;

2), judging conditions i<10;

3), perform the Loop body printf ("love%d\n", i+1);

4), the implementation of control conditions i++;

5), and then loop to perform 2 judging conditions, cyclic execution;

Note that the initialization statement is executed only once,

When the condition is not satisfied, the cycle ends;

3. Other formats for the For loop:

Expression omitted (three expressions can be omitted)

The initialization statement can be written out, omitting the expression 1;

The control condition can omit the expression 3;

You can omit the expression 2, judge the statement, and replace it with an if

, the number must not be omitted!

4, for loop nesting:

1  for (;;) {23for (;;) {45  }67 }       

"Break keyword"

1. Break Statement usage:

Can be used in the loop, the expression jumps out of the loop, the loop ends, can be used in the switch inside, the end of the statement, the subsequent do not execute;

If there is no break then there will be case penetrating;

In a multilayer loop, break just jumps out of a layer of loops, and the outer layers are executed;

"Continue keyword"

1. Continue statement:

End this cycle and continue the next cycle

It is used in for,while and other loops, often used together with if, to accelerate the cycle;

 

Dark Horse Programmer---C-base 4 "loop structure" "While loop" "Do While "for loop" "Break keyword" "Continue keyword"

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.