1 2. Program Construction in Java. 2.4 Selection (decisions)

17
1 2. Program Construction in Java

Transcript of 1 2. Program Construction in Java. 2.4 Selection (decisions)

Page 1: 1 2. Program Construction in Java. 2.4 Selection (decisions)

1

2. Program Construction in Java

Page 2: 1 2. Program Construction in Java. 2.4 Selection (decisions)

2.4 Selection (decisions)

Page 3: 1 2. Program Construction in Java. 2.4 Selection (decisions)

3

Making decisions

•In programming, making decisions involves evaluating a logical (boolean) expression (i.e. questions with the answer true or false) and passing control to different branches depending on the answer.

Page 4: 1 2. Program Construction in Java. 2.4 Selection (decisions)

4

Making decisions

•A human equivalent is to evaluate the statement it is raining:

‣ if true we go to the cinema,

‣ if false we go for a picnic.

•Such decisions, where program flow could go either way are called selection.

Page 5: 1 2. Program Construction in Java. 2.4 Selection (decisions)

5

The if statement

‣ if (condition_to_test){ statements to execute if true;}

Page 6: 1 2. Program Construction in Java. 2.4 Selection (decisions)

6

The if...else statement

‣ if (condition_to_test){ statements to execute if true;}

‣ else{ statements to execute if false;}

Page 7: 1 2. Program Construction in Java. 2.4 Selection (decisions)

7

if..else if..else

‣ if (condition_to_test) { statements to execute if true;}

‣ else if (other_condition_to_test) { statements to execute if true;}

‣ else { statements to execute if false;}

Page 8: 1 2. Program Construction in Java. 2.4 Selection (decisions)

8

if..else if..else

•You can use as many else if blocks as you wish, but if this gets complex, consider the switch statement (see later).

Page 9: 1 2. Program Construction in Java. 2.4 Selection (decisions)

9

Boolean expressions

•The condition_to_test takes the form of a boolean expression , i.e. one that has the value true or false, e.g.

‣ count < 5 or

‣ taxRate == 17.5

Page 10: 1 2. Program Construction in Java. 2.4 Selection (decisions)

10

Boolean expressions

•if (a < b) (if a is less than b),

•if (a <= b) (if a is less than or equal to b),

•if (a == b) (if a is equal to b, note the two signs),

•if (a > b) (if a is greater than b).

Page 11: 1 2. Program Construction in Java. 2.4 Selection (decisions)

11

Boolean expressions

•if (a >= b) (if a is greater than or equal to b)

•if (a != b) (if a is not equal to b)

•if (!a) (if a is not true) (where a is a boolean variable)

•if (pass) (where pass is a boolean variable).

Page 12: 1 2. Program Construction in Java. 2.4 Selection (decisions)

12

AND and OR

•Expressions can be combined using && (AND) and || (OR):

‣ if (a < b && c == d) means both must be true,

‣ if (a < b || c == d) means either can be true.

•& is the ampersand, | the break bar.

Page 13: 1 2. Program Construction in Java. 2.4 Selection (decisions)

13

Comparing Strings

•Remember, Strings are not primitives, you cannot use ==, > or <.

•s1.equals(s2) returns a boolean true or false.

•s1.compareTo(s2) returns -1 if s1 comes before s2 alphabetically, 0 if they are equal and +1 if it is after.

Page 14: 1 2. Program Construction in Java. 2.4 Selection (decisions)

14

Nesting•It is syntactically correct to nest as many if blocks inside other as you wish:

‣ if (condition_to_test){ if (another_condition_to_test) { statements to execute if both conditions are true; }}

Page 15: 1 2. Program Construction in Java. 2.4 Selection (decisions)

15

The switch statement

•An alternative to the multi-way if is the switch statement:

‣ switch (name_of_variable){ case 'A: ...statements to execute if the variable's value is A... break;

‣ continued...

Page 16: 1 2. Program Construction in Java. 2.4 Selection (decisions)

16

The switch statement

‣ case 'B: ...statements to execute if the variable's value is B... break; case 'C: ...statements to execute if the variable's value is C... break;}

Page 17: 1 2. Program Construction in Java. 2.4 Selection (decisions)

17

The switch statement

•Note the whole switch block is enclosed by one pair of braces.

•The different cases are followed by colons (:).

•The break; line jumps control to the end of the block.