Control statements tell an application which other statements to execute under a particular set of circumstances.
The two main categories of control statements are decision statements and looping statements. The following sections describe the decision and looping statements provided by C#.
A decision statement represents a branch in the program. It marks a place where the program can execute one set of statements or another or possibly no statements at all.
The if=else
statement has the following syntax.
if (condition1) block1;
else if (condition2) block2;
else if (condition3) block3;
...
else blockElse;
The program evaluates each condition and executes the first block for which the condition is true
.
If none of the conditions is true
, then the final blockElse
block is executed. If the final else
statement and the blockElse
are not provided, no code is executed.
Each block could be a single statement or a sequence of statements included in braces.
A switch
statement lets a program execute one of several pieces of code based on a test value. The switch
statement is roughly equivalent to a sequence of if-else
statements.
The basic syntax is as follows.
switch (value)
{
case expression1:
statements1;
break;
case expression2:
statements2;
break;
...
«default:
statementsDefault
break;»
}
The program compares value to the expressions until it finds one that matches or it runs out of expressions to test. The expressions must be constant statements and cannot duplicate each other.
If the program finds a match, it executes the corresponding code. If the program runs out of expressions, it executes the statements in the default
section (if it is present).
You can place multiple case
statements in a group to make them all execute the same code. However, you cannot allow the code from one case section to fall through to the next. If a section contains lines of code, then it must end with a break
statement before the next case begins.
The conditional and null-coalescing operators are actually operators, but they behave like decision statements so their descriptions are included here.
The conditional operator ?:
(sometimes called the ternary operator) takes three operands. If the first operand is true
, it returns the second operand. Otherwise, it returns the third operand.
The null-coalescing operator ??
takes two operands. It returns its left operand if its value is not null
. If the left operand is null
, it returns its right operand.
Looping statements make the program execute a series of statements repeatedly. C# provides four kinds of loops: for
loops, while
loops, do
loops, and foreach
loops.
A for
loop has the following syntax.
for («initialization»; «test»; «increment») block;
Here:
true
, the loop continues. If the result is false
, the loop ends.true
.A while
loop has the following syntax.
while (test)
block;
As long as the test evaluates to true
, the loop executes the block, which can be a single statement or a sequence of statements enclosed in braces. Note that this means the loop might not execute even once if the test is false
when the loop starts.
You cannot omit the test but you can set it to true
to make the while
loop repeat indefinitely.
A do
loop has the following syntax.
do
block;
while (test);
A do
loop is similar to a while
loop except it performs its test after the loop has executed instead of before it executes. That means a do
loop always executes at least once.
A foreach
loop iterates over the items in a collection, array, or other container class that supports foreach
loops. A foreach
loop has the following syntax.
foreach (variable in group)
statement;
Here, group is a collection, array, or other object that supports foreach
.
An enumerator is an object that lets you move through the objects contained by some sort of container class.
You can use an enumerator to view the objects in a collection but not to modify the collection itself. You can use the enumerator to alter the objects in the collection (for example, to change their properties), but you can generally not use it to add, remove, or rearrange the objects in the collection.
Initially, an enumerator is positioned before the first item in the collection. The following table summarizes methods that an enumerator provides to let you move through its collection.
Method | Purpose |
MoveNext | Moves to the next item in the collection. This method returns true if it successfully moves to a new item. |
Reset | Restores the enumerator to its original position before the first object. |
Current | Returns the object that the enumerator is currently reading. |
An iterator is similar in concept to an enumerator. Iterators also provide methods for moving through a collection of items.
Iterators are more specialized than enumerators. How you use them depends on what you need to do and on the kind of iterator, so they are not described in detail here.
Inside a loop, the break
statement makes a program immediately break out of the closest enclosing loop without executing any statements inside the loop that follow the break
statement.
The continue
statement makes a program jump to the beginning of the closest containing loop without executing any statements inside the loop that follow the continue
statement.