0% found this document useful (0 votes)
69 views

Lecture 6 Labwork

The document discusses loops in VB.NET. There are five main types of loops: Do While, For Next, For Each, While End, and With End loops. The Do While loop executes blocks of code as long as a condition is true, checking the condition after each iteration. The For Next loop iterates through a block of code a specified number of times, using a counter variable that is initialized, checked against an end value, and incremented by an optional step value each iteration. Nested loops can be used to repeat loops multiple times within other loops. Examples demonstrate using Do While and For Next loops to print numbers from 1 to 10.

Uploaded by

Mercy Jeptanui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Lecture 6 Labwork

The document discusses loops in VB.NET. There are five main types of loops: Do While, For Next, For Each, While End, and With End loops. The Do While loop executes blocks of code as long as a condition is true, checking the condition after each iteration. The For Next loop iterates through a block of code a specified number of times, using a counter variable that is initialized, checked against an end value, and incremented by an optional step value each iteration. Nested loops can be used to repeat loops multiple times within other loops. Examples demonstrate using Do While and For Next loops to print numbers from 1 to 10.

Uploaded by

Mercy Jeptanui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

VB.

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.

Advantages of VB.NET loop

o It provides code iteration functionality in a program.


o It executes the statement until the specified condition is true.
o It helps in reducing the size of the code.
o It reduces compile time.

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.

Use of Until in Do Until Loop statement


In the VB.NET loop, there is a Do Until loop statement, which is similar to the Do While
loop. The Do Statement executes as long as Until condition becomes true.

Example: Write a program to understand the uses of Do Until Loop in VB.NET.

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.

Nested Do While Loop Statement


In VB.NET, when we use one Do While loop inside the body of another Do While loop, it
is called Nested Do While loop.

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

1. For variable_name As [ DataType ] = start To end [ Step step ]


2. [ Statements to be executed ]
3. Next

Let's understand the For Next loop in detail.

o For: It is the keyword that is present at the beginning of the definition.


o variable_name: It is a variable name, which is required in the For loop Statement. The
value of the variable determines when to exit from the For-Next loop, and the value
should only be a numeric.
o [Data Type]: It represents the Data Type of the variable_name.
o start To end: The start and end are the two important parameters representing the initial
and final values of the variable_name. These parameters are helpful while the execution
begins, the initial value of the variable is set by the start. Before the completion of each
repetition, the variable's current value is compared with the end value. And if the value of
the variable is less than the end value, the execution continues until the variable's current
value is greater than the end value. And if the value is exceeded, the loop is terminated.
o Step: A step parameter is used to determine by which the counter value of a variable is
increased or decreased after each iteration in a program. If the counter value is not
specified; It uses 1 as the default value.
o Statements: A statement can be a single statement or group of statements that execute
during the completion of each iteration in a loop.
o Next: In VB.NET a Next is a keyword that represents the end of the For loop's
Flowchart of For Next loop
The following flowchart represents the functioning of the For Next loop in the VB.NET
programming language.

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:

1. For variable_name As [Data Type] = start To end [ Step step ]


2. For variable_name As [Data Type] = start To end [ Step step ]
3. [ inner loop statements ]
4. Next
5. [ Outer loop statements ]
6. Next

Following is the example of Nested For Next loop in VB.NET.

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.

Example 2: Write a program to print a pattern in VB.NET.

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:

1. For Each var_name As [ DataType ] In Collection_Object


2. [ Statements to be executed]
3. Next

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.

Flowchart of For Each loop


The following flowchart represents the For Each Next loop's functioning to iterate through
array elements in the VB.NET programming language.
The first step is to initialize an array or collection object to execute each element of the
array with the help of variables in For Each loop. A variable is used in For Each loop to
checks whether the element is available or not. If the element is available in the collection
object, the For Each block will be executed until the condition remains true. After the
execution of each element of an array, the control transfer to the end statement.

Examples 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.

Flow Diagram of the While End Loop in VB.NET


