L4 - CSE 114 Computer Science I Conditional Statements...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 114 – Computer Science I Conditional Statements Quirpon Island, Newfoundland
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 4
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?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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:
Background image of page 6
Want to be safe? Always use brackets if (boolean expression) { statement(s); } else if (boolean expression2) { statement(s); } else { statement(s); }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example using brackets Scanner keyboard = new Scanner(System.in); System.out.print("Enter your systolic blood pressure: "); int systolicBP = keyboard.nextInt();
Background image of page 8
Image of page 9
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.

Page1 / 25

L4 - CSE 114 Computer Science I Conditional Statements...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online