Download presentation
Presentation is loading. Please wait.
2
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University
3
Road Map if / else continued –Nested if / else statements Logical operators –&&, ||, ^ –! –&, | switch statement Reading: –Liang 5: chapter 2: 2.10;chapter 3: 3.2 –Liang 6: chapter 3: 3.3, 3.5
4
3 review What is the output of the following code fragment? int a = 100; if (a = 100) System.out.println (" a is equal to " + a); What is the output of the following code fragment? int a = 100, b = 1; if (a < b) System.out.println ("a is less than b");
5
4 review What is the output of the following code fragment? int a = 100; if (a != 100); System.out.println (" a is equal to " + a); What is the output of the following code fragment? int a = 100, b = 1; if (a < b) System.out.println ("a is less than b"); System.out.println ("Thank you");
6
Java Provides a shortcut if/else operator: This is Java’s only ternary operator (i.e. it takes three operands) System.out.println ( (sales > 100) ? "Federal Express" : "US Mail"); The conditional Statement. True Option False Option ? short- cut operator colon
7
nested if statements When one if/else structure is contained inside another if/else structure it is called a nested if/else. if (grade > 60) { if (grade > 70) System.out.println("You passed"); else System.out.println("You passed but need a tutor"); } else System.out.println("You failed");
8
else if Usually you try to nest within the else statement. Note the indentation. if (grade > 70) System.out.println("You passed"); else if (grade > 60) System.out.println("You passed but need a tutor"); else System.out.println("You failed");
9
8 Choosing the flavor of your if When you have multiple possible branches, you can have any of the following situations: 1.You want to execute exactly one of the branches 2.You want to execute zero or one of the branches 3.You want to execute zero or more branches Which would be appropriate for each of the situations above: a)A series of if s b)A series of if / else if s ending with an else if c)A series of if / else if s ending with an else 1 – c 2 – b 3 – a
10
9 Using boolean variables as flags You may want to use boolean variables to hold the value of a boolean expression. For example: boolean passed = (grade >= 65) Then you can use the variable later in a conditional statement: if (passed) System.out.println ("You passed"); else System.out.println ("You failed");
11
&& - Logical AND ((boolean exp a) && (boolean exp b)) When using the and operator (&&), both expression a and b must be true for the compound statement to be true. truth table For example: ((total > 50) && (status == 0))
12
|| - Logical OR ((boolean exp a) || (boolean exp b)) When using the or operator (||), at least one expression a or b must be true for the compound statement to be true. truth table For example: ((total > 50) || (status == 0))
13
^ - Exclusive OR ((boolean exp a) ^ (boolean exp b)) When using the exclusive or operator (^), at least one expression a or b must have opposite Boolean values for the compound statement to be true. truth table For example: ((person1 == 1) ^ (person2 == 1))
14
logical negation ! !(a) Reverses the truth or falsity of expression a Boolean expression ! has high precedence so you must use parenthesis You can avoid using the logical negation by expressing the condition differently with an appropriate relational operator. However, in the case of complex expressions, it is sometimes easier to use negation. Note: its a unary operator
15
14 Unconditional vs. Conditional Boolean Operators Java provides us with a second “and” operator and a second “or” operator. The unconditional operators guarantee that both expressions will be evaluated In this class, you should just use the conditional operators.
16
switch Multiple-Selection Structure Used when testing a variable or expression for EQUALITY (ie no >, =, <= tests) separately for each of the constant integral values it may assume. Preferred over if else in situations where you are testing the same expressions for equality with many different values. Allows you to perform different actions for each value.
17
switch Multiple-Selection Structure switch (expression){ case value1: action(s); break; case value2: action(s); break; … default: actions(s); break; } keyword switch could use more than one case; if the same actions are required expression can be a variable or a more complicated expression actions within a single case do not need brackets the default case will be executed in the event that no other case is
18
The switch Multiple-Selection Structure Flowchart of the switch structure true false...... case a case a action(s)break case b case b action(s)break false case z case z action(s)break true default action(s) 2000 Prentice Hall, Inc. All rights reserved.
19
beware of “fall through” If you forget to use the break keyword between cases, unexpected things may happen. Once a case tests true, all the statements following that case, will be executed until the next break. Experienced programmers may use this on purpose. For this class we will rarely use fall though.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.