Delivery and other DO Examples Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1 based on lecture notes by D. Schneider.
5.04 Apply Decision Making Structures
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Control Arrays, Records, and Record Arrays in V.B. Week 10.
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
InvEasy (Project1) Please use speaker notes for additional information!
Break Processing Please use speaker notes for additional information!
Chapter 7 Code Tables. VB Code Box 7-1 Event Procedure for Compute Button Private Sub hsbExemptions_Change() txtExemptions.Text =Str(hsbExemptions.Value)
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.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
The Repetition Process in Visual Basic. The Repetition Process The capability to repeat one or more statements as many times as necessary is what really.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
Logic Structure - focus on looping Please use speaker notes for additional information!
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
Array - adding to array at run time Please see speaker notes for additional information!
Do Loop with Interest Please see speaker notes for additional information!
Chapter Six: Working With Arrays in Visual Basic.
New Project in Visual Basic Please use speaker notes for additional information!
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright by Ronald P. Kessler, Ph.D.
MDI with Menu Please use speaker notes for additional information!
Random Files Please see speaker notes for additional information!
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
22/11/ Selection If selection construct.
Notes on ADO from other projects Please use speaker notes for additional information!
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)
Two Forms Please use speaker notes for additional information!
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Pay Example (PFirst98) Please use speaker notes for additional information!
Visual Basic I/O Programs (ProjRead1, ProjRead2, ProjWrite1, ProjPay) Please use speaker notes for additional information!
Maximum Profit Please use speaker notes for additional information!
CECS 5020 Computers in Education Visual Basic Variables and Constants.
31/01/ Selection If selection construct.
Using a Database Access97 Please use speaker notes for additional information!
Adding Code to the Option Button. Open VB 1.Double click the Calculate button and select General from the Object list box. 2.Add the following code to.
Unbound data fields, find, filter etc. using Data Environment Please use speaker notes for additional information!
I am using Visual Basic 6 for this class. If you want to use a different version, please contact me. Thanks!
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
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.
Multiple forms - SDI & MDI Please use speaker notes for additional information!
Visual Basic - Break Processing
Processing multiple files
Visual Basic Fundamental Concepts
Find, filter etc with connection to Access code internally
VB.Net Programming Console Application
When I want to execute the subroutine I just give the command Write()
Visual Basic..
Department Array in Visual Basic
Please use speaker notes for additional information!
If selection construct
If selection construct
If statements (Inven1, Inven2, Inven2a, Inven3, Inven3a)
More and Still More on Procedures and Functions
Please see speaker notes for additional information!
Text / Serial / Sequential Files
Introduction to Computer Programming IT-104
Presentation transcript:

Delivery and other DO Examples Please use speaker notes for additional information!

Do Loop Private Sub cmdCalc_Click() Rem up to 1 lb costs $1.50 Rem over 1 lb costs $1.50 plus $.50 for every Rem additional 4 0unces Const cstFirstLb As Single = 1.5 Const cstEvery4oz As Single = 0.5 Dim wrkWeight As Integer Dim wrkCost As Single wrkWeight = Val(txtPounds.Text) * 16 + Val(txtOunces.Text) If wrkWeight < 17 Then wrkCost = cstFirstLb Else wrkWeight = wrkWeight - 16 wrkCost = cstFirstLb Do While wrkWeight > 0 wrkCost = wrkCost + cstEvery4oz wrkWeight = wrkWeight - 4 Loop End If txtAmountDue.Text = Format(wrkCost, "Currency") End Sub 5 pounds were entered - I am converting that to ounces and then adding the number of ounces that were entered. 5 * = 86 for this example If weight is a pound or less the cost is the cost of a pound. 86 not < 17 In this example, wrkWeight = = 70 wrkCost = 1.5 The pricing is 1.5 for the first pound and then.5 for every 4 ounces (or portion of 4 ounces) over. Does the loop while wrkWeight > 0. Each pass through it adds.5 to the cost and subtracts 4 ounces from the weight. wrkCost is put in the text box - in this example it is 10.5.

