Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops, Subs, & Functions Stefano Grazioli.

Similar presentations


Presentation on theme: "Loops, Subs, & Functions Stefano Grazioli."— Presentation transcript:

1 Loops, Subs, & Functions Stefano Grazioli

2 Critical Thinking Homework folders are created for each homework. Submit during the right time window. EasyMeter 

3 H5

4 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

5 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 b default 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

6 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

7 You Do The Talking Name Learning objectives
What you like about the class so far What can be improved Attitude towards the Tournament?

8 Loops

9 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

10 Loops – For Each For Each myCell As Excel.Range in Range("A1:B4")
myCell.Value = myCell.Value * 2 ‘ more commands as needed Next

11 Loops – Do Loop #1 ' count columns. numberOfColumns starts = 0
Do While topLeftCell.Offset(0,numberOfColumns).Text <>"" numberOfColumns = numberOfColumns + 1 Loop

12 Loops – Do Loop #2 Do ‘ body of the loop Loop While <test>

13 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)

14 What Is New In Technology?
WINIT What Is New In Technology?

15 Lasagne and Tagliatelle
Procedures Lasagne and Tagliatelle

16 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

17 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

18 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

19 Suggestions None. Doing well.

20


Download ppt "Loops, Subs, & Functions Stefano Grazioli."

Similar presentations


Ads by Google