***** SWTJC STEM ***** Chapter 5 cg 57 Selection Construct Choices 1.One-way selection - Use simple if.  2.Two-way selection - Use if - else.  3.if blocks.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Chapter 4: Making Decisions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 6 Horstmann Programs that make decisions: the IF command.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4: Control Structures: Selection
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Decision Structures and Boolean Logic
Computer Science Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth 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 J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 2. Program Construction in Java. 2.4 Selection (decisions)
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
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.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
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.
Chapter 5 The if Statement
Decisions Chapter 4.
A mechanism for deciding whether an action should be taken
Control Statements: Part 2
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Control Statement Examples
Topics The if Statement The if-else Statement Comparing Strings
Professor Jodi Neely-Ritz CGS3460
Chapter 4: Decision Structures and Boolean Logic
Flow Control Statements
Control Structure Chapter 3.
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Decisions, decisions, decisions
Chapter 4: Decision Structures and Boolean Logic
Controlling Program Flow
Control Structure.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

***** SWTJC STEM ***** Chapter 5 cg 57 Selection Construct Choices 1.One-way selection - Use simple if.  2.Two-way selection - Use if - else.  3.if blocks  4.Nested if blocks  5.Multi-way selection - Use either: 1.Differing conditions - Use if - else if - else if else. 2.Same variable or expression with multiple choices - Use switch.

***** SWTJC STEM ***** Chapter 5 cg 57 Multi-Way Selection Example else if Use else if construct for multiple conditions. Consider this example: “A value x is classified as x < 200low 200  x < 500medium 500  x < 800medium-high 800  xhigh Program classification output for a given value of x.”

***** SWTJC STEM ***** Chapter 5 cg 57 Multi-Way Selection Flowchart else if x<200? true false Entry Point Exit Point “low” x<500? false x<800? false “medium” “medium-high” “high” true

***** SWTJC STEM ***** Chapter 5 cg 57 Multi-way Selection Code else if int x = 250; if (x < 200) System.out.println("low"); else if (x < 500) System.out.println("medium"); else if (x < 800) System.out.println("medium-high"); else System.out.println("high"); MultiWayElseIf.java

***** SWTJC STEM ***** Chapter 5 cg 60 Multi-Way Selection Example switch Use switch when the decision is based on multiple values of a single variable. Consider this example: “A value x is classified as x = 1residential x = 2commercial x = 3non-profit otherwiseunclassified Code a program to output the classification for a given value of x.”

***** SWTJC STEM ***** Chapter 5 cg 60 Multi-Way Selection Flowchart switch x=1? true false Entry Point Exit Point “residential” x=2? false x=3? false “commercial” “nonprofit” “unclassified” true

***** SWTJC STEM ***** Chapter 5 cg 60 Multi-way Selection Code switch int x =2; switch (x) { case 1: System.out.println("residential"); break; // or return a value case 2: System.out.println("commercial"); break; // or return a value case 3: System.out.println("non-profit"); break; // or return a value default: System.out.println("unclassified"); // or return a value }... Continue Here MultiWaySwitch.java

Given a value, the MultiWayCalculator applies two factors to calculate a final value. The two factors are as follows: 1.The type factor is based on a the value type TypeType Factor The range factor is based on ranges of value Range Range Factor x <  x <  x <  x The final value is equal to value * type factor * range factor ***** SWTJC STEM ***** Chapter 5 cg 54 MultiWayCalculator Example

***** SWTJC STEM ***** Chapter 5 cg 60 MultiWayCalculator (UML) MultiWayCaculator value: double type: double setValue (double value): void get Value ( ): double setType (int value): void getType ( ): int -getTypeFactor ( ): double -getRangeFactor ( ): double getFinalValue: double) toString( ): String

