320 likes | 325 Views
Learn how to make decisions in C# using the if statement, if-else statement, switch statement, and conditional operator. Understand compound expressions, nested loops, and the NOT operator.
E N D
Chapter Five Selection and Repetition
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
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
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
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)
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;
Making Decisions Using the if-else Statement • Dual-alternative if-else statement used in a program
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
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
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
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
Using Compound Expressions In if Statements • What would happened if the parentheses were not used in the above if statement?
Using Compound Expressions In if Statements • Output of MovieDiscount program before adding parentheses to alter precedence
Using Compound Expressions In if Statements • Output of MovieDiscount program after adding parentheses to alter precedence
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
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
Making Decisions Using the switch Statement • Example switch structure
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
Making Decisions Using the switch Statement • Example switch structure using multiple labels to execute a single statement block
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
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”);
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
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
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
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
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
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
Using the for Loop • Printing integers 1 through 10 with while and for loops
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
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
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
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