CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

20
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions

description

CS-1010 Dr. Mark L. Hornick 3 The Java if statement specifies whether a block of code should execute …depending on the result of evaluating a test condition called a boolean expression public static void main(…) { if( ){ // block of code that executes when // the boolean expression is true } }

Transcript of CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

Page 1: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

1

Selection and Iterationand conditional expressions

Page 2: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

2

Instructions in a Java program are executed in sequence

Starting with the first instruction in your main() method and continuing to the end.

To alter the control flow, decision-making instructions can be added to a program

An instruction that alters the control flow is called a control statement or selection statement.

public static void main(…)

<Java instruction 1> <Java instruction 2> … <Java instruction N>}

Page 3: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

3

The Java if statement specifies whether a block of code should execute

…depending on the result of evaluating a test condition called a boolean expression

public static void main(…) {<Java instructions…>

if( <boolean expression> ){// block of code that executes when

// the boolean expression is true}

<more Java instructions…>}

Page 4: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

4

What is a boolean expression?First recall: A numerical expression uses arithmetic

operators and is evaluated to a numerical value Some numerical expressions using binary arithmetic

operators: x+y x-y x*y x/y

Arithmetic operators operate on numerical datatypes byte, short, int, long, float, double

Exception: (+ operator works for the String datatype)

Page 5: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

5

The boolean datatype is another primitive datatype

Variables declared as boolean datatypes can only assume one of the two boolean values: true falseboolean isOK = false;boolean insufficientFunds = true;

boolean, true, and false are Java reserved words

Page 6: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

6

A boolean expression uses relational operators between numerical values and is evaluated to a boolean value: either true or false. x < y // less than operator; evaluates to

true if x is less than y; otherwise false x <= y // less than or equal to; note =< is not

valid x > y // greater than x >= y // greater than or equal to x == y // equal to; be careful w.r.t. x=y x != y // not equal to

Page 7: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

7

Variables of data type boolean can be declared and assigned to the result of a boolean expression:

int score = 86;boolean result;

result = 70 < score; if ( result ){

<Java instruction 1> <Java instruction 2>

…<Java instruction N>

}

Page 8: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

8

The { } braces are optional, but this means that the block of code is the single instruction following the if statement

if (<boolean expression>)<Java instruction 1> // executed if true<Java instruction 2> // executed always

This means that whatever Java statement following the if statement will be executed when the boolean expression is trueAnd only the single instruction following the if(…)

Page 9: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

9

Methods can be declared to return a boolean value

The String class has a contains() method that returns a boolean result:

String message = “Welcome to MSOE”;boolean isPresent = message.contains(“to”);if( isPresent ) // contains “to”

JOptionPane.showInputDialog(…);

Page 10: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

10

A more complex if statement

if (<boolean expression>){<then block> // executed when exp. is true

} else {<else block> }// executed when exp. is false

The else clause is optional When present, the instructions in the else

block are executed only when the boolean expression is false

Page 11: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

11

The Boolean operatorsRepeat: relational operators take numerical operands

and return a boolean value: either true or false. Example: (x<y)Exception: == and != operators can be used between boolean

datatypes

A boolean operator takes boolean values as its operands and returns a boolean value. The three boolean operators are && // means “and” || // means “or” ! // means “not”

Page 12: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

12

Boolean operators and their meanings:

P Q P && Q P || Q !P

false false false false true

false true false true true

true false false true false

true true true true false

Page 13: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

13

Complex boolean expressions

int x = 100, t=50;boolean passed, record; // initialized to falsepassed = (x >= 70); // or !(x < 70)record = (t < 60); // or !(t >= 60)if( pass && record )

msg = “You passed in record time!”;

Page 14: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

CS-1010Dr. Mark L. Hornick

14

Complex boolean expressionsint x = 100, t=50; boolean pass, record; // initialized to falsepass = (x >= 70); // evaluates to truerecord = (t < 60); // evaluates to trueboolean both = pass && record;if( both ){

msg = “You passed in record time!”;} // always safer to include { }

Page 15: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

SE-1010Dr. Mark L. Hornick

15

Looping and Repetition Statements control a block of code to be executed for: a fixed number of times

1, 10, 10000…

until a certain condition is met a value remains positive (or negative) a value remains below (or above) some limit …

Page 16: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

SE-1010Dr. Mark L. Hornick

16

The two (three) main loop types:

while() and it’s cousin do…while() Sentinel-controlled loops, where the loop body is

executed repeatedly until a designated condition, called a sentinel, is encountered.

for() A count-controlled loop, where the loop body is

executed a fixed number of times.

Page 17: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

SE-1010Dr. Mark L. Hornick

17

The do-while() StatementFormat:do {<statements>} while (<boolean expression>);

The <statements> part is known as the loop bodyThe loop body is executed until the <boolean expression> becomes false And is always executed at least once!

Even if <boolean expression> is false

Page 18: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

SE-1010Dr. Mark L. Hornick

18

The do-while() statement Demo

Page 19: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

SE-1010Dr. Mark L. Hornick

19

The while Statement

Format: while ( <boolean expression> ){

<statements that repeat> // 0 or more}

As long as the <boolean expression> is true, the loop body is executed

Page 20: CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

SE-1010Dr. Mark L. Hornick

20

JOptionPane Confirmation dialog

Created by using the showConfirmDialog method of the JOptionPane class:

int answer = JOptionPane.showConfirmDialog(null, “Play another game?", “Confirmation",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);// answer will contain the values corresponding// to JOptionPane.YES_OPTION or JOptionPane.NO_OPTION