Week 3 If Else If Else.pdf
Document Details
Uploaded by SimplifiedLaplace
Tags
Full Transcript
Selection Structures if – else if- else structure Control Structures 1. Sequence 2. Selection – if – if / else – else / if – Switch 3. or Iteration/repetition if/else ladder Sample Program 1 /* Program to illustrate the use of if else statements */ import java.util.*; public class PosNeg...
Selection Structures if – else if- else structure Control Structures 1. Sequence 2. Selection – if – if / else – else / if – Switch 3. or Iteration/repetition if/else ladder Sample Program 1 /* Program to illustrate the use of if else statements */ import java.util.*; public class PosNegTester1 { public static void main(String[] args) { Scanner keyboardIn = new Scanner(System.in); int number; System.out.print("Please enter an integer value: number = keyboardIn.nextInt(); } } ") ; if ( number > 0 ) { System.out.print(number + " is a positive value ") ; } else { System.out.print(number + " is a negative value ") ; } What is the output if number is 0? Sample Program 2 /* Program to illustrate the use of else/if statements */ import java.util.*; public class PosNegTester2 { public static void main(String[] args) { Scanner keyboardIn = new Scanner(System.in); int number; System.out.print("Please enter an integer value: number = keyboardIn.nextInt(); ") ; if(number > 0 ) { System.out.print(number + " is a positive value ") ; } else if (number < 0 ) { System.out.print(number + " is a negative value ") ; } else { System.out.print("Number entered is zero") ; } } //end main method Alternative Program 2 if(number == 0 ) { System.out.print("Number entered is zero") ; } else if (number < 0 ) { System.out.print(number + " is a negative value ") ; } else { System.out.print(number + " is a positive value ") ; } else-if The expressions are evaluated in order: if any expression is true, the statement associated with if is executed and it terminates the chain. if (expression) statement; else if (expression) statement; else if (expression) statement; else statement; The last else handle none of the above. • The conditional expressions are evaluated from the top down • Once a true condition is found, the statement associated with it is executed and the rest of the ladder is bypassed • If none of the conditions is true, the final else statement will be executed • The final else acts as a default condition • i.e. if all other conditional tests fail, the last else statement is executed • If there is no final else and all other conditions are false, then no action will take place • The final else is optional import java.util.Scanner; public class ExamGrade { public static void main(String[] args) { Scanner keyboardIn = new Scanner(System.in); int mark; char grade; System.out.print( "Please enter an exam mark ") ; mark = keyboardIn.nextInt(); if ( mark >= 70 ) { grade = 'A' ; } else if ( mark >= 60 ) { grade = 'B' ; } else if ( mark >= 50 ) { grade = 'C' ; } else if ( mark >= 40 ) { grade = 'D' ; } else if ( mark >= 30 ) { grade = 'E' ; } else{ Multiple Alternatives: Sequences of Comparisons • if (condition1) statement1; else if (condition2) statement2; . . . else statement4; • The statement associated with first true condition is executed • Order matters if ( mark >= 0 grade = 'F' else if ( mark grade = 'E' else if ( mark . . . . ) //is always true ; >= 30 ) //never executes!! ; >= 40 ) Consider the code: if ( mark >= 0 grade = 'F' else if ( mark grade = 'E' else if ( mark grade = 'D' else if ( mark grade = 'C' else if ( mark grade = 'B' else grade = 'A' ) ; >= ; >= ; >= ; >= ; 30 ) 40 ) 50 ) 60 ) ; How could I rewrite this code????? Alternative comparisons if ( mark < 30 grade = 'F' else if ( mark grade = 'E' else if ( mark grade = 'D' else if ( mark grade = 'C' else if ( mark grade = 'B' else grade = 'A' ) ; < ; < ; < ; < ; ; 40 ) 50 ) 60 ) 70 ) else/if flowchart expr1 true statement1 false expr2 true statement2 false expr3 false statement4 true statement3 if(expr1) statement1; else if(expr2) statement2; else if(expr3) statement3; else statement4; Logical operators • Conditions often have more than one criteria. • Logical operators are used to allow more than one criteria in a conditional statement. • Example: – Cinema ticket system – Teenagers pay less than full price – This condition has more than one criteria (age must be more than 12 AND age must be less than 20) Logical Operators Logical Operators Tests for Example && AND Condition A && Condition B Return true if both condition A and B are true, otherwise return false || OR Condition A || Condition B Return true if either Condition A or B is true for if both Condition A and B are true ! NOT !A If Condition A is true, result of this operation is false If Condition A is false, result of this operation is true Example - && if(age > 12 && age < 20) { System.out.println("You are a teenager"); } • Here both conditions must be true for the message to be printed on screen • If either condition is false, the message will not be printed. Example - || if(age < 12 || age > 65) { System.out.println("Admission is free"); } • Here a minimum of one condition must be true for the message to be printed on screen • If both conditions are false, the message will not be printed. Truth Table for logical operators Operands p q p && q Results p || q !p F F F F T F T F T T T F F T F T T T T F Where p and q are expressions that evaluate to TRUE or FALSE Order of evaluation • When Java sees a && operator or a ||, the expression on the left side of the operator is evaluated first. int num1 = -2; int num2 = 4; if(num1 > 0 && num2 <100) … • In this example, Java first checks to see if num1 > 0 is true. Here this is false, so the entire condition must be false regardless of whether num2 <100 is true or not, so Java doesn't bother checking num2 <100. Order of evaluation int num1 = 2; int num2 = 4; if(num1 > 0 || num2 <100) … • Again, Java first checks to see if num1 > 0 is true. Here this is true, so the entire condition must be true regardless of whether num2 <100 is true or not, so Java doesn't bother checking num2 <100. • This is known as short-circuit evaluation. Spot the difference if(caResult >= 40 && examResult >=40) { System.out.print("You have passed"); } if(caResult >=40 || examResult >=40) { System.out.print("You have passed"); } Exercise Write expressions for each of the following conditions • A value between 0 and 10 • A value between 0 and 100 or value is negative • A weight is greater than 50 pounds or height is greater than 60 inches Exercise Answers • A value between 0 and 10 value > = 0 && value <= 10 • A value between 0 and 100 or value is negative (value > 0 && value <100) || value < 0 • A weight is greater than 50 pounds or height is greater than 60 inches weight > 50 || height > 60 True or False? int num1, num2; num1 = 15; num2 = 20; num1 > 16 && num1<25 num1 < 1 && num1 < 25 num2>=20 || num2<=25 num2>=20 && num2<=25 num1 != num2 (!(num2>=20) && (num2 <=25)) True or False? int h = 0, j = 7, k = 1, n = -1; j > h && j < k j > h || j < k j > 0 && j < h || j > k j < h|| h < k && j < k