Week 3 C#
Week 3 C#
(PART TWO)
How to code switch statements
• To code a switch statement, you start by coding the switch keyword
followed by a switch expression.
• After the switch expression, you can code one or more case labels that
represent the possible values of the switch expression.
• A switch statement can also contain a default label that identifies the
statements that are executed if none of the values specified by the case
labels match the switch expression.
Continue…
• All of these case labels are coded within the switch statement braces.
• When you code a case label or a default label, you must be sure to
code a colon after it.
• Then, if the label contains one or more statements, you must code a
break statement to exit the switch statement.
Continue…
Continue…
Continue…
Continue…
• A switch statement begins by evaluating its switch expression.
• That means that the statements contained by the next label will be
executed.
How to code loops
• A loop is a tool to efficiently write repetitions of the same or more
often a similar activity.
• When you use a do-while statement, the condition is tested after the
do-while loop is executed.
• Then, any variables or constants that are declared in the block have
block scope.
• If a loop requires just one statement, you don't have to enclose the
state1nent in braces.
Continue…
• If the conditional expression never becomes false, the loop never ends.
• Then, the program goes into an infinite loop that you can cancel by
using the Break All or Stop Debugging commands from the Debug
tool bar.
For loop
• This type of loop is useful when you need to increment or decrement a
counter variable that determines how many ti1nes the loop is going to
be executed.
• To code a for loop, you start by coding the for keyword followed by
three expressions enclosed in parentheses and separated by
semicolons.
Continue…
• The first expression is an initialization expression that typically
declares a counter variable and assigns a starting value to it.