Introduction to Computers and Programming Lecture 6 New York University Road Map • if / else continued – Nested if / else statements • Logical operators –

Download Report

Transcript Introduction to Computers and Programming Lecture 6 New York University Road Map • if / else continued – Nested if / else statements • Logical operators –

Introduction to Computers and
Programming
Lecture 6
New York University
Road Map
• if / else continued
– Nested if / else statements
• Logical operators
– &&, ||, ^
– !
– &, |
• switch statement
• Reading:
– chapter 2: 2.10
– chapter 3: 3.2
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“);
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”);
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.
? shortcut operator
True
Option
colon
False
Option
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");
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");
&& - 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
F A LS E
TR U E
F A LS E
F A LS E
F A LS E
TR U E
F A LS E
TR U E
• For example:
((total > 50) && (status == 0))
|| - 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
F A LS E
TR U E
•
F A LS E
F A LS E
TR U E
TR U E
TR U E
TR U E
For example:
((total > 50) || (status == 0))
^ - 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
FALSE
TRUE
•
FALSE
FALSE
TRUE
TRUE
TRUE
FALSE
For example:
((person1 == 1) ^ (person2 == 1))
logical negation !
!(a)
• Reverses the truth or falsehood 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
12
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.
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.
switch Multiple-Selection Structure
switch (expression) {
case value1:
action(s);
break;
keyword switch
expression can be a variable or
a more complicated expression
case value2:
action(s);
break;
…
default:
actions(s);
break;
}
could use more than one case;
if the same actions are required
actions within a single case do
not need brackets
the default case will be executed
in the event that no other case is
The switch Multiple-Selection Structure
• Flowchart of the switch structure
case a
true
case a action(s)
break
case b action(s)
break
case z action(s)
break
false
case b
true
false
.
.
.
case z
true
false
default action(s)
 2000 Prentice Hall, Inc.
All rights reserved.
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 is fall
though.