Chapter 05 Selection and Repetition in C#
Chapter 05 Selection and Repetition in C#
1
Objectives
2
Objectives
3
Making Decisions Using the if Statement
4
Making Decisions Using the if Statement
5
Making Decisions Using the if-else
Statement
• Some decisions you make are dual-alternative
decisions; they have two possible outcomes
• The if-else statement takes the form
if(expression)
statement1;
else
statement2;
6
Making Decisions Using the if-else
Statement
• Dual-alternative if-else statement used in a program
7
Using Compound Expressions In if
Statements
• As an alternative to nested if statements, you can use
the conditional AND operator within a Boolean
expression to determine whether two expressions are
true
• The two sample codes shown below work the same way
8
Using Compound Expressions In if
Statements
• You are never required to use the AND operator because
nested if statements always achieve the same results
• You must include a complete Boolean expression on
each side of the && operator
• A common mistake is as follows:
if(saleAmount >1000 &&<5000)
• If the first expression is false, the second expression is
never evaluated, because its value does not matter
9
Using Compound Expressions In if
Statements
• You can use the conditional OR operator when you
want some action to occur even if only one of two
conditions is true
• The OR operator is written as ||
• When the OR operator is used in an if statement, only
one of the two Boolean expressions in the if statement
needs to be true for the resulting true statement to
execute
10
Using Compound Expressions In if
Statements
• You can combine as many AND and OR operators in an
expression as you need
• When you combine AND and OR operators within the
same Boolean expression, the AND operators take
precedence
• You can use parentheses to correct the logic and the
order of the evaluation of expressions
11
Using Compound Expressions In if
Statements
12
Using Compound Expressions In if
Statements
• Output of MovieDiscount program before adding parentheses to alter
precedence
13
Using Compound Expressions In if
Statements
• Output of MovieDiscount program after adding parentheses
to alter precedence
14
Making Decisions Using the switch
Statement
• By nesting a series of if and else statements, you can
choose from any number of alternatives
• An alternative to a series of nested if statements is to
use the switch statement
15
Making Decisions Using the switch
Statement
• The switch structure uses four new keywords:
– switch starts the structure and is followed immediately by a test
expression
– case is followed by one of the possible values that might equal
the switch expression
– break optionally terminates a switch structure at the end of each
case
– default optionally is used prior to any action that should occur if
the test expression does not match any case
16
Making Decisions Using the switch
Statement
18
Making Decisions Using the switch
Statement
20
Using the NOT Operator
if(!OverSeventeen)
Console.WriteLine(“You can’t see the movie”);
else
Console.WriteLine(“You can see the movie”);
21
Using the while Loop
22
Using the while Loop
23
Using the while Loop
24
Using the while Loop
25
Using the for Loop
26
Using the for Loop
27
Using the for Loop
28
Using the do Loop
29
Chapter Summary
30
Chapter Summary
31
Chapter Summary
32