. NET Foundation Step by step scene [Loop, logical statement block]

Source: Internet
Author: User

Loop, logical statement block

Long time not to write a blog, broken more days, from Friday to today, from Beijing to Shanghai, spanning 1213.0 kilometers, from a familiar city to a strange city, fortunately I have a better adaptability, and thanks to the small partners reception, everything is not a matter, good, into the topic:

This article is also the. NET basic part, the main summary of the cycle, judge:

Cycle:

for Loops

Grammar:

for (expression 1; expression 2; expression 3)

{

Circulation body;

}

Expression 1 is generally declared as a loop variable, the number of times the loop is recorded (int i=0;)

Expression 2 is generally a cyclic condition (i<10)

Expression 3 is usually the code that changes the loop condition so that the loop condition ceases to be true one day (i++).

execution Procedure : The program first executes expression 1, declares a loop variable used to record the number of cycles,

The expression 2 is then executed to determine if the loop condition is true, and if the expression 2 returns the result

The loop body is executed. When the loop body is executed, the expression 3 is executed, and then the expression 2 continues to determine whether the loop condition is true, and if so, the loop body is executed, and if not, the for loop is popped up.

Case:

Note: As in the case of "Narcissus number" do not understand Baidu itself.

foreach Loop:

Grammar:

foreach (data type identifier in expression)

{

Loop body

}

foreach (type identifier in expression)

{

Statement

}

which

Type

The type of identifier.

Identifier

Represents an iteration variable for a collection element. If the iteration variable is a value type, the read-only variable that cannot be modified is also valid.

Expression

An object collection or array expression. The type of the collection element must be convertible to the identifier type. Do not use an expression that evaluates to NULL.

Instead, the type that implements the IEnumerable or declares the GetEnumerator method should be computed. In the latter case, GetEnumerator should either return the type that implements IEnumerator, or declare all the methods defined in IEnumerator.

Statement

The embedded statement to execute.

Case:

Note: An array of type int is declared in the case. An array, a collection, is described in a later chapter.

while Loop :

Grammar:

while (loop condition)

{

Circulation body;

}

Execution procedure: When the program runs to the while, first determine if the loop condition in the parentheses with the while is true,

If it is true, that is to say, return one, then execute the loop body, execute the loop body again, and return to

The loop condition is judged, if it is still true, the loop body is continued, and if it is not, it jumps out of the while loop.

In the while loop, there is always a line of code that can change the cycle condition, so that one day no longer holds,

If there is not a single line of code that can change the loop condition, that is, the loop condition is always set, and we call this loop

Called the cycle of death.

The simplest of the most commonly used dead loops:

while (true)

{

}

features : First judgment, then execution, it is possible that the cycle is not executed.

Case:

Do-while Loops

Grammar:

Do

{

Circulation body;

}while (cyclic conditions);

Execution procedure: The program first executes the loop body in do, after the execution completes, to judge the do-while circulation condition,

If this is true, the loop body in do is continued, and if not, the Do-while loop is popped out.

Features: First cycle, then judge, at least to perform the loop body.

Case:

Nested loops : The outer loop is used to control the number of rows in the output, and the inner loop is used to control the number of columns in the output

for (expression 1; expression 2; expression 3)

{

for (expression 1; expression 2; expression 3)

{

Circulation body;

}

}

For example, 99 multiplication table:

Logical statement BLOCK:

if Statement :

Grammar:

if (judging condition)

{

The code to execute;

}

Criteria: Typically a relationship expression or a value of type bool.

Execution process: The program runs to if, first of all to determine if the parentheses in the condition of the judgment,

If the condition is true, then the code in the curly braces with if is executed,

If the judging condition is not true, it returns a false. Skips the IF structure and continues down execution.

If structure features: First judge, then execute

Case:

If-else

Grammar:

if (judging condition)

{

Code of execution;

}

Else

{

Code to execute

}

Execution procedure: The execution of the program to if, first of all to determine if the conditions of the parentheses in the if the condition is established,

If it is true, the code in the curly braces with the if is returned.

When execution is complete, jump out of the if-else structure.

If the judging condition in the parentheses with the if is not true, it returns a false,

Skip the If statement, execute the statement in the curly braces with else, and after the execution is done, jump out of the if-else structure.

If-else Features: First judge, then execute, at least to execute a code.

For two cases of judgment

Note: Else is always paired with the if closest to it

Case:

If else-if

Function: Used to deal with multiple conditions of the interval of judgment.

Grammar:

if (judging condition)

{

The code to execute;

}

else if (judging condition)

{

The code to execute;

}

else if (judging condition)

{

The code to execute;

}

else if (judging condition)

{

The code to execute;

}

........

Else

{

The code to execute;

}

The procedure first determines the condition of the parentheses in the first if, or returns a true if the condition is set

Executes the code in the curly braces with the IF, and immediately jumps out of the if ELSE-IF structure after execution is complete.

If the first if the judging condition is not true, that is, return a false, then continue to judge, in order to judge each if the band

The judging condition, if established, executes the code in the curly braces with the IF, and if not, continues to judge,

If the judging conditions for each if are not valid, see if there is an else in the current if else-if structure.

If there is else, then the code in the else is executed, and if there is no else, the whole if-else if God horse does not.

else can be omitted.

Case:

According to the above you will see if the else-if will cause the code to be very long when the conditions are particularly high, and we will replace it with another:

Switch-case

The judgment used to deal with the fixed value of multiple conditions.

Grammar:

Switch (the value of a variable or an expression)

{

Case value 1: the code to execute;

Break

Case Value 2: the code to execute;

Break

Case Value 3: the code to execute;

Break

..........

Default: The code to execute;

Break

}

Execution procedure: The program executes to switch, first the value of the variable or expression in parentheses is calculated,

Then take this value to match the value followed by each case, and once the match succeeds, it executes

The case takes the code, and after execution finishes, it encounters a break. Jump out of the switch-case structure.

If it does not match the value that is taken with each case. Depends on whether the current switch-case structure exists.

Default, if there is default, the statement in default is executed, and if there is no default, the switch-case structure

Don't do anything.

Case:

Finally, an egg :

Break , continue, return the difference and effect:

First: Break statements are usually used in loop statements and switch statements, and when the break statement is used in a do-while, for, and while Loop statement, the program terminates the loop and executes the statement following the loop, usually the break statement is always associated with the IF statement. That is, it jumps out of the loop when the condition is met. For example: NOTE:
1) The break statement does not work for if-else conditional statements.
2) in a multilayer loop, a break statement jumps only one layer outward.

Second: The function of the continue statement is to skip the remaining statements in the loop and forcibly execute the next loop. The continue statement is used only in the loop body of for, while, Do-while, and often with an if condition statement to speed up the loop. In fact, the continue skips a loop and the following statement, making the next loop.

Third: The return statement returns the value of the function to the keynote function. For example:
The general form of the return statement is:
return expression
or for:
return (expression)

OK, this article is here, and another case to provide only, there is no running results, please handle it yourself. Hope to bring help to beginners, but also hope that the great God can bring us, take us to install force, take us to fly ...

Finally, a small ad: QQ group: . NET Step by Step screen number:590170361 (dabigatran Note: Blog Park to see)

. NET Foundation Step by step scene [Loop, logical statement block]

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.