Private Sub cmdCalc_Click() Rem up to 1 lb costs $1.50 Rem over 1 lb costs $1.50 plus $.50 for every Rem additional 4 0unces Const cstFirstLb As Single = 1.5 Const cstEvery4oz As Single = 0.5 Dim wrkWeight As Integer Dim wrkCost As Single wrkWeight = Val(txtPounds.Text) * 16 + Val(txtOunces.Text) If wrkWeight < 17 Then wrkCost = cstFirstLb Else wrkWeight = wrkWeight - 16 wrkCost = cstFirstLb Do While wrkWeight > 0 wrkCost = wrkCost + cstEvery4oz wrkWeight = wrkWeight - 4 Loop End If txtAmountDue.Text = Format(wrkCost, "Currency") End Sub 5 pounds were entered - I am converting that to ounces and then adding the number of ounces that were entered. 5 * = 86 for this example If weight is a pound or less the cost is the cost of a pound. 86 not < 17 In this example, wrkWeight = = 70 wrkCost = 1.5 wrkCost 0f 10.5 is s put in the text box Enter Loop: wrkWeight = 70, wrkCost = 1.5 End 1st pass: wrkCost = 2 wrkWeight = 66 End 2nd pass: wrkCost = 2.5 wrkWeight = 62 End 3rd pass: wrkCost = 3 wrkWeight = 58 End 4th pass: wrkCost = 3.5 wrkWeight = 54 End 5th pass: wrkCost = 4 wrkWeight = 50 End 6th pass: wrkCost = 4.5 wrkWeight = 46 End 7th pass: wrkCost = 5 wrkWeight = 42 End 8th pass: wrkCost = 5.5 wrkWeight = 38 End 9th pass: wrkCost = 6 wrkWeight = 34 End 10th pass: wrkCost = 6.5 wrkWeight = 30 End 11th pass: wrkCost = 7 wrkWeight = 26 End 12th pass: wrkCost = 7.5 wrkWeight = 22 End 13th pass: wrkCost = 8 wrkWeight = 18 End 14th pass: wrkCost = 8.5 wrkWeight = 14 End 15th pass: wrkCost = 9 wrkWeight = 10 End 16th pass: wrkCost = 9.5 wrkWeight = 6 End 17th pass: wrkCost = 10 wrkWeight = 2 End 17th pass: wrkCost = 10.5 wrkWeight = -2 wrkWeight is no longer greater than 0 so the loop ends

Private Sub cmdCalc_Click() Rem up to 1 lb costs $1.50 Rem over 1 lb costs $1.50 plus $.50 for every Rem additional 4 0unces Const cstFirstLb As Single = 1.5 Const cstEvery4oz As Single = 0.5 Dim wrkWeight As Integer Dim wrkCost As Single wrkWeight = Val(txtPounds.Text) * 16 + Val(txtOunces.Text) If wrkWeight < 17 Then wrkCost = cstFirstLb Else wrkWeight = wrkWeight - 16 wrkCost = cstFirstLb Do While wrkWeight > 0 wrkCost = wrkCost + cstEvery4oz wrkWeight = wrkWeight - 4 Loop End If txtAmountDue.Text = Format(wrkCost, "Currency") End Sub Private Sub cmdClear_Click() txtName.Text = "" txtStAdr.Text = "" txtCSZ.Text = "" txtPounds.Text = "" txtOunces.Text = "" txtAmountDue.Text = "" End Sub Private Sub cmdExit_Click() End End Sub Delivery

ProjNP1 "123","Ann Alden",25,"A" "234","John Adams",35,"B" "345","Susan Smith",40,"A" "456","Linda Costa",36,"B" "567","Donald Page",28,"B" "678","Ryan Brown",40,"C" "789","Robert Souza",35,"A" "890","Mary Fields",25,"C" Option Explicit Private Sub cmdExit_Click() Close #1 End End Sub Private Sub cmdRead_Click() Dim Idno As String, Name As String Dim Hrs As Integer, PayCode As String If Not EOF(1) Then Input #1, Idno, Name, Hrs, PayCode txtIdNo = Idno txtName = Name txtHrs = Hrs txtPayCode = PayCode End If End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub

Option Explicit Private Sub cmdClear_Click() txtEnterName.Text = "" txtPay.Text = "" Open "A:\Nametest.txt" For Input As #1 txtEnterName.SetFocus End Sub Private Sub cmdEnd_Click() Close #1 End End Sub Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do While Name <> txtEnterName.Text And Not EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub "123","Ann Alden",25,"A" "234","John Adams",35,"B" "345","Susan Smith",40,"A" "456","Linda Costa",36,"B" "567","Donald Page",28,"B" "678","Ryan Brown",40,"C" "789","Robert Souza",35,"A" "890","Mary Fields",25,"C" ProjPay1

Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do While Name <> txtEnterName.Text And Not EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub "123","Ann Alden",25,"A" "234","John Adams",35,"B" "345","Susan Smith",40,"A" "456","Linda Costa",36,"B" "567","Donald Page",28,"B" "678","Ryan Brown",40,"C" "789","Robert Souza",35,"A" "890","Mary Fields",25,"C" ProjPay1 Ryan Brown was entered as txtEnterName. The loop will sequentially move through the records in the file until it finds a match or until EOF is encountered. The loop continues as long as there is not a match and it is not EOF. Each pass through the loop brings in a record. When there is a match the loop ends and the last input is the name we are looking for.

