Week 3 C#

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

DECISIONS

(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.

• After evaluating the switch expression, the switch statement transfers


control to the appropriate case label.

• If control isn't transferred to one of the case labels, the optional


default label is executed.
Continue…
• The break statement exits the switch statement. If a label contains one
or more statements, the label must end with a break statement.

• If a label doesn't contain any statements, code execution will fall


through to the next label.

• 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.

• C# provides three different statements for controlling the execution of


loops.
1. for loop
2. While loop
3. And do-while loop
While and do-while loops
• The difference between while loop and do-while is:

• The Boolean expression is evaluated at the beginning of a while


loop and at the end of a do-while loop.

• As a result, the statements in a while loop are executed zero or more


times, while the statements in a do-while loop are always executed at
least once.
Continue…
Continue…
Continue…
• When you use a while statement, the condition is tested before the
while loop is executed.

• When you use a do-while statement, the condition is tested after the
do-while loop is executed.

• A while or do-while loop executes the block of statements within its


braces as long as its Boolean expression is true.
Continue…
• If a loop requires more than one statement, you must enclose the
statements in braces.

• 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.

• The second expression is a Boolean expression that specifies the


condition under which the loop executes.

• And the third expression is an incre1nent expression that determines


how the counter variable is incremented or decremented each time the
loop is executed.
Continue…
Continue…
Continue…
• Within the parentheses of a for loop, you code an initialization
expression that declares a counter variable and assigns a starting value
to it,
• a Boolean expression that specifies the condition under which the loop
executes,
• and an increment expression that indicates how the counter variable
should be incremented or decremented each time the loop is executed.

You might also like