Presentation is loading. Please wait.

Presentation is loading. Please wait.

If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.

Similar presentations


Presentation on theme: "If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean."— Presentation transcript:

1 if…else statements

2 Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean

3 Boolean Expressions Relational operator - an operator used for comparison of data items of the same type OperatorMeaning = =Is equal to <Is less than >Is greater than <=Is less than or equal to >=Is greater than or equal to !=Is not equal to

4 Boolean Expressions Examples: 7 = = 7//true -3.0 = = 0.0//false 4.2 > 3.7//true -18 < -15//true -17 != -17//false ‘a’ <= ‘b’//true

5 Boolean Expressions I M P O R T A N T ! ! ! ! Do not confuse = with = = Remember: = is for assignment (equations) total = num1 + num2; = = is for comparison (Boolean expressions) 18 = = 17

6 Boolean Expressions Logical operator - an operator used for comparison of Booleans OperatorMeaning && and || or ! not

7 Rules for && (and) A n &&(and) is only TRUE when both parts are TRUE. Otherwise, it is FALSE. pqp && q true false truefalse

8 Rules for || (or) An ||(or) is only FALSE when both parts are FALSE. Otherwise, it is TRUE. pqp || q true falsetrue falsetrue false

9 Boolean Expressions Order of Precedence: 1. ( ) 2. ! 3. *, /, % 4. +, - 5., >=, = =, != 6. && 7. ||

10 Boolean Expressions - Examples (3 < 5) || (21 < 18) TRUE || FALSE TRUE

11 Boolean Expressions - Examples !((7< 3) && (6 = = (3 + 3))) !((7 < 3) && (6 == 6)) !( FALSE && TRUE) !(FALSE) TRUE

12 Boolean Expressions - Examples ((24 % 7) == 3) && (3.1 <= 6.7) (3 ==3) && (3.1< = 6.7) TRUE && TRUE TRUE

13 if Statements Format of an if statement: statement1; if (Boolean expression) statement2; statement3; Notice - there is NO semi-colon on the line with the word if!!

14 if Statements Also notice that the statement after the if is indented. Statement2 is only executed when the Boolean expression is TRUE. It is skipped if the Boolean expression is FALSE.

15 if Statements Example: sum = 2.5; if (sum <= 3) System.out.println ( “The sum is less than three.”); System.out.println ( “Example of a simple if statement.”); -------------------------------------------------------- The sum is less than three. Example of a simple if statement.

16 if Statements When more than 1 statement needs to be executed, a compound if statement is used. The multiple statements are enclosed within curly braces. Without the curly braces, only the FIRST line after the if is considered.

17 if Statements Format: statement1; if (boolean expression) { statement2; statement3; etc. } statement4;

18 if Statements Example: grade = 9; if (grade < 12) { grade = grade + 1; System.out.println(“Next year, you will be in “ + grade); } -------------------------------------------------------------- Next year, you will be in 10

19 if … else statements Format of an if…else statement: statement1; if (Boolean expression) statement2; else statement3; statement4;

20 if … else statements Once again, notice that the statements after the if and else are indented. And notice there is no semi-colon after the else. Statement2 is only executed when the Boolean expression is TRUE. If the Boolean expression is FALSE, statement3 is executed.

21 if … else statements sum = 4.5; if (sum <= 3) System.out.println( “The sum is less than or equal to three”); else System.out.println( “The sum is greater than three”); -------------------------------------------------------- The sum is greater than three


Download ppt "If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean."

Similar presentations


Ads by Google