Do While Condition ? Execute the statements in the loop True Execute the statements following the loop False Syntax: Do While condition VB statements Loop

Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do While Name <> txtEnterName.Text And Not EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub ProjPay1 "123","Ann Alden",25,"A" "234","John Adams",35,"B" "345","Susan Smith",40,"A" "456","Linda Costa",36,"B" "567","Donald Page",28,"B" "678","Ryan Brown",40,"C" "789","Robert Souza",35,"A" "890","Mary Fields",25,"C" If a match was found the loop ended. The loop can also end because EOF was reached. The IF statement checks to make sure that the match was found and then processes. No processing is done if the match was not found. The inner IF statement checks the PayCode and calculates the wrkPay depending on the code. At the end of the inner IF which checks the PayCode, the wrkPay is formatted as currency and moved to a text box.

Option Explicit Private Sub cmdClear_Click() txtEnterName.Text = "" txtPay.Text = "" Open "A:\Nametest.txt" For Input As #1 txtEnterName.SetFocus End Sub Private Sub cmdEnd_Click() Close #1 End End Sub Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do While Name <> txtEnterName.Text And Not EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub ProjPay1 Note that in this program, I/O is handled in the following ways: Open is done with the form load. Close is done after the search that is done in cmdFind. Open is done when the screen is cleared. Close is done when cmdEnd is executed.

Option Explicit Private Sub cmdClear_Click() txtEnterName.Text = "" txtPay.Text = "" Open "A:\Nametest.txt" For Input As #1 txtEnterName.SetFocus End Sub Private Sub cmdEnd_Click() Close #1 End End Sub Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do Until Name = txtEnterName.Text Or EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop Rem The Until can either be with the Do or with Rem the Loop clause Rem Do Rem Input #1, IdNo, Name, Hrs, PayCode Rem Loop until Name = txtEnterName.Text Or EOF(1) If Name = txtEnterName.Text Then If PayCode = "A" Then wrkPay = Hrs * 10 ElseIf PayCode = "B" Then wrkPay = Hrs * 15 ElseIf PayCode = "C" Then wrkPay = Hrs * 20 Else wrkPay = Hrs * 0 End If txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub ProjPay2 The Do Until executes the loop until the until clause becomes true true as opposed to the Do While which executes the loop while the condition is true. Note that the until can be coded with the Do or with the Loop.

Condition ? Execute the statements in the loop True Execute the statements following the loop False Syntax: Do VB statements Loop Until condition is true Do Until

Option Explicit Private Sub cmdClear_Click() txtEnterName.Text = "" txtPay.Text = "" Open "A:\Nametest.txt" For Input As #1 txtEnterName.SetFocus End Sub Private Sub cmdEnd_Click() Close #1 End End Sub Private Sub cmdFind_Click() Dim IdNo As String, Name As String Dim Hrs As Integer, PayCode As String Dim wrkPay As Single Do Until Name = txtEnterName.Text Or EOF(1) Input #1, IdNo, Name, Hrs, PayCode Loop Rem The Until can either be with the Do or with Rem the Loop clause Rem Do Rem Input #1, IdNo, Name, Hrs, PayCode Rem Loop until Name = txtEnterName.Text Or EOF(1) If Name = txtEnterName.Text Then Select Case PayCode Case "A" wrkPay = Hrs * 10 Case "B" wrkPay = Hrs * 15 Case "C" wrkPay = Hrs * 20 Case Else wrkPay = Hrs * 0 End Select txtPay.Text = Format(wrkPay, "Currency") End If Close #1 End Sub Private Sub Form_Load() Open "A:\Nametest.txt" For Input As #1 End Sub ProjPay3 Select Case PayCode means that the PayCode field from the record on the file will be checked. The valid cases are where the PayCode is an A, or a B, or a C. For any other Code, the pay will be calculated using a 0 times the hours worked.