CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?



Similar documents
Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Introduction to Java

Conditionals (with solutions)

Chapter 5. Selection 5-1

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Chapter 8 Selection 8-1

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Building Java Programs

In this Chapter you ll learn:

Topic 11 Scanner object, conditional execution

J a v a Quiz (Unit 3, Test 0 Practice)

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

COMP 110 Prasun Dewan 1

Conditional Statements Summer 2010 Margaret Reid-Miller

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Building Java Programs

Token vs Line Based Processing

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Lecture 1 Introduction to Java

PRINT AWARENESS ASSESSMENT. Instructions and Teacher Recording Form

Some Scanner Class Methods

Chapter 2: Elements of Java

Part I. Multiple Choice Questions (2 points each):

Building Java Programs

AP Computer Science Java Subset

Sample CSE8A midterm Multiple Choice (circle one)

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Boolean Expressions & the if Statement

1 Introduction. 2 Overview of the Tool. Program Visualization Tool for Educational Code Analysis

Arrays. Introduction. Chapter 7

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Lesson 3: Using Inequalities to Problem Solve

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Building Java Programs

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Programming Your App to Make Decisions: Conditional Blocks

PART A: For each worker, determine that worker's marginal product of labor.

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

A basic create statement for a simple student table would look like the following.

INDIVIDUAL MASTERY for: St#: Test: CH 9 Acceleration Test on 29/07/2015 Grade: B Score: % (35.00 of 41.00)

INDIVIDUAL MASTERY for: St#: Test: CH 9 Acceleration Test on 09/06/2015 Grade: A Score: % (38.00 of 41.00)

JavaScript: Control Statements I

While Loop. 6. Iteration

Lecture 5: Java Fundamentals III

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

While and Do-While Loops Summer 2010 Margaret Reid-Miller

Moving from CS 61A Scheme to CS 61B Java

Conditions & Boolean Expressions

Unit 6. Loop statements

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Chapter 2 Introduction to Java programming

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Programming Languages CIS 443

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

VB.NET Programming Fundamentals

>


Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

GCE. Computing. Mark Scheme for January Advanced Subsidiary GCE Unit F452: Programming Techniques and Logical Methods

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

Maria Litvin Phillips Academy, Andover, Massachusetts. Gary Litvin Skylight Publishing, Andover, Massachusetts

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

The if Statement and Practice Problems

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Chapter 2 Basics of Scanning and Conventional Programming in Java

Problem Solving Basics and Computer Programming

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Iteration CHAPTER 6. Topic Summary

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

ALGORITHMS AND FLOWCHARTS

03 - Lexical Analysis

Java 進 階 程 式 設 計 03/14~03/21

Maple T.A. Beginner's Guide for Instructors

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

CS170 Lab 11 Abstract Data Types & Objects

Statements and Control Flow

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Homework/Program #5 Solutions

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Decision-making Computer Science Lesson to Prepare for UIL Computer Science Contest

Object Oriented Software Design

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

13 Classes & Objects with Constructors/Destructors

TImath.com Algebra 1. Absolutely!

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

D) Marginal revenue is the rate at which total revenue changes with respect to changes in output.

Intro to Web Programming. using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

csce4313 Programming Languages Scanner (pass/fail)

Transcription:

CASCADING IF-ELSE A cascading if- is a composite of if- statements where the false path of the outer statement is a nested if- statement. The nesting can continue to several levels. Cascading if- Syntax if ( truth value ) path if ( truth value ) path if ( truth value ) path... statement below Cascading if- Semantics What the computer executes: What is the truth value? true false Execute path What is the truth value? true false Execute path What is the truth value? true Execute path false... Proceed to execute statement below As illustrated in the picture, execution begins with the first (i.e. top-most) if- statement Cascading If-Else Page

