This document introduces loops, which allow code to be repeated without having to manually write out each repetition. It provides examples of FOR and WHILE loops in Visual Basic code. FOR loops repeat a certain number of times based on a counter, stopping when the counter reaches a limit. WHILE loops continuously repeat until a condition is no longer met. Loops help programmers write cleaner, more efficient code compared to manually writing out repetitive code multiple times.
2. Very Often, You Will Want to Do
Something More Than Once
HA HA
HA HA
HA HA
HA HA
HA HA
HA HA
3. You Could Code The ¡®Ha Ha¡¯ Like This
Console.WriteLine("Ha Ha ")
Console.WriteLine("Ha Ha ") But is actually Lame!
Console.WriteLine("Ha Ha ") No good programmer
Console.WriteLine("Ha Ha ") does this!
Console.WriteLine("Ha Ha ")
Console.WriteLine("Ha Ha ")
Console.WriteLine("Ha Ha ")
Console.WriteLine("Ha Ha ")
4. Loops To The Rescue!
? A loop is a piece of code which allows you to
repeat some code more than once without
having to write it out more than once.
Do that 8
times!
Console.WriteLine("Ha Ha ")
5. COUNTER = 0 The FOR Loop
For as long as the
counter isn¡¯t 8 carry FOR loops stop
on when their
counter runs out
Console.WriteLine("Ha Ha ")
COUNTER + 1
6. FOR Loops in VB
Module Module1
Sub Main()
Dim count As Integer
For count = 1 To 8
Console.WriteLine("HA HA!")
Next
Console.ReadLine()
End Sub
End Module
7. STOP = no The WHILE Loop
While stop is still no
carry on
WHILE may
never stop!
Console.WriteLine("Ha Ha ")
STOP = yes
8. Module Module1 WHILE Loops in VB
Sub Main()
Dim stopProgram As String
stopProgram = "no"
While stopProgram = "no"
Console.WriteLine("HA HA!")
Console.WriteLine("HA HA!")
Console.WriteLine("HA HA!")
Console.WriteLine("Do you want to stop?")
stopProgram = Console.ReadLine()
End While
Console.WriteLine("GOODBYE!")
Console.ReadLine()
End Sub
End Module