Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions."— Presentation transcript:

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

2 CS-1010 Dr. 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(…) … }

3 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 } }

4 CS-1010 Dr. 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)

5 CS-1010 Dr. 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 false boolean isOK = false; boolean insufficientFunds = true; boolean, true, and false are Java reserved words

6 CS-1010 Dr. 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

7 CS-1010 Dr. 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 ){ … }

8 CS-1010 Dr. 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 ( ) // executed if true // 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(…)

9 CS-1010 Dr. 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(…);

10 CS-1010 Dr. Mark L. Hornick 10 A more complex if statement if ( ){ // executed when exp. is true } else { }// 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

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

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

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

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

15 SE-1010 Dr. 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 …

16 SE-1010 Dr. 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.

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

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

19 SE-1010 Dr. Mark L. Hornick 19 The while Statement Format: while ( ){ // 0 or more } As long as the is true, the loop body is executed

20 SE-1010 Dr. 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


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

Similar presentations


Ads by Google