If…then…else
If then else is a condtional statement where if condition is true then then clause is executed or else clause is executed
if(condition)
{
//statements;
}
else
//statements;
}
In C#, the expression in the if clause must evaluate to a Boolean.
Switch
The switch…case statement is good for selecting one branch of execution from a set of mutually exclusive ones.When the expression in the switch argument evaluates to one of the values beside a case clause, the code immediately following the case clause executes. This is one example where you don’t need to use curly braces to join statements into blocks; instead, you mark the end of the code for each case using the break statement. You can also include a default case in the switch statement, which will execute if the expression evaluates to none of the other cases.the case values must be constant expressions; variables are not permitted.
if a case clause is fired early on in the block, later clauses cannot be fired unless you use a goto statement to mark that you want them fired, too. it means the break statement is mandatory…
There is one exception to the no-fall-through rule, however, in that you can fall through from one case to the next if that case is empty. This allows you to treat two or more cases in an identical way (without the need for goto statements).
The order of the cases doesn’t matter.
The For Loop
The C# for loop is for iterating a set of statements for a set of times depending on the condition.
(initializer; condition; iterator)
statement(s)
where
-
The initializer is the expression evaluated before the first loop is executed (usually initializing a local variable as a loop counter).
-
The condition is the expression checked before each new iteration of the loop (this must evaluate to true for another iteration to be performed).
-
The iterator is an expression evaluated after each iteration (usually incrementing the loop counter). The iterations end when the condition evaluates to false.
The while Loop
The while loop is identical to the while loop in C++ and Java, and the While…Wend loop in Visual Basic. Like the for loop, while is a pretest loop. The syntax is similar, but while loops take only one expression:
while(condition) statement(s);
Unlike the for loop, the while loop is most often used to repeat a statement or a block of statements for a number of times that is not known before the loop begins.
The do . . . while Loop
The do…while loop is the post-test version of the while loop. It does the same thing with the same syntax as do…while in C++ and Java, and the same thing as Loop…While in Visual Basic. This means that the loop’s test condition is evaluated after the body of the loop has been executed. Consequently, do…while loops are useful for situations in which a block of statements must be executed at least one time.
bool condition;
do
{
// This loop will at least execute once, even if Condition is false.
MustBeCalledAtLeastOnce();
condition = CheckCondition();
} while (condition);
The foreach Loop
The foreach loop allows you to iterate through each item in a collection.
foreach (int temp in arrayOfInts)
{
Console.WriteLine(temp);
}
The goto Statement
The goto statement allows you to jump directly to another specified line in the program, indicated by a label (this is just an identifier followed by a colon):
goto Label1; Console.WriteLine("The control doesn't reavh here...");
Label1: Console.WriteLine("instead control strts executing frm here...");
Some restrictions are there for using goto statement..
-
You can’t jump into a block of code such as a for loop.
-
you can’t jump out of a class, and you can’t exit a finally block after try…catch blocks
There is one place where it is quite handy: jumping between cases in a switch statement, particularly because C#’s switch is so strict on fall-through.
The break Statement
When you use the break statement the control reaches the statement immediately after the end of the loop…If the statement occurs in a nested loop, control will switch to the end of the innermost loop.
The continue Statement
The continue statement is similar to break, and must also be used within a for, foreach, while, or do…while loop. However, it exits only from the current iteration of the loop, meaning that execution will restart at the beginning of the next iteration of the loop, rather than outside the loop altogether.
The return Statement
The return statement is used to exit a method of a class, returning control to the caller of the method. If the method has a return type, return must return a value of this type; otherwise if the method returns void, you should use return without an expression.