Top Banner
(c) 2003 E.S.Boese 1 Conditionals Chapter 10 - Student
24

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

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: (c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.

(c) 2003 E.S.Boese 1

Conditionals

Chapter 10 - Student

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

(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

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

(c) 2003 E.S.Boese 3

if statement

if ( booleanExpression ){

statement;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 ( … )

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

(c) 2003 E.S.Boese 4

if statement

conditionevaluated

statement(s)

truefalse

if ( booleanExpression ){

statement;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 ( …. )

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

(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

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

(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.)

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

(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;

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

(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 (=)

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

(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;

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

(c) 2003 E.S.Boese 10

Grades if example

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

(c) 2003 E.S.Boese 11

if else statement

if ( booleanExpression ){

statements;multiplestatements;

}else{

otherstatements;}

conditionevaluated

statements

true false

otherstatements

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

(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

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

(c) 2003 E.S.Boese 13

Grades if else if else example

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

(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” );

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

(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 )

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

(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” );}

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

(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

_______________________

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

(c) 2003 E.S.Boese 18

switch (Extra material)

switch( expression ){

case constant1:statements;statements;break;

case constant2:statements;break;

…default:

statements;}

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

(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

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

(c) 2003 E.S.Boese 20

switch

Expression MUST evaluate to either a _____, byte, short, or _______

Constants must be ________ or _______ variables

‘default’ is __________

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

(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” );

}

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

(c) 2003 E.S.Boese 22

Grades switch example

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

(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?

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

(c) 2003 E.S.Boese 24

Summary

if if else instanceof switch