07 Further testing1June 15 07 Further selection CE00858-1: Fundamental Programming Techniques.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Logic & program control part 3: Compound selection structures.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
06 Testing selection1June Testing selection CE : Fundamental Programming Techniques.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
05 Simple selection1June Simple selection CE : Fundamental Programming Techniques.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
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
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
The switch statement: an N-way selection statement.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
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.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Chapter 05 (Part III) Control Statements: Part II.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
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 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
13 Arrays CE : Fundamental Programming Techniques June 161.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CNG 140 C Programming (Lecture set 3)
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Control Statement Examples
More Selections BIS1523 – Lecture 9.
Alternate Version of STARTING OUT WITH C++ 4th Edition
SELECTION STATEMENTS (2)
Chapter 7 Conditional Statements
Topics 4.1 Relational Operators 4.2 The if Statement
Lecture Notes – Week 2 Lecture-2
Conditionals.
Chapter 13 Control Structures
Presentation transcript:

07 Further testing1June Further selection CE : Fundamental Programming Techniques

07 Further testing2June 15 Objectives In this session, we will: look at the ternary operator and when it should be used see how if statements are used in validation use the switch statement to handle mutually exclusive choices

07 Further testing3June 15 Ternary operator example: Morning alternative notation for if else construction if exp1 is true, exp2 is executed, otherwise exp3 is executed only use where it doesn't reduce readability problem: output greeting based on time: prompt user for hour in 24-hour time format either output "Good morning" or "Good afternoon" exp1 ? exp2 : exp3

Morning analysis what data is used? hour: positive integer in range 0 – 23 input by user need selection as times in morning handled differently from those in afternoon what operations are done before the selection? create Scanner prompt user for hour input hour what operations are done for times in morning? output "Good morning" what operations are done for times not in the morning? output "Good afternoon" what operations are done after the selection? none 07 Further testing4June 15

07 Further testing5June 15 Morning code //output appropriate greeting Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter hour: "); int hour = myKeyboard.nextInt(); //output greeting System.out.println("Good " + (hour < 12 ? "morning" : "afternoon")); Morning.java output if condition true output if condition false

07 Further testing6June 15 Data validation in many applications it is necessary to validate data only perform any calculations if the data is valid validation involves selection ensuring data is correct type (not covered here) ensuring data is in range ensuring data is specific value

07 Further testing7June 15 Data in range example – Percentage either check for valid data being within range: or check for invalid data being outside range: if (percentage 100) //report invalid data else //process valid data if (percentage >= 0 && percentage <= 100) //process valid data else //report invalid data Percentage.java

07 Further testing8June 15 Data specific type example – Gender can only check for valid data being specific type: can split check and use else for invalid data: if (gender.equals("Male") || gender.equals("Female")) //process valid data else //report invalid data if (gender.equals("Male")) // process male else if (gender.equals("Female")) //process female else //report invalid data Gender.java

07 Further testing9June 15 Switch statement in Java alternative to if for mutually exclusive options comprises a single expression and any number of alternative cases statements expression: evaluated then matched against case statements must be byte, short, int or char case statements: each case statement must be a unique literal first case statement that matches is executed can optionally have a default case that is executed if no others match break: break statement is used to end the case statement

07 Further testing10June 15 Switch advantages and disadvantages although any switch statement could be implemented as an if statement, using switch has benefits: reduction in the amount of coding required improved readability of the code simplified logic but... choice must be of type byte, short, int or char easy to miss out one of the breaks

07 Further testing11June 15 Switch syntax switch ( choice ) { case 0: statement0; break; case 1: statement1; break; case 2: statement2; break; default: statement3; } statement0 executed if choice = 0 executed if no cases match used to terminate statements for case 0

07 Further testing12June 15 Switch example – DayNumber problem: output the day name based on its number 1 is Saturday 2 is Sunday etc. output message if invalid day input

07 Further testing13June 15 DayNumber analysis what data is used? day: integer in the range 1 – 7 input by user need selection as different message output for each day what operations are done before the selection? create Scanner prompt user for day input day what operations are done if day is 1? output "Saturday" what operations are done if day is 2? output "Sunday" similar for remaining days what operations are done if day is none of the above? output "Invalid day number" what operations are done after the selection? none Note the design doesn’t specify that switch is to be used

07 Further testing14June 15 //output day based on number Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter day, where 1 is Saturday: "); int day= myKeyboard.nextInt(); switch (day) { case 1: System.out.println("Saturday"); break; case 2: System.out.println("Sunday"); break; //similar code for other cases case 7: System.out.println("Friday"); break; default: System.out.println("Invalid data"); } executed if all conditions false DayNumber.java executed if day is 1 used to break out of switch statement

07 Further testing15June 15 Switch with fall through case can be followed by as many statements as required before break break marks the end of the case if break is omitted, execution falls through into the next case

07 Further testing16June 15 Switch fall through example – GradePoint problem: output a description of a Grade Point: GP 0 - 1:Uncompensatable fail GP 2 - 3:Compensatable fail GP 4 - 6:Third class GP 7 - 9:Lower second class GP :Upper second class GP :First class

07 Further testing17June 15 GradePoint analysis what data is used? grade point: integer in the range 0 – 15 input by user need selection as grade points fall into different categories what operations are done before the selection? create Scanner prompt user for grade point input grade point what operations are done if the grade point is 0 or 1? output "Non-compensatable fail" what operations are done if the grade point is 2 or 3? output "Compensatable fail" similar for other grade points what operations are done if the grade point is none of the above? output "Invalid grade point" what operations are done after the selection? none

07 Further testing18June 15 //output explanation of grade point Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter grade point: "); int gp = myKeyboard.nextInt(); switch (gp) { case 0: case 1: System.out.println("Uncompensatable fail"); break; case 2: case 3: System.out.println("Compensatable fail"); break; //similar code for other cases case 13: case 14: case 15: System.out.println("First class"); break; default: System.out.println("Invalid data"); } GradePoint.java executed if gp is 0 or 1 used to break out of switch statement

07 Further testing19June 15 Switch using characters - Capital the selector in a switch statement can also be a variable of type char the character may need to be extracted from a string before being used problem: input the name of a UK country (England, Wales, Scotland or Ireland) output the capital of that country assume all data is valid:

07 Further testing20June 15 Capital analysis what data is used? countryString: String country input by user countryLetter: char first letter extracted from countryString need selection as each country has a different capital what operations are done before the selection? create Scanner prompt user for country input countryString extract first character what operations are done if the first character is 'E'? output "London" what operations are done if the first character is 'W'? output "Cardiff" what operations are done if the first character is 'S'? output "Edinburgh" what operations are done if the first character is 'I'? output "Dublin" what operations are done after the selection? none

07 Further testing21June 15 //output capital of UK country Scanner myKeyboard = new Scanner(System.in); //input country and get first letter System.out.print("Enter country: "); String countryString = myKeyboard.next(); char countryLetter = countryString.charAt(0); switch (countryLetter) { case 'E': System.out.println("London"); break; case 'W': System.out.println("Cardiff"); break; case 'S': System.out.println("Edingburgh"); break; case 'I': System.out.println("Dublin"); } Capital.java

07 Further testing22June 15 Testing switch switch is used to implement multiple selection must check each path through switch if fall through is used, need to check all possible cases

07 Further testing23June 15 Summary In this session we have: used the ternary operator validated input using if statements implemented switch statements and seen how break is used to control execution In the next session we will: introduce iteration