For…Next
Walk though the following code segments, recording the values stored in the variables identified in the table as values are assigned to them. Record any output produced in the output column.
1)
Dim intNumber as integer
For intNumber = 1 to 5 step 1
Print intNumber * intNumber
Next intNumber
|
intNumber |
Output |
|
1 |
1 |
|
2 |
4 |
|
3 |
9 |
|
4 |
16 |
|
5 |
25 |
|
6 Since the loop is not satisfied until the value of intNumber is greater than 5 the value is has at the end is 6 |
|
|
|
|
|
|
|
2)
Dim intNumber as integer
Dim intAnswer as integer
For intNumber = 1 to 4
intAnswer = intNumber * 2
intTotal = intTotal + intAnswer
Next intNumber
Print intAnswer
|
intNumber |
intAnswer |
intTotal |
Output |
|
0 |
0 |
2 |
8 The final value of intAnswer is displayed after the loop is satisfied |
|
1 |
2 |
6 |
|
|
2 |
4 |
12 |
|
|
3 |
6 |
20 |
|
|
4 |
8 |
|
|
|
5 Since the loop is not satisfied until the value of intNumber is greater than 4 the value is has at the end is 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|