All programming languages have a loop capability. Looping is the program logic’s ability to repeat one or more lines of code either:
1. A set number of times, a “for loop” (i.e. 10, 100, 500, 763 or more times)
2. An “unknown” number of times (i.e. loop terminates if a tested condition becomes “true” or as long as the tested condition remains “false”), a “while” loop, a “do until” loop, a “do while” loop.
This Lab exercise demonstrates the use of the “For Loop”, a standard loop in all programming languages. Visual Basic’s standard For Loop format looks like:
For N = 1 To 100
Loop line 1 code
Loop line 2 code
Loop line 3 code
Loop line N code
Next N
The loop code needs:
1. A “loop counter variable” (N in the above example) to keep track of how many iterations this loop has run
2. A starting value (i.e. 1 in the above example)
3. A stop value (i.e. 100 in the above example)
The loop will start at 1 and increment by 1 until the counter exceeds 100. The loop will then stop at that point. We can substitute variables for the start and stop numeric constant values such as:
For N = 1 To NumberofTimes
Lines of Loop code
Next N
This Lab Assignment will allow the user to:
1. Enter a positive number (we will dispense with GIGO code)
2. Run a For Loop from 1 to N times keeping a running count of the loop iteration values (i.e. 1 + 2 + 3 + 4 + 5 + 6 +…+ N)
3. Display that count in a Label
4. You will need a textbox to hold the value that the user enters, a label to display the final total, a button to calculate, a button to clear the label and the textbox, and a button to quit the VB application
The basic loop logic looks like:
For N = 1 To NumberofTimes
Total = Total + N
Next N