Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *

Similar presentations


Presentation on theme: "Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *"— Presentation transcript:

1

2 Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius * radius * PI; System.out.println(“circle radius ”+radius+“ has area ”+area); } if-statements allow conditional execution of commands (you can execute different pieces of code in different situations) For example, in the ComputeArea program (2 nd chapter lectures) we could improve the program as follows

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

4 Comparison (relational) operators (Liang 42..48) 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’

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

6 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);

7 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

8 More complex boolean expressions (Liang 43..44) 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.

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

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

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

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

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

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

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

16 Sample if...else if (radius >= 0) { area = radius * radius * PI; System.out.println("Area is" + area); } else { System.out.println("Negative input!"); }


Download ppt "Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *"

Similar presentations


Ads by Google