Sie sind auf Seite 1von 11

Iteration and Flow control statement

The statements that enable you to control program flow in a C# application fall into three main categories: selection statements, iteration statements, and jump statements. In each of these cases, a test resulting in a Boolean value is performed and the Boolean result is used to control the application's flow of execution. You use selection statements to determine what code should be executed and when it should be executed. C# features two selection statements: the switch statement, used to run code based on a value, and the if statement which runs code based on a Boolean condition.

The most commonly used of these selection statements is the if statement. The if statement executes one or more statements if the expression being evaluated is true. The if statement's syntax follows-the square brackets denote the optional use of the else statement (which we'll cover shortly): if (expression) statement1 [else statement2]

Here, expression is any test that produces a Boolean result. If expression results in true, control is passed to statement1. If the result is false and an else clause exists, control is passed to statement2. Also note that statement1 and statement2 can consist of a single statement terminated by a semicolon (known as a simple statement) or of multiple statements enclosed in braces. One aspect of the if statement that catches new C# programmers off guard is the fact that the expression evaluated must result in a Boolean value. This is in contrast to languages such as C++ where you're allowed to use the if statement to test for any variable having a value other than 0.

The switch Statement


Using the switch statement, you can specify an expression that returns an integral value and one or more pieces of code that will be run depending on the result of the expression. It's similar to using multiple if/else statements, but although you can specify multiple (possibly unrelated) conditional statements with multiple if/else statements, a switch statement consists of only one conditional statement followed by all the results that your code is prepared to handle. Here's the syntax: switch (switch_expression) { case constant-expression: statement jump-statement case constant-expressionN:

There are two main rules to keep in mind here. First, the switch_expression must be of the type (or implicitly convertible to) int, uint, long, ulong, char, or string (or an enum based on one of these types). Second, you must provide a jump-statement for each case statement unless that case statement is the last one in the switch, including the break statement. First, the switch_expression is evaluated, and then the result is compared to each of the constant-expressions or case labels,defined in the different case statements. Once a match is made, control is passed to the first line of code in that case statement.

In C#, the while, do/while, for statements enable you to perform controlled iteration, or looping. In each case, a specified simple or compound statement is executed until a Boolean expression resolves to true. The do/while statement takes the following form: do embedded-statement while ( Boolean-expression )

Because the evaluation of the while statement's Booleanexpression occurs after the embedded-statement, you're guaranteed that the embedded-statement will be executed at least one time.

The break Statement


You use the break statement to terminate the current enclosing loop or conditional statement in which it appears. Control is then passed to the line of code following that loop's or conditional statement's embedded statement. Having the simplest syntax of any statement, the break statement has no parentheses or arguments and takes the following form at the point that you want to transfer out of a loop or conditional statement

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication22 { class Program { static void Main(string[] args) { int i; for (i = 1; i <= 100; i++) { if (i%6==0) Console.WriteLine(i); if (i == 66) break; } } } }

The continue Statement


Like the break statement, the continue statement enables you to alter the execution of a loop. However, instead of ending the current loop's embedded statement, the continue statement stops the current iteration and returns control back to the top of the loop for the next iteration.

The return statement has two functions. It specifies a value to be returned to the caller of the currently executed code (when the current code is not defined as returning void), and it causes an immediate return to the caller. The return statement is defined with the following syntax: return [ return-expression ] When the compiler encounters a method's return statement that specifies a return-expression, it evaluates whether the returnexpression can be implicitly converted into a form compatible with the current method's defined return value. It's the result of that conversion that is passed back to the caller.

Das könnte Ihnen auch gefallen