Presentation is loading. Please wait.

Presentation is loading. Please wait.

(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.

Similar presentations


Presentation on theme: "(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student."— Presentation transcript:

1 (c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student

2 (c) 2003 E.S.Boese 2 Conditional Statements if :: checks condition, if ______, conditional statement executed if-else :: depending on conditions executes either the if or the else statements switch :: ________ to one of many labels depending on the value of switch expression

3 (c) 2003 E.S.Boese 3 if statement if ( booleanExpression ) { statement; … } ** OR ** if ( booleanExpression ) statement; Note that { and } are unnecessary when the if statement only has _____ single statement to do Note also that there is no semicolon at the end of if ( …. ) Do not put a semi-colon at the end of if ( … )

4 (c) 2003 E.S.Boese 4 if statement condition evaluated statement(s) true false if ( booleanExpression ) { statement; … } ** OR ** if ( booleanExpression ) statement; Note that { and } are unnecessary when the if statement only has one single statement to do Note also that there is no semicolon at the end of if ( …. )

5 (c) 2003 E.S.Boese 5 if statement Evaluate the boolean expression If it evaluates to true,  Do the statement(s) following Otherwise do ________ and __________ with the program

6 (c) 2003 E.S.Boese 6 Boolean Expressions Boolean expressions evaluate to either ________ or _________ Examples (refer to Chapter 9)  Is A greater than B?  Is A equal to B?  Is A less than or equal to B? In most cases, A and B can be any ________. Primitives (int, double, char, etc.) work differently than objects (String, etc.)

7 (c) 2003 E.S.Boese 7 Boolean Expressions con’t Examples (refer to ch 9)  Is A greater than B? A > B  Is A equal to B? A == B  Is A less than or equal to B? A <= B if ( A == B ) C = A*2;

8 (c) 2003 E.S.Boese 8 Boolean Expressions A boolean expression often uses one of Java's equality operators or relational operators, which all return boolean results:  ==equal to  !=  <less than  >greater than  <=less than or  >= Note the difference between the equality operator (==) and the assignment operator (=)

9 (c) 2003 E.S.Boese 9 Boolean Expressions con’t Primitives (int, double, char, etc.) work differently than objects (String, etc.) int x = 5; String s = “Hi”; int y = 5; String t = “Hi”; boolean z = (x == y); boolean u = (s==t); if( x >= y ) if( s.equals(t) ) y = y + x;u = true;

10 (c) 2003 E.S.Boese 10 Grades if example

11 (c) 2003 E.S.Boese 11 if else statement if ( booleanExpression ) { statements; multiplestatements; } else { otherstatements; } condition evaluated statements truefalse otherstatements

12 (c) 2003 E.S.Boese 12 if else statement Evaluate the boolean expression If it evaluates to true,  Do the statement(s) following Otherwise do the other statements

13 (c) 2003 E.S.Boese 13 Grades if else if else example

14 (c) 2003 E.S.Boese 14 if-else Example Compare Strings Comparing strings – use.equals( ) method do NOT use == String txt = textfield.getText( ); if ( txt.equals( “Cookie Monster” ) ) textarea.append( “Welcome back “ + txt ); else textarea.append( “You’re not logged in” );

15 (c) 2003 E.S.Boese 15 instanceof operator instanceof operator returns ______ or ______ The ________ operand is the one you want to check The ________ operand is an object data type if ( obj instanceof JButton ) if ( obj instanceof JTextArea ) if ( obj instanceof JList )

16 (c) 2003 E.S.Boese 16 if-else Example instanceof instanceof operator if ( object instanceof JComboBox ) { textarea.append( “It’s a combobox” ); } else if( object instanceof JRadioButton ) { textarea.append( “It’s a radio button” ); }

17 (c) 2003 E.S.Boese 17 if and else if ( x > 0 ) if ( y > 0 ) t = 1; else t = 2; What is the value of t? NOTE: an else clause is always matched to the _______________________

18 (c) 2003 E.S.Boese 18 switch (Extra material) switch( expression ) { case constant1: statements; break; case constant2: statements; break; … default: statements; }

19 (c) 2003 E.S.Boese 19 switch Select one of several actions, depending on the value of some expression If variable == constant, do this, otherwise if variable == constant2, do that, otherwise, …. Cases must be _________, of the same _______ as the expression Use __________ if no other cases match ________ jumps out of the switch, otherwise keep executing until a break

20 (c) 2003 E.S.Boese 20 switch Expression MUST evaluate to either a _____, byte, short, or _______ Constants must be ________ or _______ variables ‘default’ is __________

21 (c) 2003 E.S.Boese 21 switch example switch( aValue ) { case 5: label.setText( “High” ); statements; break; case 4: case 3: case 2: label.setText( “Medium” );; break; default: label.setText( “Low” ); }

22 (c) 2003 E.S.Boese 22 Grades switch example

23 (c) 2003 E.S.Boese 23 Good Fun Exercises Can any if-else structure be represented also as a switch structure? Can any switch structure also be represented as an if-else structure?

24 (c) 2003 E.S.Boese 24 Summary if if else instanceof switch


Download ppt "(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student."

Similar presentations


Ads by Google