Þ
Nested switch Statements
→
It
is possible to have a switch as part of the statement sequence of an outer
switch. This is called a nested switch.
* 2.2.2 Looping structure
1. The For Loop
→
You
can repeatedly execute a sequence of code by creating a loop. C# supplies a
powerful assortment of loop constructs. The one we will look at here is the for
loop.
→
For
repeating a block, the general form is
for(initialization; condition;
iteration)
{
statement sequence
}
→
The
initialization is usually an assignment statement that sets the initial value
of the loop control variable, which acts as the counter that controls the loop.
→
The
condition is a Boolean expression that determines whether the loop will repeat.
The iteration expression defines the amount by which the loop control variable
will change each time the loop is repeated.
→
Notice
that these three major sections of the loop must be separated by semicolons.
The for loop will continue to execute as long as the condition tests true.
→
Once
the condition becomes false, the loop will exit, and program execution will
resume on the statement following the for.
→
An
important point about for loops is that the conditional expression is always
tested at the top of the loop. This means that the code inside the loop may not
be executed at all if the condition is false to begin with.
2. The while Loop
→
Another
of C#’s loops is the while. The general form of the while loop is
while(condition) statement;
→
Where
statement can be a single statement or a block of statements, and condition
defines the condition that controls the loop and may be any valid Boolean
expression.
→
The statement is performed while the condition
is true. When the condition becomes false, program control passes to the line
immediately following the loop. As with the for loop, the while checks the conditional
expression at the top of the loop.
3. The do-while Loop
→
Unlike
the for and the while loops, in which the condition is tested at the top of the
loop, the do-while loop checks its condition at the bottom of the loop. This
means that a do-while loop will always execute at least once. The general form
of the do-while loop is:
do
{
statements;
} while(condition);
→
Although
the braces are not necessary when only one statement is present, they are often
used to improve readability of the do-while construct, thus preventing
confusion with the while. The do-while loop executes as long as the conditional
expression is true.
4. The foreach Loop
→
The
foreach loop is used to cycle through the elements of a collection. A
collection is a group of objects. C# defines several types of collections, of
which one is an array. The general form of foreach is shown here:
foreach(type loopvar in collection)
statement;
→
Here,
type loopvar specifies the type and name of an iteration variable. The
iteration variable receives the value of the next element in the collection
each time the foreach loop iterates.
→
When
the loop begins, the first element in the array is obtained and assigned to
loopvar. Each subsequent iteration obtains the next element from the array and
stores it in loopvar. The loop ends when there are no more elements to obtain.
Thus, the foreach cycles through the array one element at a time, from start to
finish.
→
One
important point to remember about foreach is that the iteration variable
loopvar is read-only. This means you can’t change the contents of an array by
assigning the iteration variable a new value.
0 comments:
Post a Comment