Iteration -- Once Is Not Enough
Though people don’t like to repeat themselves, repetition is a valuable facility that a computer can provide. If program instructions are to be performed more than once, as in Alphabetize CDs, repetition is needed |
Two Additional Control Statements
The conditional statement (If-Then-Else) is the only way (so far) to control which statements are executed | ||
We will introduce two more: | ||
ElseIf -- a variation on the If-Then-Else for long sequences of tests | ||
Do While -- a control facility allowing statements to be repeated as long as some condition is true | ||
ElseIf solves the problem of testing a long sequence of alternatives |
If txtNum.Text = 1 Then | |
MsgBox(“John”) | |
ElseIf txtNum.Text = 2 Then | |
MsgBox(“Paul”) | |
ElseIf txtNum.Text = 3 Then | |
MsgBox(“George”) | |
ElseIf txtNum.Text = 4 Then | |
MsgBox(“Ringo”) | |
Else | |
MsgBox(“Who?”) | |
EndIf |
What does this print? | |
Dim x As Integer | |
x = 10 | |
If x = 1 Then | |
Print “octopus” | |
ElseIf x = 2 Then | |
Print “squid” | |
Else | |
Print “clam” | |
EndIf | |
Print “mollusc” |
What does this print? | |
Dim x As Integer | |
x = 10 | |
If x = 1 Then | |
Print “octopus” | |
ElseIf x = 2 Then | |
Print “squid” | |
Else | |
Print “clam” | |
EndIf | |
Print “mollusc” |
Contrast Elseif With Nested If
ElseIf is not a nested test as seen before, though it is similar | |
An If statement that uses Else If passes through all of the previous cases before reaching a given test … think about the consequences | |||
If someVar < 20 Then | |||
MsgBox(“Less than 20”) | |||
ElseIf someVar < 10 Then | |||
MsgBox(“Less than 10”) | |||
Else | |||
... | |||
EndIf |
Iteration is the repeated execution of a series of statements in programming | ||
To perform iteration, programming languages include special statements often called iteration statements | ||
There are two crucial components of all iterations: | ||
The statements that will be repeated -- called the loop body | ||
A test specifying when to repetition stops -- termination test | ||
Additionally, loops typically have at least one variable that is explicitly changed “inside” the loop -- this is called the iteration variable | ||
VB6, like most languages, has several iteration statements. We’ll just use one in CSE/IMT 100: | ||
The semantics are as follows: | ||
The termination condition is tested and if it is false the statements are all skipped; execution continues after Loop | ||
If it is true, the statements are performed once | ||
The termination condition is tested again, and if it is false the loop is over and the statements are skipped; continue after Loop | ||
If it is true, the statements are performed a second time | ||
… | ||
An easy way to get the idea of iteration is to print out the iteration variable ... |
Try the same computation with a different termination condition |
What does this code print? Dim i As Integer i = 2 Do While i <= 4 Print i i = i + 1 loop |
|
What does this code print? Dim i As Integer i = 2 Do While i <= 4 Print i i = i + 1 loop |
|
What does this code print? Dim i As Integer i = 10 Do While i <= 4 Print i i = i + 1 loop |
|
What does this code print? Dim i As Integer i = 10 Do While i <= 4 Print i i = i + 1 loop |
|
What does this code print? Dim i As Integer i = 1 Do While i >= 1 Print i i = i + 1 loop |
|
What does this code print? Dim i As Integer i = 1 Do While i >= 1 Print i i = i + 1 loop |
To get out of an infinite loop:
In Visual Basic, type control-break Then you are in the debugger, and can see where you are stuck. Select “continue” to proceed |
|
If a program (any program) gets stuck in Windows, you can also type control-alt-delete and get a task manager. Select the offending task and end it. Caution: you’ll lose any changes for that program since the last save. |
Suppose we have a procedure “squid” that takes a single integer argument. The argument is only read by squid, not changed. Write a loop that calls a “squid” with 2, 4, 6, 8, 10. |
Suppose we have a procedure “squid”
that takes a single integer argument.
The argument is only read by squid, not changed. Write a loop that calls a “squid” with 2,
4, 6, 8, 10. Dim i As Integer i = 2 Do While i <= 10 Call squid(i) i = i + 2 loop |