You are on page 1of 4

Visual Programming

Assignment: 2
Submitted To: Sir Asfandyar
Name: Syed Hamza Sherazi
Roll No: 47611
Dated: 17-01-2021
Problem Overview

Scenario: You have been asked to write a VB program which will demonstrate the loops like
For Next, While and For Each Next Loop using Nested Loop concept?
Resources required

▪ A computer running Microsoft Visual Studio with Visual Basic Language


▪ General Loops Programming logic
Hints:

Nested Loops

You can nest For Each loops by putting one loop within another.

The following example demonstrates nested For Each…Next structures.

Example:

' Create lists of numbers and letters ' by using array initializers.

Dim numbers() As Integer = {1, 4, 7}

Dim letters() As String = {"a", "b", "c"}

' Iterate through the list by using nested loops.

For Each number As Integer In numbers

For Each letter As String In letters

MsgBox(number.ToString & letter & " ")

Next

Next
1
MsgBox ("")

'Output: 1a 1b 1c 4a 4b 4c 7a 7b 7c

Code:

Public Class Form1

Dim numbers() As Integer = {1, 4, 7}

Dim letters() As String = {"a", "b", "c"}

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

For index = 0 To numbers.Length - 1

For i = 0 To letters.Length - 1

MsgBox(numbers(index) & letters(i) & " ")

Next

Next

MsgBox("")

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Dim index As Integer = 0

While index <= numbers.Length - 1

Dim i As Integer = 0

While i <= letters.Length - 1

MsgBox(numbers(index) & letters(i) & " ")

i += 1

End While

index += 1

End While

MsgBox("")

2
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

For Each number As Integer In numbers

For Each letter As String In letters

3
4

You might also like