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.