Lecture 6 Labwork
Lecture 6 Labwork
NET Do Loop
A Loop is used to repeat the same process multiple times until it meets the specified
condition in a program. By using a loop in a program, a programmer can repeat any
number of statements up to the desired number of repetitions. A loop also provides the
suitability to a programmer to repeat the statement in a program according to the
requirement. A loop is also used to reduce the program complexity, easy to understand,
and easy to debug.
Types of Loops
There are five types of loops available in VB.NET
o Do While Loop
o For Next Loop
o For Each Loop
o While End Loop
o With End Loop
Do While Loop
In VB.NET, Do While loop is used to execute blocks of statements in the program, as long
as the condition remains true. It is similar to the While End Loop
, but there is slight difference between them. The while loop initially checks the defined
condition, if the condition becomes true, the while loop's statement is executed. Whereas in
the Do loop, is opposite of the while loop, it means that it executes the Do statements, and then it
checks the condition.
Syntax:
1. Do
2. [ Statements to be executed]
3. Loop While Boolean_expression
4. // or
5. Do
6. [Statement to be executed]
7. Loop Until Boolean_expression
In the above syntax, the Do keyword followed a block of statements, and While keyword
checks Boolean_expression after the execution of the first Do statement.
Flowchart of Do loop
The above flow chart represents the flow of Do While loop. It is used to control the flow of
statements, such that it executes the statement at least once before checking the While or Until
condition. If the condition is true, the next iteration will be executed till the condition become
false.
Example 1. Write a simple program to print a number from 1 to 10 using the Do While loop in
VB.NET.
Do_loop.vb
1. Imports System
2. Module Do_loop
3. Sub Main()
4. ' Initializatio and Declaration of variable i
5. Dim i As Integer = 1
6. Do
7. ' Executes the following Statement
8. Console.WriteLine(" Value of variable I is : {0}", i)
9. i = i + 1 'Increment the variable i by 1
10. Loop While i <= 10 ' Define the While Condition
11.
12. Console.WriteLine(" Press any key to exit...")
13. Console.ReadKey()
14. End Sub
15. End Module
Now compile and execute the above program by clicking on the Start button, it shows the
following output:
In the above program, the Do While loop executes the body until the given condition
becomes false. When the condition becomes false the loop will be terminated.
Do_loop.vb
1. Imports System
2. Module Do_loop
3. Sub Main()
4. ' Initialization and Declaration of variable i
5. Dim i As Integer = 1
6. Do
7. ' Executes the following Statement
8. Console.WriteLine(" Value of variable i is : {0}", i)
9. i = i + 1 'Increment variable i by 1
10. Loop Until i = 10 ' Define the Until Condition
11.
12. Console.WriteLine(" Press any key to exit...")
13. Console.ReadKey()
14. End Sub
15. End Module
Output:
In the above program, a Do Until loop is executed their statement until the given condition Until
(i =10) is not meet. When the counter value of the variable i becomes 10, the defined statement
will be false, and the loop will be terminated.
Syntax
1. Do
2. // Statement of the outer loop
3. Do
4. // Statement of outer loop
5. While (condition -2)
6. // Statement of outer loop
7. While (condition -1)
Example 2: Write a simple program to use the Do While loop Statement in VB.NET.
Nest_Do_While.vb
1. Imports System
2. Module Nest_Do_While
3. Sub Main()
4. ' Declare i and j as Integer variable
5. Dim i As Integer = 1
6. Do
7. ' Outer loop statement
8. Console.WriteLine(" Execution of Outer loop is {0}", i & " times")
9.
10. Dim j As Integer = 1
11.
12. Do
13. 'Inner loop statement
14. Console.WriteLine(" Execution of Inner loop is {0}", j)
15. j = j + 1 ' Increment Inner Counter variable by 1
16. Loop While j < 3
17.
18. Console.WriteLine()
19. i = i + 1 ' Increment Outer Counter variable by 1
20. Loop While i < 4
21. Console.WriteLine(" Exit from the loop")
22. Console.WriteLine(" Press any key to exit...")
23. Console.ReadKey()
24. End Sub
25. End Module
Output:
In the above example, each iteration of the outer loop also executes the inner loop
repeatedly until the inner condition becomes false. When the condition of the outer loop
becomes false, the execution of the outer and inner loop will be terminated.
For Next Loop
A For Next loop is used to repeatedly execute a sequence of code or a block of code
until a given condition is satisfied. A For loop is useful in such a case when we know how
many times a block of code has to be executed. In VB.NET, the For loop is also known as
For Next Loop.
Syntax
In the above flow chart, the first step is to initialize the variable name with the start value.
And then, the value of the variable will be compared to the end expression or value. If
the condition is true, the control enters the loop body and executes the statements. After
that, the value of a variable will be automatically incremented by the compiler.
Upon completion of each iteration, the current value of a variable will be again
compared to the end expression. If the condition is not true, the controlled exit from the
loop.
Example 1. Write a simple program to print the number from 1 to 10 using the For Next
loop.
Number.vb
1. Imports System
2. Module Number
3. Sub Main()
4. ' It is a simple print statement, and 'vbCrLf' is used to jump in the next line.
5. Console.Write(" The number starts from 1 to 10 " & vbCrLf)
6. ' declare and initialize variable i
7. For i As Integer = 1 To 10 Step 1
8. ' if the condition is true, the following statement will be executed
9. Console.WriteLine(" Number is {0} ", i)
10. ' after completion of each iteration, next will update the variable counter
11. Next
12. Console.WriteLine(" Press any key to exit... ")
13. Console.ReadKey()
14. End Sub
15. End Module
Output:
In the above example, we have initialized an integer variable i with an initial value 1. The
For loop will continuously execute its body until the value of i is smaller or equal to 10.
After each iteration, the value of i is automatically increased with 'Step 1'. If the value of i
reached 10, the loop would be terminated and control transfer to the Main() function.
Further, we can change the Step in For Next loop. Write the following program to skip
the number is 2.
Number.vb
1. Imports System
2. Module Number
3. Sub Main()
4. ' declaration of variable i
5. Dim i As Integer
6. Console.Write(" Skip one number at the completion of each iteration in between 1 t
o 10 " & vbCrLf)
7. ' initialize i to 1 and declare Step to 2 for skipping a number
8. For i = 1 To 10 Step 2
9. ' if condition is true, it skips one number
10. Console.WriteLine(" Number is {0} ", i)
11. ' after completion of each iteration, next will update the variable counter t
o step 2
12. Next
13. Console.WriteLine(" Press any key to exit... ")
14. Console.ReadKey()
15. End Sub
16. End Module
Output:
As we can see in the above output, the value of the variable i is initialized with 1, and
the value of i is skipped by 'Step 2' in the loop for each iteration to print the skipped
number from 1 to 10.
Example 2: Write a simple program to print a table in the VB.NET.
Table.vb
1. Imports System
2. Module Table
3. Sub Main()
4. 'declaration of i and num variable
5. Dim i, num As Integer
6. Console.WriteLine(" Enter any number to print the table")
7. num = Console.ReadLine() ' accept a number from the user
8. Console.WriteLine(" Table of " & num)
9. 'define for loop condition, it automatically initialize step to 1
10. For i = 1 To 10
11. Console.WriteLine(num & " * " & i & " = " & i * num)
12. Next
13. Console.ReadKey()
14. End Sub
15. End Module
Output:
Nested For Next Loop in VB.NET
In VB.NET, when we write one For loop inside the body of another For Next loop, it is
called Nested For Next loop.
Syntax:
Nested_loop.vb
1. Imports System
2. Module Nested_loop
3. Sub Main()
4. Dim i, j As Integer
5. For i = 1 To 3
6. 'Outer loop
7. Console.WriteLine(" Outer loop, i = {0}", i)
8. 'Console.WriteLine(vbCrLf)
9.
10. 'Inner loop
11. For j = 1 To 4
12. Console.WriteLine(" Inner loop, j = {0}", j)
13. Next
14. Console.WriteLine()
15. Next
16. Console.WriteLine(" Press any key to exit...")
17. Console.ReadKey()
18. End Sub
19. End Module
Output:
In the above example, at each iteration of the outer loop, the inner loop is repeatedly
executed its entire cycles until the condition is not satisfied.
Pattern.vb
1. Imports System
2. Module Pattern
3. Sub Main()
4. Dim i, n, j As Integer
5. Console.WriteLine(" Enter a number to show rows in a Pattern")
6. ' take a number from user
7. n = Console.ReadLine()
8.
9. 'Outer loop
10. For i = 1 To n
11. 'Inner loop
12. 'value of j should be less than i
13. For j = 1 To i
14. Console.Write(" * ")
15. Next
16. Console.WriteLine("")
17. Next
18. Console.WriteLine(" Press any key to exit...")
19. Console.ReadKey()
20. End Sub
21. End Module
Output:
VB.NET For Each Loop
In the VB.NET, For Each loop is used to iterate block of statements in an array or
collection objects. Using For Each loop, we can easily work with collection objects such as
lists, arrays, etc., to execute each element of an array or in a collection. And when iteration
through each element in the array or collection is complete, the control transferred to the
next statement to end the loop.
Syntax:
For Each loop is used to read each element from the collection object or an array.
The Data Type represents the type of the variable, and var_name is the name of the
variable to access elements from the array or collection object so that it can be used in
the body of For Each loop.
Write a simple program to understand the uses of For Each Next loop in VB.NET.
For_Each_loop.vb
1. Imports System
2. Module For_Each_loop
3. Sub Main()
4. 'declare and initialize an array as integer
5. Dim An_array() As Integer = {1, 2, 3, 4, 5}
6. Dim i As Integer 'Declare i as Integer
7.
8. For Each i In An_array
9. Console.WriteLine(" Value of i is {0}", i)
10. Next
11. Console.WriteLine("Press any key to exit...")
12. Console.ReadLine()
13. End Sub
14. End Module
Output:
In the above example, we create an integer array with the name An_array (), and For Each
loop is used to iterate each element of the array with the help of defined variable 'i'.
Example 2: Write a simple program to print fruit names using For Each loop in VB.NET.
For_each.vb
1. Imports System
2. Module For_each
3. Sub Main()
4. 'Define a String array
5. Dim str() As String
6.
7. 'Initialize all element of str() array
8. str = {"Apple", "Orange", "Mango", "PineApple", "Grapes", "Banana"}
9.
10. Console.WriteLine("Fruit names are")
11.
12. 'Declare variable name as fruit
13. For Each fruit As String In str
14. Console.WriteLine(fruit)
15. Next
16. Console.WriteLine(" Press any key to exit...")
17. Console.ReadKey()
18. End Sub
19. End Module
Output:
In this example, str() is a String type array that defines different fruits names. And fruit is
the name of a variable that is used to iterate each element of the str() array using For
Each loop in the program. If all the element is read, control passes to the Main() function
to terminate the program.
VB.NET While End Loop
The While End loop is used to execute blocks of code or statements in a program, as
long as the given condition is true. It is useful when the number of executions of a block
is not known. It is also known as an entry-controlled loop statement, which means it
initially checks all loop conditions. If the condition is true, the body of the while loop is
executed. This process of repeated execution of the body continues until the condition is
not false. And if the condition is false, control is transferred out of the loop.
Syntax:
1. While [condition]
2. [ Statement to be executed ]
3. End While
Here, condition represents any Boolean condition, and if the logical condition is true,
the single or block of statements define inside the body of the while loop is executed.
Example: Write a simple program to print the number from 1 to 10 using while End loop
in VB.NET.
while_number.vb
1. Imports System
2. Module while_number
3. Sub Main()
4. 'declare x as an integer variable
5. Dim x As Integer
6. x=1
7. ' Use While End condition
8. While x <= 10
9. 'If the condition is true, the statement will be executed.
10. Console.WriteLine(" Number {0}", x)
11. x = x + 1 ' Statement that change the value of the condition
12. End While
13. Console.WriteLine(" Press any key to exit...")
14. Console.ReadKey()
15. End Sub
16. End Module
Output:
In the above example, while loop executes its body or statement up to the defined state
(i <= 10). And when the value of the variable i is 11, the defined condition will be false;
the loop will be terminated.
Example 2: Write a program to print the sum of digits of any number using while End
loop in VB.NET.
Total_Sum.vb
Output:
Syntax
Write a program to understand the Nested While End loop in VB.NET programming.
Nest_While.vb
1. Imports System
2. Module Nest_While
3. Sub Main()
4. ' Declare i and j as Integer variable
5. Dim i As Integer = 1
6.
7.
8. While i < 4
9. ' Outer loop statement
10. Console.WriteLine(" Counter value of Outer loop is {0}", i)
11. Dim j As Integer = 1
12.
13. While j < 3
14. 'Inner loop statement
15. Console.WriteLine(" Counter value of Inner loop is {0}", j)
16. j = j + 1 ' Increment Inner Counter variable by 1
17. End While
18. Console.WriteLine() 'print space
19. i = i + 1 ' Increment Outer Counter variable by 1
20. End While
21. Console.WriteLine(" Press any key to exit...")
22. Console.ReadKey()
23. End Sub
24. End Module
Output:
In the above example, at each iteration of the outer loop, the inner loop is repeatedly
executed its entire cycles until the inner condition is not satisfied. And when the condition
of the outer loop is false, the execution of the outer and inner loop is terminated.
VB.NET With End With Statement
In VB.NET, the With End statement is not the same as a loop structure. It is used to access
and execute statements on a specified object without specifying the name of the objects
with each statement. Within a With statement block, you can specify a member of an
object that begins with a period (.) to define multiple statements.
Syntax:
1. With objExpression
2. [ Statements to be Executed]
3. End With
objExpression: It defines the data type of objExpression. It may be any class or structure
type or basic data type such as Integer. It can be executed once in the With End statement.
Statement: It defines one or more executed statements within the With block. The
statement refers to the member of the object that links with objectExpression to execute
the With statement block.
Example: Write a program to understand the uses of With End statement in VB.NET.
Employee.vb
Output:
Example: Write a program to display the student details using the With End statement in
VB.NET.
With_Statement.vb
1. Imports System
2. Module With_statement
3. Sub Main()
4. ' Create a stud object
5. Dim stud As Student = New Student()
6.
7. ' To define the member of an object using With Statement
8. With stud
9. .stud_name = " Mr. Robert"
10. .stud_course = "Computer Science"
11. .stud_rollno = "01"
12. End With
13.
14. With stud
15. ' use stud as a reference
16. Console.WriteLine(" Student Name is : {0}", .stud_name)
17. Console.WriteLine(" Student Course Name is : {0}", .stud_course)
18. Console.WriteLine(" Student RollNo. is : {0}", .stud_rollno)
19. End With
20. Console.WriteLine(" Press any key to exit...")
21. Console.ReadKey()
22. End Sub
23.
24. ' Create a Class Student
25. Public Class Student
26. Public stud_name As String 'Define the variable of a class
27. Public stud_course As String
28. Public stud_rollno As Integer
29. End Class
30. End Module
Output:
VB.NET Exit Statement
In VB.NET, the Exit statement is used to terminate the loop (for, while, do, select case,
etc.) or exit the loop and pass control immediately to the next statement of the
termination loop. Furthermore, the Exit statement can also be used in the nested loop to
stop or terminate the execution of the inner or outer loop at any time, depending on our
requirements.
Syntax
Generally, the Exit statement is written along with a condition. If the Exit condition
is true inside the loop, it exits from the loop and control transfer to the next statement,
followed by the loop. And if the Exit condition fails for the first time, it will not check
any statement inside the loop and terminates the program.
We will now see how to use the Exit statement in loops and Select case statements to
finish the program's execution in the VB.NET programming language.
Exit_While.vb
1. Imports System
2. Module Exit_While
3. Sub Main()
4. ' Definition of count variable
5. Dim count As Integer = 1
6.
7. ' Execution of While loop
8. While (count < 10)
9. ' Define the Exit condition using If statement
10. If count = 5 Then
11. Exit While ' terminate the While loop
12. End If
13. Console.WriteLine(" Value of Count is : {0}", count)
14. count = count + 1
15. End While
16. Console.WriteLine(" Exit from the While loop when count = {0}", count)
17. Console.WriteLine(" Press any key to exit...")
18. Console.ReadKey()
19. End Sub
20. End Module
Output:
In the above example, the While End loop is continuously executed, its body until the
given condition While (count < 10) is not satisfied. But when the Exit condition (count
= 5) falls inside a while loop, the execution of the loop is automatically terminated, and
control moves to the next part of the loop statement.
1. Imports System
2. Module Exit_For
3. Sub Main()
4. 'Definition of num variable
5. Dim num As Integer
6. Dim sum As Double = 0.0
7.
8. 'Execution of For loop
9. For i As Integer = 1 To 10
10. 'Accept a number from the user
11. Console.WriteLine("Enter a number : ")
12. num = Console.ReadLine()
13. ' If the user enters a negative number, the loop terminates
14. If num < 0 Then
15. Exit For ' terminate the For loop
16. End If
17. sum += num
18. Next
19.
20. Console.WriteLine(" Exit from the For loop when (num < 0) is: {0}", num)
21. Console.WriteLine(" Total sum is : {0}", sum)
22. Console.WriteLine(" Press any key to exit from the Console Screen")
23. Console.ReadKey()
24. End Sub
25. End Module
Output:
The above program accepts a number from the user until it encounters a negative or less
than 0 (num < 0). And when there is a negative number, the Exit statement terminates
the loop, and the control transfer to the next part of the loop.
Exit_Do_While.vb
1. Imports System
2. Module Exit_Do_While
3. Sub Main()
4. 'Definition of the count variable
5. Dim count As Integer = 20
6.
7. ' Definition of Do While loop
8. Do
9. 'Define the Exit condition using If statement.
10.
11. If count = 25 Then
12. Exit Do ' terminate the Do While loop
13. End If
14. Console.WriteLine(" Value of Count is : {0}", count)
15. count = count + 1
16. Loop While (count < 50)
17. Console.WriteLine(" Exit from the Do While loop when count = {0}", count)
18. Console.WriteLine(" Press any key to exit...")
19. Console.ReadKey()
20. End Sub
21. End Module
Output:
In the above example, the Do While loop is continuously executed, its body until the given
condition While (count < 50) is not satisfied. But when the Exit condition (count =
25) is encountered, the Do While loop is automatically terminated. The control is
immediately transferred to the next statement, followed by the loop statements.
VB.NET Continue Statement
In VB.NET, the continue statement is used to skip the particular iteration of the loop and
continue with the next iteration. Generally, the continue Statement is written inside the
body of the For, While and Do While loop with a condition. In the previous section, we
learned about the Exit Statement. The main difference between the Exit and
a Continue Statement is that the Exit Statement
is used to exit or terminate the loop's execution process. In contrast, the Continue Statement is used
to Skip the particular iteration and continue with the next iteration without ending the loop.
Syntax:
.
In the above diagram, a Continue Statement is placed inside the loop to skip a particular
statement or iteration. Generally, a continue statement is used with a condition. If the
condition is true, it skips the particular iteration and immediately transfers the control to
the beginning of the loop for the execution of the next iteration.
Now we will see how to use the Continue statement in the loop to skip the execution of
the code and send the control to the beginning of the loop to execute further statements
in the VB.NET programming language.
.Continue_While.vb
1. Imports System
2. Module Continue_While
3. Sub Main()
4. 'Declaration and initialization of variable i
5. Dim i As Integer = 10
6.
7. 'Define the While Loop Condition
8. While i < 20
9.
10. If i = 14 Then
11. Console.WriteLine(" Skipped number is {0}", i)
12. i += 1 ' skip the define iteration
13. Continue While
14. End If
15. Console.WriteLine(" Value of i is {0}", i)
16.
17. i += 1 ' Update the variable i by 1
18. End While
19. Console.WriteLine(" Press any key to exit...")
20. Console.ReadKey()
21. End Sub
22. End Module
Output:
In the above program, the While loop is continuously executed, their body until the given
condition ( i < 20) is met. But when the value of i is equal to 15, the Continue statement
is encountered. It skips the current execution and transfers the control to the beginning
of the loop to display the next iteration.
.Continue_For.vb
1. Imports System
2. Module Continue_For
3. Sub Main()
4. 'Declaration and initialization of variable i, num
5. Dim i As Integer = 10
6. Dim num As Integer
7. 'Define the For Loop Condition
8. For i = 10 To 1 Step -1
9.
10. If i = 5 Then ' if i = 5, it skips the iteration
11. num = i 'Assign the skip value to num variable
12. Continue For ' Continue with Next Iteration
13. End If
14. Console.WriteLine(" Value of i is {0}", i)
15.
16. Next
17. Console.WriteLine(" Skipped number is {0}", num)
18. Console.WriteLine(" Press any key to exit...")
19. Console.ReadKey()
20. End Sub
21. End Module
Output:
In the above program, the For loop is continuously decremented their body until the
variable i is 1. But when the value of i is equal to 5, the Continue Statement is
encountered. Then it skips the current execution and transfers control to the beginning
of the loop to display the next iteration.
Continue_Do_While.vb
1. Imports System
2. Module Continue_Do_While
3. Sub Main()
4. 'Declaration and initialization of local variable
5. Dim i As Integer = 10, num As Integer
6.
7. 'Definition the Do While Loop
8. Do
9. If i = 14 Then
10. num = i
11. i += 1 ' skip the define iteration
12. Continue Do
13. End If
14. Console.WriteLine(" Value of i is {0}", i)
15.
16. i += 1 ' Update the variable i by 1
17. Loop While i < 20
18. Console.WriteLine(" Skipped number is {0}", num)
19.
20. Console.WriteLine(" Press any key to exit...")
21. Console.ReadKey()
22. End Sub
23. End Module
Output:
is continuously executed their body until the given condition ( i < 20) is met. But when the value of i is
equal to 15, the Continue Statement is encountered. It skips the current execution and transfers the
control to the beginning of the loop to display the next iteration.
VB.NET GoTo Statement
In VB.NET, the GoTo statement is known as a jump statement. It is a control statement
that transfers the flow of control to the specified label within the procedure. The GoTo
statement uses labels that must be a valid identifier. The GoTo statement can be used in
Select case, decision control statements, and loops.
Syntax:
1. GoTo label_1
Here, GoTo is a keyword, and label_1 is a label used to transfer control to a specified label
statement in a program.
Now we will see how to use the GoTo statement in the loop, select case, or decision-
making statement to transfer control to the specified label statement in the VB.NET
program.
Goto_Statement.vb
1. Imports System
2. Module Goto_Statement
3. Sub Main()
4. 'Declaration of local variable
5. Dim num As Integer
6. Console.WriteLine(" Enter the number :")
7. num = Console.ReadLine ' Accept a number from the user
8. If (num Mod 2 = 0) Then
9. GoTo even ' Jump to even label
10. Else
11. GoTo odd ' Jump to odd label
12. End If
13. odd:
14. Console.WriteLine(" It is an Odd number")
15.
16. even:
17. Console.WriteLine(" It is an Even number ")
18. Console.WriteLine(" Press any key to exit...")
19. Console.ReadKey()
20. End Sub
21. End Module
Output:
In the above program, when we enter a number, it checks whether the number is even or
odd. And if the number is odd, the GoTo statement will be encountered, and the control
transfers to the odd statement Else the control transfer to the even statement.
Goto_For.vb
1. Imports System
2. Module Goto_For
3. Sub Main()
4. 'Declaration of local variable
5. Dim i, n As Integer, Sum As Integer = 0
6.
7. ' Define For Loop Statement
8. For i = 1 To 10
9. Console.WriteLine(" Value of i is {0}", i)
10. Sum = Sum + i 'At each iteration the value of i added to Sum
11. If i = 5 Then
12. n = i 'Assign i to n
13.
14. GoTo total_sum ' Jump to total_sum
15. End If
16. Next
17. total_sum:
18. Console.WriteLine(" Total sum is {0}", Sum)
19. Console.WriteLine(" GoTo Encounter, when value of i = {0}", n)
20. Console.WriteLine(" Press any key to exit...")
21. Console.ReadKey()
22. End Sub
23. End Module
Output:
In the above program, the For loop is executed till the given condition ( i = 1 To 10). And
when the value of i is equal to 5, the GoTo statement will be encountered, and it transfers
the control to total_sum so that the total sum of each iteration can be printed in the For
loop.
Goto_Select.vb
1. Imports System
2. Module Goto_Select
3. Sub Main()
4. 'Definition of local variable
5. Dim Days As String
6. Days = "Thurs"
7. Select Case Days
8.
9. Case "Mon"
10. Case1:
11.
12. Console.WriteLine(" Today is Monday")
13. Case "Tue"
14. Console.WriteLine(" Today is Tuesday")
15. Case "Wed"
16. Console.WriteLine("Today is Wednesday")
17. Case "Thurs"
18. Console.WriteLine("Today is Thursday")
19. GoTo Case1
20. Case "Fri"
21. Console.WriteLine("Today is Friday")
22. Case "Sat"
23. Console.WriteLine("Today is Saturday")
24. Case "Sun"
25. Console.WriteLine("Today is Sunday")
26. Case Else
27. Console.WriteLine(" Something wrong")
28.
29. End Select
30. Console.WriteLine("You have selected : {0}", Days)
31. Console.WriteLine("Press any key to exit...")
32. Console.ReadLine()
33. End Sub
34. End Module
Output:
In the Select case statement, the value of Days Thurs will compare the values of all
selected cases available in a program. If it matches any statement, it prints the particular
statement and contains the GoTo statement that transfers control to defined case1: and
then the following statement is executed.
1. Imports System
2. Module GoTo_While
3. Sub Main()
4. 'Declaration and initialization of the local variable
5. Dim i As Integer = 1, num As Integer
6. start1:
7. 'Definition of While Loop
8. While i < 10
9. If i = 6 Then
10. num = i
11. i += 1
12. GoTo start1 'transfer control at start1
13. End If
14. Console.WriteLine(" Value of i is {0}", i)
15.
16. i += 1 ' Update the variable i by 1
17. End While
18. Console.WriteLine(" Control transfer at number {0}", num)
19.
20. Console.WriteLine(" Press any key to exit...")
21. Console.ReadKey()
22. End Sub
23. End Module
Output: