Full Transcript

Selection Structures if – else structure Selection Selection - one group of statements is selected from several available groups Syntax : The if Statement if(condition) statement; Example: if (amount <= balance) { balance = balance - amount; } Purpose: To execute a statement when a condition is...

Selection Structures if – else structure Selection Selection - one group of statements is selected from several available groups Syntax : The if Statement if(condition) statement; Example: if (amount <= balance) { balance = balance - amount; } Purpose: To execute a statement when a condition is true Flow chart for if statement Conditional exp false true statements Flow chart for if statement amount <= balance false true balance=balance-amount; if-else selection • Syntax: if (expression) statement1; else statement2; • If the expression is true, then the target of the if will execute, and the else portion will be skipped • If the expression is false, then the target of the if is bypassed and the target of the else will execute • Under no circumstances will both statements execute • The addition of the else provides a two-way decision path The if-else statement • The if statement performs an indicated action only when the condition is true. • With an if-else you have the choice of executing one or other of two statements – Different actions are to be performed when the condition is true than when the condition is false Syntax : The if/else Statement if (condition) statement1; else statement2; Example: if (balance < 0) { System.out.print(“Your account is overdrawn”); } else { System.out.print(“Your account is in credit”); } Purpose: To execute a statement when a condition is true or false Flow chart for if/else statement false statement2 Conditional exp true statement1 Flow chart for if/else statement “In credit” false balance < 0 true “overdrawn” Sample Program import java.util.Scanner; public class IfElseTest { public static void main(String[] args) { Scanner keyboardIn = new Scanner(System.in); double accBalance; //Ask user to input Balance System.out.print("Enter Account balance: "); balance = keyboardIn.nextDouble(); //determine if overdrawn or not if(balance < 0) { System.out.println("You are overdrawn"); } else { System.out.println("Your balance is : " +balance); } }//end main method }//end class Sample Code if (grade >= 40) { System.out.println("Passed."); } else { System.out.println("Failed."); } • If grade is 35, what is the output? • Examines the value of grade variable. – If the value of grade is 40 or above the message Passed is displayed to screen – Otherwise the message Failed is displayed to screen Sample Code //determine price depending on age if(age < 12 ) { ticketPrice = 3.50; } else { ticketPrice = 8.00; } //rest of program System.out.print("Amount Due: "+ticketPrice); • If age is 20 what price is the ticket? • Examines the value of age variable. – If the value of age is less than 12 the ticketprice is set to €3.50 – Otherwise the ticketprice is set to €8.00

Use Quizgecko on...
Browser
Browser