Loops, Subs, & Functions Stefano Grazioli
Critical Thinking Homework folders are automatically created for each homework. Submit during the right time window. EasyMeter
H5
It begins with figuring out what needs to be done Hey, awesome financial calculator! Can I have the table of results in a choice of colors? User perspective “Use cases” Talk about it Sketch it Show it Prototype with paper and pencil
Activity Diagram (part III) Simple Financial Calculator User inputs principal, years, and interest rate Autofit the columns User presses “print table” button Principal, Year, or Rate is missing Ask the user for a color preference Alert the user Print table header and data a default b i > number of years Format the data Format Table in Red Format Table in Green Format Table in Blue i <= number of years Print the ith row of data
An alternative to IF…THEN when there are multiple options Ask the user for a color preference Select Case textFromUser Case “a” ‘ More instructions… red paint Case “b” ‘ More instructions… green paint Case Else ‘ More instructions… blue paint End Select a b default Format Table in Red Format Table in Green Format Table in Blue
You Do The Talking Name Learning objectives What you like about the class so far What can be improved Attitude towards the Tournament?
Loops
Loops – For … Next For i As Integer = 1 To 10 Step 1 next Range(“A3”).Offset(i, 0).Value = i * 100 ‘ more commands as needed next
Loops – For Each For Each myCell As Excel.Range in Range("A1:B4") myCell.Value = myCell.Value * 2 ‘ more commands as needed Next
Loops – Do Loop #1 ' count columns. numberOfColumns starts = 0 Do While topLeftCell.Offset(0,numberOfColumns).Text <>"" numberOfColumns = numberOfColumns + 1 Loop
Loops – Do Loop #2 Do ‘ body of the loop Loop While <test>
Implementing a nested Loop Increment r by 2 & reset c Increment c by 1 For r As Integer = 1 To (numberOfRows-1) Step 2 For c As Integer = 0 To (numberOfColumns - 1) topLeftCell.Offset(r, c).Interior.Color = Drawing.Color.Pink Next Color the cell (offset r,c) c < (number of columns -1) r < (number of rows -1)
What Is New In Technology? WINIT What Is New In Technology?
Lasagne and Tagliatelle Procedures Lasagne and Tagliatelle
Subs are a type of procedure Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim inputFromUser As Double = 0 Dim square As Double = 0 inputFromUser = InputBox("Please enter a number to square") square = inputFromUser ^ 2 ShowTheUser(square) End Sub Private Sub ShowTheUser(someValue As Double) Range(“A1”).Value = "The result is " + someValue.ToString("N0") A procedure is a named sequence of steps Purpose: reuse & simplify existing code Good practice: shorter than a single screen Sometimes it takes arguments “Macro” is the old name for procedure
Variables and Parameters Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim inputFromUser As Double = 0 Dim square As Double = 0 inputFromUser = InputBox("Please enter a number to square") square = inputFromUser ^ 2 ShowTheUser(square) End Sub Private Sub ShowTheUser(someValue As Double) Range(“A1”).Value = "The result is " + someValue.ToString("N0") Variables have a lifecycle / scope Parameters are a special type of variable: used as input to procedures Find the vars and params in here
Functions are another type of procedure input Return a single value Conceptually similar to the formulas in your worksheets output Private Function CalcInterest(r As Double, principal As Double, t As Double) As Double Dim interest As Double = 0 interest = principal * ((((1 + r) ^ t) - 1)) Return interest End Function
Suggestions None. Doing well.