Selection and Iteration and conditional expressions CS1010 Dr

  • Slides: 20
Download presentation
Selection and Iteration and conditional expressions CS-1010 Dr. Mark L. Hornick 1

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

Instructions in a Java program are executed in sequence public static void main(…) <Java

Instructions in a Java program are executed in sequence public static void main(…) <Java instruction 1> <Java instruction 2> … <Java instruction N> } 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. CS-1010 Dr. Mark L. Hornick 2

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

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…> } CS-1010 Dr. Mark L. Hornick 3

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

What is a boolean expression? First recall: A numerical expression uses arithmetic operators and is evaluated to a numerical value l Some numerical expressions using binary arithmetic operators: l l l Arithmetic operators operate on numerical datatypes l l x+y x-y x*y x/y byte, short, int, long, float, double Exception: (+ operator works for the String datatype) CS-1010 Dr. Mark L. Hornick 4

The boolean datatype is another primitive datatype Variables declared as boolean datatypes can only

The boolean datatype is another primitive datatype Variables declared as boolean datatypes can only assume one of the two boolean values: l l true false boolean is. OK = false; boolean insufficient. Funds = true; l boolean, true, and false are Java reserved words CS-1010 Dr. Mark L. Hornick 5

A boolean expression uses relational operators between numerical values and is evaluated to a

A boolean expression uses relational operators between numerical values and is evaluated to a boolean value: either true or false. l l l x< y if x is x <= y valid x> y x >= y x == y x != y // less than operator; evaluates to true less than y; otherwise false // less than or equal to; note =< is not // greater than or equal to // equal to; be careful w. r. t. x=y // not equal to CS-1010 Dr. Mark L. Hornick 6

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

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> } CS-1010 Dr. Mark L. Hornick 7

The { } braces are optional, but this means that the block of code

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 true And only the single instruction following the if(…) CS-1010 Dr. Mark L. Hornick 8

Methods can be declared to return a boolean value The String class has a

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 is. Present = message. contains(“to”); if( is. Present ) // contains “to” JOption. Pane. show. Input. Dialog(…); CS-1010 Dr. Mark L. Hornick 9

A more complex if statement if (<boolean expression>){ <then block> // executed when exp.

A more complex if statement if (<boolean expression>){ <then block> // executed when exp. is true } else { <else block> }// executed when exp. is false l l The else clause is optional When present, the instructions in the else block are executed only when the boolean expression is false CS-1010 Dr. Mark L. Hornick 10

The Boolean operators Repeat: relational operators take numerical operands and return a boolean value:

The Boolean operators Repeat: 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 l l l && || ! // means “and” // means “or” // means “not” CS-1010 Dr. Mark L. Hornick 11

Boolean operators and their meanings: P Q P && Q P || Q !P

Boolean operators and their meanings: P Q P && Q P || Q !P false true false true true false CS-1010 Dr. Mark L. Hornick 12

Complex boolean expressions int x = 100, t=50; boolean passed, record; // initialized to

Complex boolean expressions int x = 100, t=50; boolean passed, record; // initialized to false passed = (x >= 70); // or !(x < 70) record = (t < 60); // or !(t >= 60) if( pass && record ) msg = “You passed in record time!”; CS-1010 Dr. Mark L. Hornick 13

Complex boolean expressions int x = 100, t=50; boolean pass, record; // initialized to

Complex boolean expressions int x = 100, t=50; boolean pass, record; // initialized to false pass = (x >= 70); // evaluates to true record = (t < 60); // evaluates to true boolean both = pass && record; if( both ){ msg = “You passed in record time!”; } // always safer to include { } CS-1010 Dr. Mark L. Hornick 14

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

Looping and Repetition Statements control a block of code to be executed for: a fixed number of times l 1, 10000… until a certain condition is met l l l a value remains positive (or negative) a value remains below (or above) some limit … SE-1010 Dr. Mark L. Hornick 15

The two (three) main loop types: while() and it’s cousin do…while() l Sentinel-controlled loops,

The two (three) main loop types: while() and it’s cousin do…while() l Sentinel-controlled loops, where the loop body is executed repeatedly until a designated condition, called a sentinel, is encountered. for() l A count-controlled loop, where the loop body is executed a fixed number of times. SE-1010 Dr. Mark L. Hornick 16

The do-while() Statement Format: do { <statements> } while (<boolean expression>); The <statements> part

The do-while() Statement Format: do { <statements> } while (<boolean expression>); The <statements> part is known as the loop body The loop body is executed until the <boolean expression> becomes false l And is always executed at least once! Even if <boolean expression> is false SE-1010 Dr. Mark L. Hornick 17

The do-while() statement l Demo SE-1010 Dr. Mark L. Hornick 18

The do-while() statement l Demo SE-1010 Dr. Mark L. Hornick 18

The while Statement Format: while ( <boolean expression> ){ <statements that repeat> // 0

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 SE-1010 Dr. Mark L. Hornick 19

JOption. Pane Confirmation dialog Created by using the show. Confirm. Dialog method of the

JOption. Pane Confirmation dialog Created by using the show. Confirm. Dialog method of the JOption. Pane class: int answer = JOption. Pane. show. Confirm. Dialog(null, “Play another game? ", “Confirmation", JOption. Pane. YES_NO_OPTION, JOption. Pane. QUESTION_MESSAGE); // answer will contain the values corresponding // to JOption. Pane. YES_OPTION or JOption. Pane. NO_OPTION SE-1010 Dr. Mark L. Hornick 20