Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Horstmann Programs that make decisions: the IF command.

Similar presentations


Presentation on theme: "Chapter 6 Horstmann Programs that make decisions: the IF command."— Presentation transcript:

1

2 Chapter 6 Horstmann Programs that make decisions: the IF command

3 Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare integers, floating- point numbers, strings, and objects To recognize the correct ordering of decisions in multiple branches To program conditions using Boolean operators and variables

4 The if Statement The if statement lets a program carry out different actions depending on a condition Continued… if (amount <= balance) { balance = balance - amount; }

5 The if Statement Figure 1: Flowchart for an if statement

6 The if/else Statement Continued… if (amount <= balance){ balance = balance - amount; } else{ balance = balance - OVERDRAFT_PENALTY; }

7 If statements and Boolean expressions An expression whose value is true or false is a boolean expression A simple if-statement has the following form: if ( some boolean expression ) { // do this if the boolean works out true } Where I’ve typed some boolean expression above, you can have any expression that returns a value of either true or false (e.g. amount >= 0, which is either true or false ). There must be brackets around this expression. else { // do this otherwise } The else part is optional: you can stop here if you want the if but don’t need the else

8 Comparison (relational) operators int x = 5; To use the if statement, we need to do comparisons (to find out if something is true or false). These comparisons are all boolean expressions. Now consider the value of each of the following expressions x == 5 // true or false? x < 5 // true? false? x != 5 // true? false? x >= 5 // true? false? Assume the following initial value: true : == means ‘check if these two numbers are equal’ false : < means ‘check if the 1 st number is less than the 2 nd ’ false : != means ‘check if these two numbers are NOT equal’ true : >= means ‘check if the 1 st number is greater than or equal to the 2 nd number’

9 if ( ) { //whatever statements we want to carry out if the above is true } myNumber < 0myNumber >= 0myNumber != 0myNumber == 0 Comparison operator summary These comparison operators are used to compare two numbers (ints floats, doubles etc.) < means ‘less than’> Means ‘greater than’ <= means ‘less than or equal to’>= Means ‘greater than or equal to’ == means ‘equal to’!= Means ‘not equal to’ We can use any of these operators in an if statement. For example, suppose we’re comparing a number variable myNumber with 0: One equal sign means assignment (putting something in a variable). IMPORTANT: the equality operator == uses two equal signs, not one.

10 Expressions and statements These are statements............ double d = 1.5; System.out.println("Hello there"); In assignment statements, the Right Hand Side may contain an expression. An expression is something whose value has to be worked out. double d2 = 1.5 * 3.2; An expression has a value, which may be a number..... Or may be the boolean value true or false.... boolean b = (x < 3); b will hold the value of expression (x < 3);

11 Simple if-statements int x = 3; int y = 2; if (x >= 4) { y = y + 1; System.out.println("We're in the if-statement"); } // what value does y have now? Note that statements inside the if-statement are within a block, and are written with extra indentation

12 More complex boolean expressions True or false? (x < 5) (x 0) // && means ‘and’ (x 0) // || means ‘or’ !(x < 5) // ! means ‘not’ Assume x = 3 and y = -1. This works out to be true This works out to be false, because while (x 0) is false (the whole thing is only true if the first part is true AND the second part is true ) This works out to be true because (x 0) is false (the whole thing is true if the first part is true OR the second part is true ) This works out to be false. (x < 5) is true, so !(x < 5) (which means NOT (X < 5) ) is false.

13 The && operator ("and") (true) && true (true) (false)false (false) (true)false (false) false

14 The || operator ("or") (true) true (true) (false)true (false) (true)true (false) false ||

15 The ! operator ("not") ! truefalse true

16 Example of complex conditions if ((mark >= 0) && (mark <= 10)) { System.out.println(“mark is between 0 and 10"); } else { System.out.println(“error: mark not between 0 and 10"); } This would be used in a program for submitting student marks (for your assignments, for example). Notice there are no semi-colons (;) after the if line or the else line.

17 Another example boolean reducedFare = false; if ((age 65)) { reducedFare = true; } // later in the program.... if(reducedFare) { // do something appropriate }

18 if.....else if ( ) { // statements for the true case } else { // statements for the false case } How to do either one thing or the other:

19 Flowchart for if...else boolean expression false true statements for the true case statements for the false case next statement

20 Sample if...else Public void withdraw(double amount) { if (amount <= balance) { balance = balance - amount; } else { balance = balance - amount; balance = balance - OVERDRAFT_PENALTY; } If you withdraw more than the balance in your account, that amount is taken and you also get an overdraft penalty


Download ppt "Chapter 6 Horstmann Programs that make decisions: the IF command."

Similar presentations


Ads by Google