This preview shows pages 1–9. Sign up to view the full content.
CSE 114 – Computer Science I
Conditional Statements
Quirpon Island, Newfoundland
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document •
Take two data values of the same type
•
Returns a
boolean
result (
true
or
false
)
<
(less than)
>
(greater than)
<=
(less than or equal to)
>=
(greater than or equal to)
==
(equal to)
!=
(not equal to)
•
Examples:
Relational Operators
// result is true
// result is true
// result is false
result = (5 <= 9);
result = (3.9 > 3.19);
result = (’a’ == ’A’);
boolean result;
We will use these
to build
boolean
expressions
Logical Operators
• Two data values of type
boolean
• Return a
boolean
result (true or false)
–
&&
(logical AND)
,
&
(bitwise AND)
–

(logical OR)
,

(bitwise OR)
// result is false
// result is true
// result is false
result = (5<=9 && 8>9);
result = (5<=9  8>9);
boolean result;
result = !(5<=9);
• Unary logical operator: ! (NOT)
We will not use
these operators
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Fully Parenthesize your Expressions
• What is the value of result after the assignment expression:
boolean result;
result = 5 + x < 7 && y/z == 0;
// really confusing
• Answer: I don’t care because I would never write it
that way
result = ( (5 + x) < 7 )
&& ( (y/z) == 0) );
// better
The if…else if…else statement
• Used for program decision making
•
if
would test for first choice
•
else if
s would test for additional choices
– not always
required
•
else
would be for all other cases
– not
always
required
• Can you think of any decisions a program makes?
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document How do we use it?
•
Use either:
•
You may also mix them:
if (boolean expression)
statement;
else if
(boolean expression2)
{
statement;
statement;
}
else
statement;
if (boolean expression)
{
statement;
statement;
}
if (boolean expression)
statement;
•
Or:
Want to be safe? Always use brackets
if (boolean expression)
{
statement(s);
}
else if (boolean expression2)
{
statement(s);
}
else
{
statement(s);
}
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Example using brackets
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your systolic blood pressure: ");
int systolicBP = keyboard.nextInt();
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 11/29/2011 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.
 Spring '08
 TASHBOOK
 Computer Science

Click to edit the document details