C # examples of judgment and loop statements,

Source: Internet
Author: User

C # examples of judgment and loop statements,
Judgment

A condition is an operation that determines whether the condition is true or not false!

If

Syntax:

If (bool_expression) {statement (s)} // experssion executes else {statement (s)} // experssion as false.
Switch

Syntax:

Switch (experssion) // This type of experssion must be an integer or enumeration type {case (constant-expression): statement (s), break; case (constant-expression ): statement (s), break; case (constant-expression): statement (s), break;} // This can contain any case statement, however, after case, a comparison value and a colon must exist. // when the condition meets the content of a case statement, the command after the case is executed and then the judgment is jumped out.

Note: The switch clause cannot be executed from a switch (case) to the next switch. If the case clause is not empty, it must contain a break statement or a jump statement.

Ternary expression
Name = mineral water? Handsome: not handsome; // The name is mineral water. The output is handsome. No, the output is not handsome.
Loop

I personally think that loop is an upgraded version of judgment in some aspects.

The main cause is that the loop contains judgment. After judgment, the condition is met and the loop is executed. If the condition is not met, the loop is exited!

While

Syntax:

While (condition) // condition can be any expression, which is true if it is any non-zero value. Execute a loop when the condition is true. {Statement (s) // statement (s) can be a separate statement or a code block composed of several statements} // you can perform conditional judgment. If the condition is true, the loop continues, if the condition is false, the loop ends.
Do while

Another version of The while LOOP

Syntax:

Do {statement (s) // execute statement (s)} while (conditoin) // to judge the condition. If the condition is true, the loop continues. If the condition is false, the loop ends.

From the syntax, we can clearly see the difference between the while loop and the Do while loop:

The while loop is executed first;

The Do while loop is executed first and then judged.

For

Upgraded version of the while LOOP

Syntax:

For (expression 1; expression 2; expression 3) // expression 1: Value assignment; expression 2: Judgment; expression 3: Operation {loop body ;}
Break and continue

Break: jumps out of the current loop, and the code behind it is skipped directly.
Continue: jump out of this loop and execute the next loop directly.

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.