Você está na página 1de 32

Chapter Five

Selection and Repetition

1
Objectives

• How to make decisions using the if statement


• How to make decisions using the if-else
statement
• How to use compound expressions in if
statements
• How to make decisions using the switch
statement
• How to use the conditional operator

2
Objectives

• How to use the NOT operator


• How to loop using the while statement
• How to loop using the for statement
• How to loop using the do statement
• How to use nested loops

3
Making Decisions Using the if Statement

• The if and if-else statements are the two most


commonly used decision-making statements in C#
• The if statement takes the form
if(expression)
statement;
• Expression represents any C# expression that can be
evaluated as true or false
• Statement represents the action that will take place if
the expression evaluates as true

4
Making Decisions Using the if Statement

• You can place any number of statements


within the block contained by curly
braces, including another if statement
(which is called a nested 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

• What would happened if the parentheses were not used in the


above if statement?

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

• Example switch structure


17
Making Decisions Using the switch
Statement
• Not allowing code to reach the end of a case is known as
the “no fall through rule”
• The governing type of a switch statement is established
by the switch expression
• A switch statement does not need to contain a default
case
• You are never required to use a switch structure because
the same results can be achieved with the if statement

18
Making Decisions Using the switch
Statement

• Example switch structure using multiple labels to execute a single


statement block
19
Using the Conditional Operator

• The conditional operator is used as an abbreviated


version of the if-else statement
• It requires three expressions separated with a question
mark and a colon
• testExpression ? trueResult : falseResult

20
Using the NOT Operator

• The NOT operator is written as an exclamation point (!)


• Any expression that evaluates as true becomes false
when preceded by the NOT operator, and any false
expression preceded by the NOT operator becomes true
• Examples:

if(!OverSeventeen)
Console.WriteLine(“You can’t see the movie”);
else
Console.WriteLine(“You can see the movie”);

21
Using the while Loop

• A loop is a structure that allows repeated execution of a


block of statements
• Within a looping structure, a Boolean expression is
evaluated. If it is true, a block of statements called the
loop body executes, and then the Boolean expression is
evaluated again.
• As long as the expression is true, the statements in the
loop body continue to execute

22
Using the while Loop

• You can use a while loop to execute a body of


statements continuously while some condition continues
to be true
• A while loop consists of the keyword while, followed by a
Boolean expression within parentheses, followed by the
body of the loop
• A loop that never ends is called an infinite loop

23
Using the while Loop

• To make a while loop end, three separate actions should


occur:
– Some variable, the loop control variable, is initialized
– The loop control variable is tested in the while statement
– The body of the while statement must take some action that
alters the value of the loop control variable

24
Using the while Loop

• Output of typical execution of LoopingBankBal program


• A value that a user must supply to stop a loop is called a sentinel
value

25
Using the for Loop

• A loop that executes a specific number of times is a


definite loop or a counter loop
• To write a definite loop, you initialize a loop control
variable, and as long as it does not pass a limit, you
continue to execute the body of the while loop
• When you use a for statement, you can indicate the
starting value for the loop control variable, the test
condition that controls loop entry, and the expression
that alters the loop control variable

26
Using the for Loop

• The three semicolon separated sections of a for


statement are used for:
– Initializing the loop control variable
– Testing the loop control variable
– Updating the loop control variable

27
Using the for Loop

• Printing integers 1 through 10 with while and for loops

28
Using the do Loop

• To create a loop whose body executes at least


once, you use the do loop. The do loop checks
the bottom of the loop after one repetition has
occurred.
• You can place loops within other loops

29
Chapter Summary

• You use an if statement to make a single-alternative


decision
• When you make a dual-alternative decision, you can use
an if-else statement
• You can use the conditional AND operator within a
Boolean expression to determine whether two
expressions are both true
• You can use the conditional OR operator when you want
some action to occur when one or both of two conditions
are true

30
Chapter Summary

• When you compare AND and OR operators within the


same Boolean expression without parentheses, the AND
operators takes precedence
• The switch statement tests a single variable against a
series of exact matches
• The conditional operator is used as an abbreviated
version of the if-else statement
• You use the NOT operator to negate the result of any
Boolean expression

31
Chapter Summary

• You can use a while loop to execute a body of


statements continuously while some condition continues
to be true
• When you use a for statement, you can indicate the
starting value of the loop control variable, the test
condition that controls loop entry, and the expression
that alters the loop control variable
• The do loop checks the bottom of the loop after one
repetition has occurred
• You can nest any combination of loops to achieve
desired results

32

Você também pode gostar