© The McGraw-Hill Companies, 2006 Chapter 2 Selection.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Loops – While, Do, For Repetition Statements Introduction to Arrays
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Fundamentals of Python: From First Programs Through Data Structures
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
First tutorial.
Chapter 3 Making Decisions
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Chapter 4 Selection Structures: Making Decisions.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Flow of Control Part 1: Selection
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.
© Jalal Kawash Programming Peeking into Computer Science 1.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 5: Structured Programming
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 5: Making Decisions
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Decision Statements, Short- Circuit Evaluation, Errors.
COMP Loop Statements Yi Hong May 21, 2015.
Decision Making and Branching
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Java Programming Fifth Edition
Week 4 - Monday CS 121.
JavaScript: Control Statements.
More Selections BIS1523 – Lecture 9.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Java Programming Control Structures Part 1
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

© The McGraw-Hill Companies, 2006 Chapter 2 Selection

© The McGraw-Hill Companies, 2006 Selection look back at the programs you have written so far and consider the order in which instruction were executed; you decided upon which instructions to include in your programs but the order in which these instructions were executed was not under your control; these instructions were always executed in sequence, that is one after the other, from beginning to end; you will soon find that this order of execution is too restrictive.

© The McGraw-Hill Companies, 2006 Making choices Very often you will want your programs to make choices among different groups of instructions. An example A program processing requests for airline tickets could have the following choices to make: –display the price of the seats requested; –display a list of alternative flights; –display a message saying that no flights are available to that destination. Selection, is a method of program control in which a choice can be made among which instructions to execute.

© The McGraw-Hill Companies, 2006 Implementing selection in Java In Java there are three forms of selection you can use: –an if statement; –an if…else statement; –a switch statement.

© The McGraw-Hill Companies, 2006 The ‘'if’' statement Sometimes one or more instructions need to be guarded so that they are executed only when appropriate; This particular form of selection is implemented by making use of Java’s if statement; The general form of an if statement is given as follows:

© The McGraw-Hill Companies, 2006 Tests A test is any expression that gives a boolean result of true or false. Examples –this password is valid; –there is an empty seat on the plane; –the temperature in the laboratory is too high. when the test gives a boolean result of true the instructions inside the braces of the if statement are executed; if the test gives a boolean result of false the instructions inside the if braces are skipped and not executed.

© The McGraw-Hill Companies, 2006 The ‘if’ statement: an example consider a temperature value that has been entered by the user; let us now check if the temperature is below freezing point (0 degrees) and display an appropriate message if it is; regardless of whether or not the temperature was freezing, we will then ask the user to enter another temperature; an if statement can be used to carry out this test:

© The McGraw-Hill Companies, 2006 The ‘if’ statement: an example continued

© The McGraw-Hill Companies, 2006 The ‘if’ statement: another example Consider once again the program that calculates the cost of a product. Now assume that a special promotion is in place for those products with an initial price over 100. For such products the company pays half the tax so, for the customer, the tax is effectively halved. Program 2.1 makes use of an if statement to apply this promotion, as well as informing the user that a tax discount has been applied.

© The McGraw-Hill Companies, 2006

Two sample runs of program 2.1 *** Product Price Check *** Enter initial price: 50 Enter tax rate: 10 Cost after tax = 55.0 *** Product Price Check *** Enter initial price: 1000 Enter tax rate: 10 Special Promotion: Your tax will be halved! Cost after tax = 1050

© The McGraw-Hill Companies, 2006 Comparison operators

© The McGraw-Hill Companies, 2006 Checking for equality note that a double equals (==) is used to check for equality in Java and not the single equals (=) which, as you know, is used for assignment. to use the single equals is a very common error! the following checks if an angle is a right angle:

© The McGraw-Hill Companies, 2006 The 'if…else’ statement the if statement allows us to build the idea of a single choice into our programs; the if...else statement allows us to build the idea of choose two alternative courses of action:

© The McGraw-Hill Companies, 2006 The 'if…else' statement: an example

© The McGraw-Hill Companies, 2006

Combining tests Often it is necessary to join two or more tests together to a create a single more complicated test. Example Assume that, for an experiment in the laboratory to be successful, the temperature must remain between 5 and 12 degrees celsius. This involves combining two tests together

© The McGraw-Hill Companies, 2006 Logical operators Symbols that join tests together to form longer tests are known as logical operators. Table 2.2 lists the Java counterparts to the three common logical operators.

© The McGraw-Hill Companies, 2006 Nested ‘if…else’ statements instructions within if and if…else statements can themselves be any legal Java commands; in particular they could contain other if or if…else statements; this form of control is referred to as nesting; nesting allows multiple choices to be processed.

© The McGraw-Hill Companies, 2006 Nested ‘if…else’ statements: an example

© The McGraw-Hill Companies, 2006

The 'switch‘ statement a switch can sometimes be used instead of a series of nested if…else statements; the general form of a switch statement in Java is given as follows:

© The McGraw-Hill Companies, 2006 When to use a ‘switch’ statement The switch statement works in exactly the same way as a set of nested if statements, but is more compact and readable. A switch statement may be used when –only one variable is being checked in each condition (in this case every condition involves checking the variable group); –the check involves specific values of that variable (e.g. 'A', 'B') and not ranges (for example >=40 ).

© The McGraw-Hill Companies, 2006 Re-writing Program 2.4 by using a ‘switch’ statement:

© The McGraw-Hill Companies, 2006

Combining options let’s assume that both groups A and C have a lab at 10.00a.m; the following switch statement could process this by grouping case 'A' and 'C' together: