© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing.
Advertisements

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Introduction to C Programming
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Introduction to C Programming
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Chapter 05 (Part III) Control Statements: Part II.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Completing the Problem-Solving Process
Chapter 5 Control Statements: switch Statement
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4- part 2 Control Statements: Loops 1
Chapter 5 – part 1 Control Statements: switch Statement
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing the switch Multiple-Selection Statement Outline 9.1 Test-Driving the Income Tax Calculator Application 9.2 Introducing the switch Multiple-Selection Statement 7.3Implicit (promotion) and Explicit (cast) conversion 9.3 Constructing the Income Tax Calculator Application 9.4 Wrap-Up

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Use the switch multiple-selection statement to choose among many alternative actions. –Use case labels to identify the alternative actions in switch statements. –Understand the short and long data types to represent small and large integers.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.1 Test-Driving the Income Tax Calculator Application

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.1 Test-Driving the Income Tax Calculator Application (Cont.) Figure 9.3 Income Tax Calculator application output for a $100,000 yearly salary.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.2 Introducing the switch Multiple-Selection Statement The syntax of the switch statement is a bit peculiar. Its objective is to check several possible constant values for an expression. Something similar to what we did at the beginning of this section with the concatenation of several if and else if instructions (source: cplusplus.com)cplusplus.com switch (expression) { case constant1: group of statements 1; break; case constant2: group of statements 2; break;. default: default group of statements }

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.2 Introducing the switch Multiple-Selection Statement switch multiple-selection statements –switch keyword –Controlling expression Must be a value of the char, short, int or long types or a type that can be converted to these types –case labels case keyword Constant expression –Can be a character literal, an integer literal or a variable containing a character or integer constant –Cannot be a floating-point value

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.2 Introducing the switch Multiple-Selection Statement (Cont.) –break statement transfers control to the first statement after the switch statement If a case label has no break statement, execution “falls through” –default case Executes if no constant expression matches the controlling expression –A switch statement cannot contain two case labels with the same constant expression or two default cases –If the controlling expression does not match any constant expression and there is no default case, none of the statements in the switch body are executed

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.2 Introducing the switch Multiple-Selection Statement (Cont.) Figure 9.4 switch multiple-selection statement UML activity diagram.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Implicit (promotion) and Explicit (cast) conversion Implicit conversion (promotion) –Values are automatically converted to “larger” data types (types at top of the list in Fig. 7.12) when necessary –No loss of data Explicit conversion (casting) –Use unary cast operator static_cast –Prevents compiler warnings when converting to a “smaller” data type –Converting to a “smaller” data type may cause a loss of data

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fundamental Data Types (Week 3 Review) Summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.3 Implicit (promotion) and Explicit (cast) conversion

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.6 Defining variables to contain the tax rate and the yearly salary. Defining variables to store user input

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.7 Prompting the user for and inputting the yearly salary. Prompting the user for and inputting the yearly salary

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.8 Adding an if... else statement to the function. Displaying an error message if the user enters an invalid salary

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.9 Adding a switch statement to the function. Beginning of the switch statement

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.10 Adding a case label to the switch statement. Sets the tax rate to 15% if salary is less than 25000

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.11 Finishing the switch statement. Sets tax rate to 25% if salary is between and Multiple case labels result in the same taxRate (28%) Multiple case labels result in the same taxRate (33%)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.12 Adding a default case to the switch statement. default case at the end of the switch statement When the default case is at the end of the switch body, a break statement is unnecessary

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.13 Calculating and displaying the income tax. Calculating the income tax Displaying the income tax

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.14 Completed Income Tax Calculator application.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. IncomeTaxCalculator. cpp (1 of 4) Define variables to store user inputPrompt the user for and input the yearly salary

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. IncomeTaxCalculator. cpp (2 of 4) Beginning of the switch statement Set the tax rate to 15% if salary is less than Set the tax rate to 25% if salary is between and Set the tax rate to 28% if salary is between and

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. IncomeTaxCalculator. cpp (3 of 4) Set the tax rate to 33% if salary is between and default case at the end of the switch statement Calculate the income taxDisplay the income tax

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. IncomeTaxCalculator. cpp (4 of 4) Display an error message if annual salary is not greater than zero

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 4 − Wage Calculator. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 4 Questions 4.1, 4.2, 4.7, 4.8, 4.9, 4.10 at the end of the Chapter 4 plus Tutorial 9 Questions 9.1, 9.2, 9.5, 9.7, 9.10 at the end of Chapter 9. Always write the question followed by the answer. Remember to highlight the answer. Exercises 4.11, 9.11, and 4.17 Due next Wednesday