100% found this document useful (1 vote)
2K views

VB Exercises Loops

The document contains examples of using for, while, and do while loops in VB.NET. It provides exercises to print patterns using for loops, prompt the user to answer questions in a loop until they choose to stop, and print ASCII character codes using while and do while loops. The solutions demonstrate using loops to iterate through conditions and print output in VB.NET code.

Uploaded by

S Ahmad1311993
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

VB Exercises Loops

The document contains examples of using for, while, and do while loops in VB.NET. It provides exercises to print patterns using for loops, prompt the user to answer questions in a loop until they choose to stop, and print ASCII character codes using while and do while loops. The solutions demonstrate using loops to iterate through conditions and print output in VB.NET code.

Uploaded by

S Ahmad1311993
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

By: http://www.worldbestlearningcenter.

com

VB exercisesloops exercises
VB.NET Loops: for loop exercises
Exercise 1: Write VB.NET code to produce the output shown below:
*******
******
*****
****
***
**
*
Solution:

Module Module1
Sub Main()
Dim i, j As Integer
For i = 0 To 6
For j = 1 To 7 - i
Console.Write("*")
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
Exercise 2: Write VB.NET code to print the following pattern:

1******
12*****
123****

By: http://www.worldbestlearningcenter.com

1234***
12345**
123456*
1234567
Solution:

Module Module1
Sub Main()
Dim i, j, k As Integer
For i = 1 To 7
For j = 1 To i
Console.Write(j)
Next
For k = 7 - i To 1 Step -1
Console.Write("*")
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
VB.NET Loops: while loop exercises
Exercise 1:
Write VB.NET program to prompt the user to choose the correct answer from a list of answer
choices of a question. The user can choose to continue answering the question or stop answering it.
See the example below:
What is the command keyword to exit a loop in VB.NET?
a. int
b. continue

By: http://www.worldbestlearningcenter.com

c. break
d. Exit For/Exit While/Exit Do While
Enter your choice: b
Incorrect!
Again? press y to continue:
Solution:

Module Module1
Sub Main()
Dim ch, con As String
con = "y"
Console.WriteLine("What is the correct way to declare a variable to store an integer value in
VB.NET?")
Console.WriteLine("a. Int")
Console.WriteLine("b. Continue")
Console.WriteLine("c. Break")
Console.WriteLine("d. Exit For/Exit While/Exit Do While")

While String.Compare(con, "y") = 0


Console.Write("Enter your choice:")
ch = Console.ReadLine().ToString()
If (String.Compare(ch, "d") = 0) Then
Console.WriteLine("Congratulation!")
Else : Console.WriteLine("Incorrect!")
End If
Console.Write("Continue? Press y to continue:")
con = Console.ReadLine().ToString()
End While
Console.ReadLine()
End Sub
End Module

By: http://www.worldbestlearningcenter.com

Exercise 2: Write VB.NET program to print the table of characters that are equivalent to the Ascii
codes from 1 to 122.The program will print the 10 characters per line.
Solution:

Module Module1
Sub Main()
Dim i As Integer = 1
While i <= 122
Console.Write("{0,4}", Chr(i).ToString())
If (i Mod 10 = 0) Then
Console.WriteLine()
End If
i=i+1
End While
Console.ReadLine()
End Sub
End Module

VB.NET Loops: do while loop exercises


Exercise 1:
By using do while loop, write VB.NET program to prompt the user to choose the correct answer
from a list of answer choices of a question. The user can choose to continue answering the question
or stop answering it. See the example below:
What is the correct way to declare a variable to an integer value in VB.NET?
a. int a
b. Dim a As Single
c. Dim a As String
d. Dim a As Integer
Enter your choice: b
Incorrect!
Again? press y to continue:

By: http://www.worldbestlearningcenter.com

Solution:

Module Module1
Sub Main()
Dim ch, con As String
con = "y"
Console.WriteLine("What is the correct way to declare a variable to store an integer value in
VB.NET?")
Console.WriteLine("a. int a")
Console.WriteLine("b. Dim a as Single")
Console.WriteLine("c. Dim a as String")
Console.WriteLine("d. Dim a as Integer")
Do While String.Compare(con, "y") = 0
Console.Write("Enter your choice:")
ch = Console.ReadLine().ToString()
If (String.Compare(ch, "d") = 0) Then
Console.WriteLine("Congratulation!")
Else : Console.WriteLine("Incorrect!")
End If
Console.Write("Continue? Press y to continue:")
con = Console.ReadLine().ToString()
Loop
Console.ReadLine()
End Sub
End Module
Exercise 2: By using do while loop, write VB.NET program to print the table of characters that are
equivalent to the Ascii codes from 1 to 122.The program will print the 10 characters per line.

Solution:

Module Module1
Sub Main()
Dim i As Integer = 1
Do While i <= 122
Console.Write("{0,4}", Chr(i).ToString())
If (i Mod 10 = 0) Then

By: http://www.worldbestlearningcenter.com

Console.WriteLine()
End If
i=i+1
Loop
Console.ReadLine()
End Sub
End Module

For more VB exercises and example programs visit http://www.worldbestlearningcenter.com

You might also like