Presentation is loading. Please wait.

Presentation is loading. Please wait.

05 Simple selection1June 15 05 Simple selection CE00858-1: Fundamental Programming Techniques.

Similar presentations


Presentation on theme: "05 Simple selection1June 15 05 Simple selection CE00858-1: Fundamental Programming Techniques."— Presentation transcript:

1 05 Simple selection1June 15 05 Simple selection CE00858-1: Fundamental Programming Techniques

2 05 Simple selection2June 15 Objectives In this session, we will: introduce selection statements use relational and logical operators in conditions see the Java syntax for If statements implement one-way and two-way selections look at common mistakes in selection statements use string comparison methods

3 05 Simple selection3June 15 Control structures any program only uses: sequence selection repetition so far only written programs using simple sequences each statement performed in order

4 05 Simple selection4June 15 Selection take different actions depending on certain conditions condition checks current circumstances actions are performed under those circumstances decide whether all situations are dealt with in the same way any actions done to all items are kept outside of the selection statement if the petrol gauge in the car is on red, then stop at the next service station and buy more petrol condition action

5 Analysis of selection when faced with a new specification we need to decide whether all data is dealt with in the same way if it is not, there will normally be a selection in the program if a program has a selection in it, need to consider: what data is used? what operations are done before the selection? what operations are done for each possibility? what operations are done after the selection? to decide on the possibilities we need to split the data into groups that are processed identically 05 Simple selection5June 15

6 05 Simple selection6June 15 Conditions compare at least one value against another use relational operators may combine results of several comparisons using logical operators

7 05 Simple selection7June 15 Relational operators checking one thing against another ==equal to !=not equal to >greater than <less than >=greater than or equal to <=less than or equal to used for integers, floating points and char avoid checking equality on floating point due to rounding errors

8 05 Simple selection8June 15 If statements in Java if (condition) { statement; } else { statement; } condition must be enclosed by round brackets if condition is true the next single statement is executed braces are needed to block more than one statement else is optional indentation is used to show program structure

9 05 Simple selection9June 15 One-way selection example – Discount performs actions when a condition is true problem: process order discounts: prompt user for number of items to order and calculate cost of order at 50p per item if order more than 100 apply 10% discount output amount to be paid

10 05 Simple selection10June 15 Discount analysis what data is used? quantity: positive integer input by user cost: double calculated by program need selection as quantity over 100 has discount what operations are done before the selection? create Scanner prompt user for quantity input quantity calculate cost: quantity * 0.5 (double) what operations are done if quantity over 100? apply 10% discount what operations are done if quantity 100 or less? none what operations are done after the selection? output cost

11 05 Simple selection11June 15 Discount code //process order discounts Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter quantity: "); int quantity = myKeyboard.nextInt(); double cost = quantity * 0.5; //apply 10% discount if more than 100 items ordered if ( quantity > 100 ) { cost = cost * 0.9; } System.out.println("Cost of order: " + cost); only executed if condition true Discount.java

12 05 Simple selection12June 15 Two-way selection example – BankBalance performs one set of actions if condition is true and another if false problem: check user’s bank balance: if overdrawn output "overdrawn", otherwise output "in credit"

13 05 Simple selection13June 15 BankBalance analysis what data is used? balance: double input by user need selection as different messages output based on balance what operations are done before the selection? create Scanner prompt user for balance input balance what operations done if balance less than 0? output "overdrawn" what operations done if balance 0 or over? output "in credit" what operations are done after the selection? none

14 05 Simple selection14June 15 BankBalance code //check user’s bank balance Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter balance: "); double balance = myKeyboard.nextDouble(); //output message giving status of account if ( balance < 0.0 ) { System.out.println("Overdrawn"); } else { System.out.println("In credit"); } executed if condition true executed if condition false BankBalance.java

15 05 Simple selection15June 15 Common mistake 1 problem... int num1 = 3; if ( num1 < 0 ) System.out.println("num1 is " + num1); System.out.println("num1 is negative");

16 05 Simple selection16June 15 Common mistake 2 problem... int num1 = 3; if ( num1 < 0 ) ; { System.out.println("num1 is " + num1); System.out.println("num1 is negative"); }

17 05 Simple selection17June 15 Common mistake 3 problem... int num1 = 3; if ( num1 = 0 ) { System.out.println("num1 is " + num1); System.out.println("num1 is negative"); }

18 05 Simple selection18June 15 Logical operators used to combine results of different tests brackets can be used to clarify meaning ! NOT - opposite of original expression && AND - true if both expressions true || OR - true if either expression true each condition part must be correctly formed

19 05 Simple selection19June 15 Logical operators example – Divisible problem: output if number is divisible by 3 and 5 analysis: what data is used? number: integer input by user selection needed as message only output for numbers divisible by 3 and 5 what operations are done before the selection? create Scanner prompt user for number input number what operations are done if the number is divisible by 3 and 5? output "divisible by 3 and 5" what operations are done if the number is not divisible by 3 and 5? none what operations are done after the selection? none

20 05 Simple selection20June 15 Divisible code //check divisible by 3 and 5 Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter number: "); int number = myKeyboard.nextInt(); //check whether number is divisible by 3 and by 5 if (( number % 3 == 0 ) && (number % 5 == 0)) { System.out.println("Divisible by 3 and 5"); } Divisible.java

21 05 Simple selection21June 15 Checking for ranges && and || can be used to check if a value is in range: //check whether person can not go on 18 – 35 holiday if (( age 35)) { System.out.println("You've missed out!"); } //check whether person can go on 18 – 35 holiday if (( age >= 18 ) && (age <= 35)) { System.out.println("Have a great holiday!"); } check for value in range check for value out of range

22 05 Simple selection22June 15 Comparing strings capital letters precede lower case cannot use relational operators on Strings need to use methods in String class to check if strings are equal: to check if strings are equal, regardless of case: to check if a string ends with another string: if (greeting.equals("Hello"))... if (greeting.equalsIgnoreCase("HeLLo"))... if (greeting.endsWith("lo"))...

23 05 Simple selection23June 15 Comparing strings example – Access problem: output message granting access based on user type user is either staff or student analysis: what data is used? userType: string input by user, either "staff" or "student" selection needed as staff and students access different facilities what operations are done before the selection? create Scanner prompt user for userType input userType what operations are done if userType is "staff" output "Access to staff facilities" what operations are done if userType is not "staff" output "Access to student facilities" what operations are done after the selection? none

24 05 Simple selection24June 15 Access code //output message granting access dependent on user type Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter user type: "); String userType = myKeyboard.nextLine(); //check user type if (userType.equalsIgnoreCase("staff")) { System.out.println("Access to staff facilities"); } else { System.out.println("Access to student facilities"); } Access.java

25 05 Simple selection25June 15 Summary In this session we have: implemented simple selection statements used relational and logical operators implemented string comparison using methods In the next session we will: look at testing selection statements


Download ppt "05 Simple selection1June 15 05 Simple selection CE00858-1: Fundamental Programming Techniques."

Similar presentations


Ads by Google