As we know, the While End loop is an entry-controlled loop used to determine if the
condition is true, the statements defined in the body of the loop are executed, and the
execution process continues till the condition is satisfied. Furthermore, after each
iteration, the value of the counter variable is incremented. It again checks whether the
defined condition is true; And if the condition is again true, the body of the While loop is
executed. And when the condition is not true, the control transferred to the end of the
loop.

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

1. Public Class Total_Sum ' Create a Class Total_sum


2. Shared Sub Main()
3. 'Declare an Integer variable
4. Dim n, remainder, sum As Integer
5. sum = 0
6.
7. Console.WriteLine(" Enter the number :")
8. n = Console.ReadLine() ' Accept a number from the user
9.
10. ' Use While loop and write given below condition
11. While (n > 0)
12. remainder = n Mod 10
13. sum += remainder
14. n = n / 10
15. End While
16. Console.WriteLine(" Sum of digit is :{0}", sum)
17. Console.WriteLine(" Press any key to exit...")
18. Console.ReadKey()
19. End Sub
20. End Class

Output:

Nested While End Loop


In VB.NET, when we write a While End loop inside the body of another While End loop, it
is called Nested While End loop.

Syntax

1. While (condition -1)


2. // body of the outer while loop
3. While (condition -2)
4. // body of inner while loop
5. End While
6. // body of the outer loop
7. End While

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.

End With: It is used to end the With block.

SQL CREATE TABLE

Example: Write a program to understand the uses of With End statement in VB.NET.

Employee.vb

1. Public Class Employee


2. ' definition of global variables
3. Public Property name As String
4. Public Property age As Integer
5. Public Property Occupation As String
6. Public Property email As String
7.
8. Shared Sub Main()
9. ' Create an emp object
10. Dim emp As New Employee
11. ' To define the member of an object
12. With emp
13. .name = " Mr. Stephen"
14. .age = 33
15. .Occupation = "Data Analyst"
16. .email = "[email protected]"
17. End With
18. With emp
19. ' use emp as a reference
20. Console.WriteLine(" Name is : {0}", .name)
21. Console.WriteLine(" Age is : {0}", .age)
22. Console.WriteLine(" Occupation is : {0}", .Occupation)
23. Console.WriteLine(" Employee Email is : {0}", .email)
24. End With
25. Console.WriteLine(" Press any key to exit...")
26. Console.ReadKey()
27. End Sub
28. End Class

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

1. Exit { Do | For | Function | Property | Select | Sub | Try | While }

The flow of Exit Statement


Following is the diagrammatical representation of Exit Statement in VB.NET programming
language.

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.

SQL CREATE TABLE

Use of Exit statement in While End loop


Example 1: Write a simple program to use the Exit Statement in While End loop.

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.

Use of Exit statement in For Next loop


Example 2: Write a program to calculate the sum of 10 numbers, and if a negative number
is entered, the For Each loop ends.

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.

Use of Exit statement in Do While loop


Example 3: Write a simple program to use the Exit Statement in Do While 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:

1. Continue { Do | For | While }

Flow Diagram of Continue Statement


Following is the pictorial representation of the Continue Statement in VB.NET
programming language

.
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.

Use of Continue statement in While End loop


Example 1: Write a simple program to use the Continue Statement in While End loop

.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.

Use of Continue statement in For Next loop


Example 2: Write a simple program to print the number from 10 to 1 with Continue
Statement in For Next loop

.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.

Use of Continue statement in Do While loop


Example 3: Write a simple program to Understand the use of Continue Statement in Do
While loop.

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:

In the above program, the Do 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.
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.

Use of GoTo statement in If Else


Example 1: Write a simple program to print whether the number is even or odd in GoTo
Statement.
HTML Tutorial

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.

Use of GoTo Statement in For Next loop


Example 2: Write a program to print the sum of the First ten numbers using the Goto
Statement in VB.NET.

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.

Use of GoTo statement in Select Case Statement


Example 3: Write a simple program to print the days' names in select cases using the
GoTo Statement.

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.

Use of GoTo statement in While End Loop Statement


Example 4: Write a simple program to understand the use of GoTo statement in the While
loop.
Goto_While.vb

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:

You might also like