Iteration and Flow Control Statement
Iteration and Flow Control Statement
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.
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.
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 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.