Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 2. Program Construction in Java. 2.4 Selection (decisions)"— Presentation transcript:

1 1 2. Program Construction in Java

2 2.4 Selection (decisions)

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

4 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.

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

6 6 The if...else statement ‣ if (condition_to_test) { statements to execute if true; } ‣ else { statements to execute if false; }

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

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

9 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

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

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

12 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.

13 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.

14 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; } }

15 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...

16 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; }

17 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.


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

Similar presentations


Ads by Google