public class MultiWayCalculator { private double value; private int type; public MultiWayCalculator(double value, int type) { this.value = value; this.type = type; } ***** SWTJC STEM ***** Chapter 5 cg 54 Declarations and Constructor MultiWayCalculator.java

public void setValue(double value) { this.value = value; } public double getValue() { return value; } public void setType(int type) { this.type = type; } public int getType() { return type; } ***** SWTJC STEM ***** Chapter 5 cg 54 Attribute Setters and Getters MultiWayCalculator.java

private double getTypeFactor () { switch (type) { case 0: return 1.5; case 1: return 2.0; case 2: return 2.5; default: return 0.0; } ***** SWTJC STEM ***** Chapter 5 cg 54 Private Getter for Type Factor MultiWayCalculator.java

private double getRangeFactor () { if (value < 200.) return 0.25; else if (value < 500.) return 0.375; else if (value < 800.) return 0.55; else return 0.85; } ***** SWTJC STEM ***** Chapter 5 cg 54 Private Getter for Range Factor MultiWayCalculator.java

public double getFinalValue() { return value * getTypeFactor () * getRangeFactor (); } public String toString() { return "Value = " + value + "\n" + "Type = " + type + "\n" + "Type Factor = " + getTypeFactor () + "\n" + "Range Factor = " + getRangeFactor () + "\n" + "Final Value = " + getFinalValue () + "\n"; } } // Class block end ***** SWTJC STEM ***** Chapter 5 cg 54 Calculated Getter and toString MultiWayCalculator.java

Logical expressions combine relational expressions using logical operators. The result is Boolean! Logical operators are NOT, OR, and AND. Conditional expression takes the form: rel-expr1 logical-operator rel-expr2 (x > y +3) OR (y < 5) Examples: Assume x is 2 and y is 4: x 2 (result is TRUE because both are TRUE) x 2 (result is TRUE because one is TRUE) NOT(x < 4) (result is FALSE because x < 4 is TRUE) A truth table is used to define logical operator results. ***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expressions

***** SWTJC STEM ***** Chapter 5 cg 54 Truth Table NOT A!A falsetrue false Truth table for NOT (Java symbol ! )

***** SWTJC STEM ***** Chapter 5 cg 54 Truth Table OR Truth table for OR (Java symbol || ) ABA || B false truefalsetrue falsetrue

***** SWTJC STEM ***** Chapter 5 cg 54 Truth Table AND Truth table for AND (Java symbol &&) ABA && B false truefalse truefalse true

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Example Consider this example (as before): “A value x is classified as x < 200low 200  x < 500medium 500 x < 800medium-high 800  xhigh Code a program to output the classification for a given value of x.”

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Flowchart x<200? true false Entry Point Exit Point “low” x  200 AND x<500? false x>=500 AND x<800? false “medium” “medium-high” “high” true

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Code int x = 450; if (x < 200) // x < 200 System.out.println("low"); if (x >= 200 && x < 500) // 200 ≤ x < 500 System.out.println("medium"); if (x >= 500 && x < 800) // 500 ≤ x < 800 System.out.println("medium-high"); if (x >= 800) // x ≥ 800 System.out.println("high"); LogicalExpressionAnd.java

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Example Consider another example: “A value x is classified as x 400out of range otherwisewithin range Code a program to output the classification for a given value of x.”

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Flowchart Entry Point Exit Point x 400? false “out of range” “within range” true

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Code int x = 350; if (x 400) // x 400 System.out.println(“out of range"); else System.out.println(“within range"); LogicalExpressionOr.java

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Example Consider another example: “If the driver’s seat belt is not fastened and the ignition switch is turned on, sound warning tone. Otherwise, do nothing.” Assume (1)“ig” is a variable set by the ignition switch that is “true” only if the key is in the ignition and it is turned on. (2)“sb” is a variable set by a seatbelt switch that is “true” only if the seat belt of the driver is latched.

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Flowchart Entry Point Exit Point ig AND NOT(sb) false “beep, beep” true

***** SWTJC STEM ***** Chapter 5 cg 54 Conditional Expression Code boolean ignitionSwitch = true; boolean seatBelt = false; if (ignitionSwitch && !(seatBelt)) // ig AND NOT (sb) System.out.println(“beep, beep"); LogicalExpressionSeatBelt.java

***** SWTJC STEM ***** Chapter 5 cg 54 Logical Expression Summary Use OR for “out of range” condition: x ≤ a or b ≤ x becomes if ( x = b)..... Use AND for “in range” condition: a ≤ x ≤ b becomes if ( x ≥ a && x ≤ b) x x a b x a b