Chapter 7 – Control Structures A payroll company calculates the gross earnings per week of employees. Employees’ weekly salaries are based on the number.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
VB Express 2008 Overview Chapters 5-8. Summary So Far We have talked about the basics of VB. How to add controls on the form? Naming convention of controls.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
Chapter 4: Control Structures: Selection
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 4: The Selection Structure
Chapter 4 The If…Then Statement
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
Chapter 12: How Long Can This Go On?
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
© 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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
Tutorial 6 The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Procedural Programming. Programming Process 1.Understand the problem 2.Outline a general solution 3.Decompose the general solution into manageable component.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Flow Control Ifs, loops. 2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Introduction to Problem Solving and Control Statements.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #5 Control Statements: Part 2.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
31/01/ Selection If selection construct.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 7 Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Chapter 4 The If…Then Statement
Chapter 5- Control Structures: Part 2
Control Structures: Part 2
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 5 – Control Structures: Part 2
Control Structures: Part 1
Chapter 3: Introduction to Problem Solving and Control Statements
المحاضرة السادسة.
Introduction to Problem Solving and Control Statements
Problem Solving and Control Statements
Presentation transcript:

Chapter 7 – Control Structures A payroll company calculates the gross earnings per week of employees. Employees’ weekly salaries are based on the number of hours they worked and their hourly wages. Create an application that accepts this information and calculates each employee’s total (gross) earnings. The application assumes a standard work week of 40 hours. The wages for 40 or fewer hours are calculated by multiplying the employee’s hourly salary by the number of hours worked. Any time worked over 40 hours in a week is considered “overtime” and earns time and a half. Salary for time and a half is calculated by multiplying the employee’s hourly wage by 1.5 and multiplying the result of that calculation by the number of overtime hours worked. The total overtime earned is added to the user’s gross earnings for the regular 40 hours of work to calculate the total earnings for that week.

Introduction Getting more complicated so we need structured approach to programming Structured Programming: techniques that bring clarity in developing and modifying programs. Algorithms: Set of instructions executed in a particular order (e.g. shower  dressed) Pseudocode: informal language to develop algorithms. –E.g. Assign 0 to counter vs. counter=0

Control Structures Sequence –E.g. A  B  C  D Selection (IF…Then, IF…Then…Else, Select case) –If (income>expenses) THEN buy ipod ELSE wait –Returns boolean data type (True/False) Iteration (or repetition) –While…End While, Do While…Loop, Do…Loop While, Do Until…Loop, Do…Loop Until, For…Next, and For Each…Next

Operators In order to evaluate a condition for a selection statement need relational and equality operators > = += -= *= /= \= ^= Control structures can be nested and stacked.

Back to application Problem: Input  (Hours/wages)  Calculate (salary)  Display (earnings) Pseudocode: -Get hours worked and hourly wages from the textbox -If hours worked are less than or equal to 40 Gross earnings equals hours worked times hourly wages -Else Gross earnings equals 40 times hourly wage plus hours above 40 times wage and a half -Display gross earnings

Designing Application Dim wage, gross As Decimal Dim hours As Double Const HOUR_LIMIT As Integer = 40 wage = Val(wageTextBox.Text) hours = Val(hoursTextBox.Text) If hours <= HOUR_LIMIT Then gross = wage * hours Else gross = HOUR_LIMIT * wage gross += (hours - HOUR_LIMIT) * wage * 1.5 End If ‘formatting the output earningsResultLabel.Text = gross [(String.Format(“{0:C}”, earnings)] Remember to insert comments

Formatting Text Output needs to be formatted String.Format(formatControlString, variable) String.Format("{0:c}", gross)

Final tips Debugger watch window –Available only in break mode –Can see contents of variables – Debug->windows->watch->type in name of variable or an expression –Tells you variable value at that point of execution and data type –Can use watch window to change var. values to text program

Chapter 8: Error message and Checkboxes Continuing with previous example. The application should give an error message if user does not input anything. If wageTextBox.Text = "" OrElse hoursTextBox.Text = "" Then MessageBox.Show("Please enter wage and hours", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If Also, create a check box that checks eligibility for overtime. E.g. if 1.5 x overtime is checked then employee eligible for 1.5 times the salary. If 1 x checked then employee eligible only for 1x the salary. If not eligible checked then employee gets paid only for 40 hours. Add a groupbox around overtime options

How it will look?

Private Sub calculateButton_Click(ByVal sender As System.EventArgs) Handles calculateButton.Click Dim wage As Double = Val(wageTextBox.Text) Dim hours As Double = Val(hoursTextBox.Text) Dim pay As Double If wageTextBox.Text = "" OrElse hoursTextBox.Text = "" Then MessageBox.Show("Please enter wage and hours", "Missing information", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ElseIf OTCheckBox.Checked = False AndAlso RegularCheckBox.Checked = False AndAlso _ NotCheckBox.Checked = False Then MessageBox.Show("Please check overtime eligibility ", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Else If OTCheckBox.Checked = True Then If hours <= 40 Then pay = wage * hours Else pay = wage * (hours - 40) * wage * 40 End If resultLabel.Text = String.Format("{0:c}", pay) End If If RegularCheckBox.Checked = True Then pay = wage * hours resultLabel.Text = String.Format("{0:c}", pay) End If If NotCheckBox.Checked = True Then If hours <= 40 Then pay = wage * hours Else pay = wage * 40 End If resultLabel.Text = String.Format("{0:c}", pay) End If End Sub Checking errors Based on checkbox selected calculate the pay. Checkbox is a bad example here. Why? Radio buttons should be used instead. Why?

Chapter 8: In-Class Exercise Dental Payment Application (Introducing checkboxes and message dialogs) A dentist’s office administrator wishes to create an application that employees can use to bill patients. The application must allow users to enter the patient’s name and specify which services were performed during the visit. The application will then calculate the total charges. If a user attempts to calculate a bill before any services are specified, or before the patient’s name is entered, an error message informing the user of that necessary input is missing will be displayed.

How will it look

Pseudocode and ACE table When user clicks calculate Clear previous total If user has not entered name or selected a checkbox Display error message Else Initialize the total to zero If “Cleaning” Checkbox is selected Add cost of a cleaning to total If “Cavity filling” Checkbox is selected Add cost of receiving filling to the total If “X-Ray” Checkbox is selected Add cost of receiving X-Ray to the total Format total to display as currency Display total

Logical Operators

Checkboxes Add checkboxes Private Sub calculateButton_Click totalResultLabel.Text = "" Dim total As Decimal If cleanCheckBox.Checked = True Then total += 35 End If If cavityCheckBox.Checked = True Then total += 150 End If If xrayCheckBox.Checked = True Then total += 85 End If totalResultLabel.Text = String.Format("{0:c}", total) End Sub

Problem If no checkboxes or no name, no error Doesn’t clear results on changing the checkbox. If (((nameTextBox.Text = "“)) OrElse ((cleanCheckBox.Checked = False AndAlso cavityCheckBox.Checked = False AndAlso xrayCheckBox.Checked = False))) Then MessageBox.Show("Please enter patients name", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End if

Homework Problem 8.11 Download the template from the website and make the necessary changes. As usual submit screen shot and a printout of your program with your name commented