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

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
Chapter 4: Control Structures I Instructor: Mohammad Mojaddam
Chapter 6 Horstmann Programs that make decisions: the IF command.
03 Data types1June Data types CE : Fundamental Programming Techniques.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 4: Control Structures I (Selection)
06 Testing selection1June Testing selection CE : Fundamental Programming Techniques.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
07 Further testing1June Further selection CE : Fundamental Programming Techniques.
11 Methods1June Methods CE : Fundamental Programming Techniques.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Logical Operators and Conditional statements
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Chapter Making Decisions 4. Relational Operators 4.1.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 4: Control Structures I (Selection)
Control Statements: Part1  if, if…else, switch 1.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Data Types and Expressions
EGR 2261 Unit 4 Control Structures I: Selection
Java Programming Loops
Chapter 4 Selection.
Visual Basic – Decision Statements
SELECTIONS STATEMENTS
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

05 Simple selection1June Simple selection CE : Fundamental Programming Techniques

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

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

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

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

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

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

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

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

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

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

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"

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

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

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

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"); }

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"); }

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

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

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

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

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"))...

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

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

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