MIS410 logic
Computing with Business Applications
1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning.
2: VBA for Modelers: Developing Decision Support Systems with Microsoft Office Excel; by Albright 5th edition, South Western Cengage Learning.
Chapter 5
Creating Loops
1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning.
pages: 177-217
Objectives
• In this chapter, you will learn about: • The advantages of looping
• Using a loop control variable
• Nested loops
• Avoiding common loop mistakes
• Using a for loop
• Common loop applications
3© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding Looping
• Looping makes computer programming efficient and worthwhile
• Write one set of instructions to operate on multiple, separate sets of data
• Loop: a structure that repeats actions while some condition continues
4© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Advantages of Looping (continued)
5
Figure 5-1 The loop structure
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using a Loop Control Variable
• As long as a condition remains true, the statements in a while loop’s body execute
• Control variable (have a separate variable) • Loop control variable initialized before entering loop
• Loop control variable tested
• Body of loop must alter value of loop control variable
• Repetitions controlled by: • Counter
• Sentinel value
6© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using a Definite Loop with a Counter • Definite loop
• Executes a predetermined number of times
• Counter-controlled loop • Program counts loop repetitions
• Loop control variables altered by: • Incrementing
• Decrementing
7© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
8
Figure 5-3 A counted while loop that outputs Hello four times
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using an Indefinite Loop with a Sentinel Value • Indefinite loop
• Performed a different number of times each time the program executes
• The user decides how many times the loop executes
9© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
10
Figure 5-4 An indefinite while loop that displays Hello as long as the user wants to continue
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Nested Loops
• Nested loops: loops within loops
• Outer loop contains the Inner loop
• Needed when values of two (or more) variables repeat to produce every combination of values
11© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Dealing with 2D ranges
• While row_num < 5 • Print row number
• While column_num < 6 • Print column number
• Increase column number
• Increase row number
• What it will print:
1, 1,2,3,4,5, 2, 1,2,3,4,5, 3, 1,2,3,4,5, 4, 1,2,3,4,5
Programming Logic & Design, Seventh Edition 12
Is Anything Wrong?
Programming Logic & Design, Seventh Edition 13
14
Figure 5-10 Incorrect logic for greeting program because the loop control variable initialization is missing
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Is Anything Wrong?
Programming Logic & Design, Seventh Edition 15
16
Figure 5- 11
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Programming Logic & Design, Seventh Edition 17
Is Anything Wrong?
18
Figure 5-12
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Programming Logic & Design, Seventh Edition 19
Is Anything Wrong?
Programming Logic & Design, Seventh Edition 20
Figure 5-14 Improved discount sticker-making
program
Using a for Loop
• for statement or for loop is a definite loop
• Provides three actions in one structure • Initializes
• Evaluates
• Alters
21© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using a for Loop (continued)
• Example for count = 0 to 3 step 1
output "Hello"
endfor
• Initializes count variable to 0
• Checks count variable against the limit 3
• If evaluation is true, print “Hello”
• Increases count by 1
22© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using a for Loop
• Pretest loop: the loop control variable is tested before each iteration • for loops and while loops are pretest loops
• Posttest loop: the loop control variable is tested after each iteration • do…while is a posttest loop
23© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
In VBA
• For loops
• For Each Loops
• Do loops
Programming Logic & Design, Seventh Edition 24
For Loops
• For counter = first to last [Steps increment] • Statements
• Next [counter]
For i = 1 To 10 Steps 2
MsgBox (i)
Next
Programming Logic & Design, Seventh Edition 25
For Each Loops
• For Each Item in collection • Statements
• Next
Dim cell as Range
For Each cell in Range(“A1:A20”)
cell.value = 1
Next
Programming Logic & Design, Seventh Edition 26
Do Loops
• Next week…
Programming Logic & Design, Seventh Edition 27
• Using a loop to validate data • Defensive programming: preparing for all possible errors before they occur
• When prompting a user for data, no guarantee that data is valid
• Validate data: make sure data falls in acceptable ranges (month values between 1 and 12)
• GIGO: Garbage in, garbage out • Unvalidated input will result in erroneous output
28
Common Loop Applications
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Common Loop Applications
• Validating data input • Validating data requires a variety of methods
• isNumeric() or similar method • Provided with the language translator you use to write your programs
• isChar() or isWhitespace()
• Accept user data as strings
• Use built-in methods to convert to correct data types
29© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Common Loop Applications (continued)
Figure 5-21 Checking data for correct type
30© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Common Loop Applications (continued)
• Validating reasonableness and consistency of data • Many data items can be checked for reasonableness
• Good defensive programs try to foresee all possible inconsistencies and errors
31© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Control Logic - Condition If … Then … Else … Endif
If some condition Then a statement to be done if the
condition is True
OR
If some condition Then
a bunch of statements to be done if the
condition is True
Else
a bunch of statements to be done if the
condition is False
End If
Single line
Block style (preferred)
Sec. 7.3
Else block is optional
Mandatory
Control Logic - Condition If … Then … Else … Endif
If dblValue < 0 Then MsgBox “Must enter non-negative #”
OR
If dblValue < 0 Then
MsgBox “Must enter non-negative #”
Else
dblSummation = dblSummation + dblValue
intSampleSize = intSampleSize + 1
End If
Go back and look at FirstProgramFinished-InClass.xls See CountHighSales_TitleBar() for if then else and for enhancing MsgBox
Control Logic - Looping For…Next
For counter = start To stop
bunch of statements to be
repeated
Next counter
For intMonth = 1 To 12
TotalCost = TotalCost + Range(“Costs”).Cells(intMonth)
Next intMonth
Sec. 7.5-7.7
When intMonth=13, the program will “break out of the loop” and execute the statement immediately AFTER the Next statement.
Control Logic - Looping For Each…Next
For Each object In collection
bunch of statements to be
repeated
Next object
Sec. 7.6
This is the example from HW1-Rubric.xlsm that we looked at when we started learning VBA.
Control Logic - Looping Do While…Loop
Do While condition
bunch of statements to be
repeated
Loop
Do While blnStillEnteringNumbers
intNumEntered = intNumEntered + 1
BLAH BLAH BLAH .....
Loop
Sec. 7.5-7.7
If condition is true
Execute these statements
When condition is False, the program will “break out of the loop” and execute the statement immediately AFTER the Loop statement.
For-Next and Do-While Do While condition
bunch of statements to
be repeated
Loop
For i = start to stop
bunch of statements to
be repeated
Next i
vs.
For i = 1 to numEmployees
For j = 1 to numRegions
bunch of statements that use i and j
Next j
Next i
How might we modify one of the For-Next loops in FirstProgram to use Do-While instead?
Loops are one of the most useful constructs in programming Allow us to repeat a block of similar code many times, very quickly
Iteration
Conditional
Sequential Flow
More on Control Logic (Ch7) • There are additional variants of conditional and looping
constructs • If-Then-ElseIf-ElseIf…-EndIf
• Very general form of If
• Select-Case
• Avoids complicated If
• Do Until … Loop
• Do something until something is true
• Do … Loop While, Do … Loop Until • Doesn’t check looping continuation until after deed is done
• Nesting of conditional logic and looping logic is very common • Use indenting and comments to avoid going crazy
• See Chapter 7 of VBA for Modelers for all the details
Summary
• Loops write one set of instructions that operate on multiple, separate sets of data
• Three steps must occur in every loop • Initialize the loop control variable
• Compare the variable to some value
• Alter the variable that controls the loop
• Nested loops: loops within loops
• Nested loops maintain two individual loop control variables • Alter each at the appropriate time
40© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary (continued)
• Common mistakes made by programmers • Neglecting to initialize the loop control variable
• Neglecting to alter the loop control variable
• Using the wrong comparison with the loop control variable
• Including statements inside the loop that belong outside the loop
• Most computer languages support a for statement • for loop used when the number of iterations is known
• Loops are used to accumulate totals in business reports and to reprompt users for valid data
41© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.