What is Repetition (Looping)?
• Repetition (or loops) means repeating code several times.
• Useful when the program must repeat certain steps until a condition is met.
• Common example: Accepting many inputs or displaying a list of values.
Why Use Loops?
• Makes code shorter and more efficient.
• Example: Instead of writing 50 lines to get 50 numbers, use a loop to repeat
the input step.
• Used to:
o Take multiple inputs (e.g., numbers, names).
o Show multiple outputs (e.g., numbers in a list box).
What You Can't Use in a Loop
• Input: Cannot use a TextBox for input in a loop → use InputBox().
o Example:
name = InputBox("Enter your name")
number = CInt(InputBox("Enter a number"))
• Output: Cannot use a Label for output in a loop → use:
o ListBox
o MessageBox.Show() or MsgBox()
Counter-Controlled Loops
• Loops that repeat a known number of times.
• Uses a counter variable.
• Loop stops when the counter reaches a target value.
You need:
1. Declare a counter
2. Initialize it
3. Test it in the loop condition
4. Increment or decrement it
Examples:
Cnt = Cnt + 1 ' Increment
Cnt += 1 ' Increment
Cnt = Cnt - 1 ' Decrement
Cnt -= 1 ' Decrement
For … Next Loop
• Easiest loop to use when the number of repetitions is known.
Syntax:
For counter = start To end [Step value]
' Statements to repeat
Next
• Default Step is 1 if not written.
• The loop:
o Initializes the counter
o Tests the counter
o Increments the counter
Examples
• Display numbers 1 to 3:
For i = 1 To 3
ListBox1.Items.Add(i)
Next
• Display numbers 0 to 3 with 0.5 step:
For i = 0 To 3 Step 0.5
ListBox1.Items.Add(i)
Next
• Display numbers 5 to 1 (counting down):
For i = 5 To 1 Step -1
ListBox1.Items.Add(i)
Next
• Get 5 names from the user and add to list box:
For i = 1 To 5
name = InputBox("Enter a name")
ListBox1.Items.Add(name)
Next
STEP is used to specify the value you want Increment(increase) or
Decrement(Decrease)
Types of Loops in VB.Net
Loop Type When to Use
Used when the number of iterations is unknown. Loop continues while condition
Do While…Loop
is True.
For…Next Best for counter-controlled loops (fixed number of repetitions).
For Each…Next Ideal for looping through collections or arrays.
While…End
Similar to Do While, repeats as long as a condition is True.
While
With…End With Not a loop, but allows multiple statements to refer to the same object.
Nested Loops Loops inside other loops (can be For, While, or Do loops).
Do While…Loop
• Structure:
Do While condition
' Loop body
Loop
• Key Points:
o Initialize before the loop.
o Test the condition at the top.
o Increment at the end of the loop.
o Can be exited early using Exit Do.
For…Next Loop
• Structure:
For counter = start To end
' Loop body
Next
• Automatically handles initialization, testing, and incrementing.
Common Loop Tasks
✅ Totals
• Steps:
o Initialize total before loop: total = 0
o Add to total inside loop: total += number
o Use/display total after loop.
Counters
• Count how many times a condition is met (e.g., number of even values).
• Initialize count = 0 before loop.
• Increment count += 1 inside the loop when condition is met.
Averages
• Accumulate total.
• Count how many valid entries.
• After loop: average = total / count.
Common Loop Errors
Error Description
No increment or wrong condition—loop never
Infinite loop
ends.
Off-by-one error Loop runs one too many or too few times.
Incorrect initialization Not setting starting values correctly.
Skipping the loop
Condition is false before entering the loop.
entirely
Best Practices
• Use For…Next for simple counter-controlled loops.
• Use Do While…Loop when:
o You want more control.
o You need to validate user input.
o The number of iterations is unknown or depends on the user.
For Loop Example
Dim i As Integer
lstNumbers.Items.Clear()
For i = 1 To 5
lstNumbers.Items.Add(i)
Next i
Explanation:
• The loop starts at 1 and ends at 5.
• Each number is added to the ListBox.
Do While Loop Example
Dim i As Integer
i = 1
lstNumbers.Items.Clear()
Do While i <= 5
lstNumbers.Items.Add(i)
i += 1
Loop
Explanation:
• i is initialized to 1 before the loop.
• It checks if i <= 5, then adds it to the ListBox.
• i is incremented inside the loop.
For Loop – Total and Average Example
vb.net
CopyEdit
Dim i As Integer
Dim total As Integer = 0
lstNumbers.Items.Clear()
For i = 1 To 5
lstNumbers.Items.Add("Number: " & i)
total += i
Next i
Dim average As Double = total / 5
lstNumbers.Items.Add("Total: " & total)
lstNumbers.Items.Add("Average: " & average)
Do While Loop – Total and Average Example
Dim i As Integer = 1
Dim total As Integer = 0
lstNumbers.Items.Clear()
Do While i <= 5
lstNumbers.Items.Add("Number: " & i)
total += i
i += 1
Loop
Dim average As Double = total / 5
lstNumbers.Items.Add("Total: " & total)
lstNumbers.Items.Add("Average: " & average)