by () evaluating truth value. If true, the computer () executes the statement or statements in path after which it branches to the statement below the cascading if- structure. If truth value is false, the computer () proceeds to the second if- statement and repeats the process. If you write the cascading if- correctly, then at most one of the execution paths (i.e. path, path, etc.) will be executed. Example This code illustrates the semantics of the cascading if-. It takes an integer representing a student s score and determines the corresponding letter grade using a 90/80/70/60 scale. For instance, if score is 75 then the relational expression at line is false, that at line 4 is false and that at line 7 is true. At line 8, grade is set to C and the remaining lines are skipped. 4 5 6 7 8 9 0 if ( score >= 90 ) grade = 'A'; if ( score >= 80 ) grade = 'B'; if ( score >= 70 ) grade = 'C'; if ( score >= 60 ) grade = 'D'; grade = 'F'; Cascading If-Else Page

Seasoned programmers prefer to avoid the text wandering across the page by indenting all the keywords at the same level. Example Cascaded if- statements indented as preferred by seasoned programmers. 4 5 6 7 8 9 0 if ( score >= 90 ) grade = 'A'; if ( score >= 80 ) grade = 'B'; if ( score >= 70 ) grade = 'C'; if ( score >= 60 ) grade = 'D'; grade = 'F'; Beginner Error on Cascading if- The most common error a novice programmer will make coding a cascading if- is to omit an. Example The code segment below incorrectly omits an at line 5. This incorrect code gives any score over 70, including 80s and 90s, a grade of C. 4 5 6 7 8 9 0 if ( score >= 90 ) grade = 'A'; if ( score >= 80 ) grade = 'B'; if ( score >= 70 ) grade = 'C'; if ( score >= 60 ) grade = 'D'; grade = 'F'; Cascading If-Else Page

Exercises. What is the output of this code segment if the user enters 5? If the user enters 0? If the user enters 5? If the user enters 0? If the user enters 0? If the user enters? Scanner input = new Scanner( System.in ); int x = input.nextint( ); if ( x <= 0 ) if ( x <= 0 ) if ( x <= 0 ) System.out.println( x );. What is the output of this code segment if the user enters 5? If the user enters 0? If the user enters 5? If the user enters 0? If the user enters 0? If the user enters? Scanner input = new Scanner( System.in ); int x = input.nextint( ); if ( x <= 0 ) if ( x <= 0 ) if ( x <= 0 ) System.out.println( x ); Cascading If-Else Page 4

. What is the output of this code segment if the user enters 5? If the user enters 8? If the user enters 0? int x = scanner.nextint( ); if ( x < 0 ) System.out.println( "UNDER TEN" ); if ( x % == 0 ) System.out.println( "EVEN" ); if ( x > 5 ) System.out.println( "OVER FIVE" ); System.out.println( "UNDER FIVE" ); 4. What is the output of this code segment if the user enters 5? If the user enters 8? If the user enters 0? int x = scanner.nextint( ); if ( x < 0 ) System.out.println( "UNDER TEN" ); if ( x % == 0 ) System.out.println( "EVEN" ); if ( x > 5 ) System.out.println( "OVER FIVE" ); System.out.println( "UNDER FIVE" ); For each of the following use a cascading if- to complete the code fragment. 5. A PBS station gives donor gifts as shown below. Each donor receives only one gift. Write a Java code fragment that inputs a donor s name and contribution and prints his or her name and gift. Donor's Contribution Gift under $5 Tote bag $5 to $50 Coffee mug over $50 DVD set Cascading If-Else Page 5

6. Pistol Pete s all-you-can-eat buffet restaurant charges as shown below. Write the Java code fragment that () inputs the customer s age, () if the customer is a child, inputs his or her weight and () determines what price to charge. Age Price under 6 Free 6 to 5 9 times child s weight (in lbs.) 6-59 $0.95 60 and over $8.95 7. Using the pay schedule shown below, write the Java code fragment that inputs an employee s wage and hours and calculates his or her pay. Hours Worked Pay 0 to 40 Base hourly wage 4 to 60 ½ times the base wage 6 to 80 times the base wage 8. Using the table below, write the Java code fragment that inputs the average student rating of a teacher and prints the correct message. Average Rating Message Under You suck! Over and under 4 You need help. Over 4 and under 5 Good Job! 5 YOU GET A RAISE! Cascading If-Else Page 6