Professional Documents
Culture Documents
NET, the control statements are the statements that controls the execution of the
program on the basis of the specified condition. It is useful for determining whether a
condition is true or not. If the condition is true, a single or block of statement is
executed. In the control statement, we will use if- Then, if Then Else, if Then ElseIf and
the Select case statement.
○ If-Then Statement
If-Then Statement
The If-Then Statement is a control statement that defines one or more conditions, and if
the particular condition is satisfied, it executes a piece of information or statements.
Syntax:
If condition Then
End If
If-Then-Else Statement
The If-Then Statement can execute single or multiple statements when the condition is
true, but when the expression evaluates to false, it does nothing. So, here comes the
If-Then-Else Statement. The IF-Then-Else Statement is telling what If condition to do
when if the statement is false, it executes the Else statement. Following is the
If-Then-Else statement syntax in VB.NET as follows:
Syntax:
If (Boolean_expression) Then
Else
Syntax
If(condition 1)Then
Else
' executes the default statement when none of the above conditions is true.
End If
In VB.NET, the Select Case statement is a collection of multiple case statements, which
allows executing a single case statement from the list of statements. A selected case
statement uses a variable to test for equality against multiple cases or statements in a
program. If the variable is matched with any test cases, that statement will be executed.
And if the condition is not matched with any cases, it executes the default statement.
Syntax
Case value1 'defines the item or value that you want to match.
Case value2 'defines the item or value that you want to match.
Case Else
End Select
vb.net
In the example above, since day is 3, the third value from the list ("Wednesday") is
chosen.
Switch Function: Definition: The Switch function evaluates a list of expressions and
returns the value of the first expression that is true. Each pair of arguments consists of
a condition and a result. If the condition is True, the result of that pair is returned.
Example:
Vb.net
grade = Switch(
True, "F"
In the example above, the function checks each condition. The condition score >= 80 is
the first true condition, so "B" is returned.
Loops
In VB.NET, loop statements allow you to execute a block of code multiple times based
on a condition or a set number of iterations. Let's delve into each type of loop available
in VB.NET.
1. For...Next Loop:
Example:
vb.net
For i As Integer = 1 To 5
Console.WriteLine(i)
Next
Example:
vb.net
Console.WriteLine(fruit)
Next
Example:
vb.net
num += 1
End While
4. Do...Loop While:
Example:
vb.net
Do
Console.WriteLine(num)
num += 1
5. Do...Loop Until:
Definition: Executes a block of statements and then continues to do so until a condition
becomes true.
Example:
vb.net
Do
Console.WriteLine(num)
num += 1
Arrays:
Definition: An array is a data structure that allows you to store multiple values of the
same type in a single variable. These values can be retrieved by referring to an index
number.
Example:
vb.net
Dim numbers(4) As Integer ' Declare an array that can hold 5 integers (0-4 inclusive)
numbers(0) = 1
numbers(1) = 2
Types of Array:
a. Single-dimensional Array:
Example:
vb.net
names(0) = "Alice"
names(1) = "Bob"
names(2) = "Charlie"
b. Multi-dimensional Array:
Definition: These arrays have more than one dimension, often visualized as matrices,
tables, or other geometric structures.
Example:
vb.net
Dim matrix(2, 2) As Integer ' 3x3 matrix
matrix(0, 0) = 1
matrix(0, 1) = 2
matrix(1, 0) = 3
c. Jagged Arrays:
Definition: An array of arrays, where each array can be of a different size. It's called
"jagged" because it doesn't have a consistent structure like a multi-dimensional array.
Example:
vb.net
In the above example, the first inner array has three elements, while the second one has
two.
2. Structures:
Definition: A structure (or Struct) in VB.NET is a composite data type, meaning it groups
together variables of different data types under a single name. Unlike classes,
structures are value types, so they are stored on the stack rather than the heap.
Example:
vb.net
Structure Book
End Structure
myBook.Pages = 500
Here, the Book structure groups together a title, author, and page count into a single
cohesive unit.
Collections:
Definition: Collections are classes designed to store and manage groups of related
objects. They can be indexed and iterated upon and provide various methods to add,
remove, or find items.
Types of Collections:
a. ArrayList:
Definition: A dynamic array that can hold items of any type. It automatically resizes itself
when needed.
Example:
vb.net
list.Add("Hello")
list.Add(123)
b. Hashtable:
Definition: Represents a collection of key/value pairs that are organized based on the
hash code of the key. It allows quick access to values based on their keys.
Example:
vb.net
hash("Name") = "John"
hash("Age") = 25
c. Stack:
Definition: Represents a last-in, first-out (LIFO) collection of objects. You "push" items
onto the stack and "pop" them off.
Example:
vb.net
stack.Push("First")
stack.Push("Second")
d. Queue:
vb.net
queue.Enqueue("First")
queue.Enqueue("Second")
a. List(Of T):
Definition: A strongly-typed dynamic array. It's similar to ArrayList but ensures type
safety since all items are of a specific type (T).
Example:
vb.net
list.Add(1)
list.Add(2)
Example:
vb.net
dict("Apple") = 5
dict("Banana") = 10
Definition: Generic versions of the Stack and Queue classes which are strongly typed.
Example:
vb.net
stack.Push("Hello")
queue.Enqueue("Hello")
Procedures: Subroutines and Functions, Passing Arguments
In VB.NET, procedures are blocks of code that perform specific tasks or operations.
There are two main types of procedures: subroutines and functions. Subroutines do not
return a value, while functions return a value. When you want to pass data to a
procedure or function, you can do so using arguments.
Here's a breakdown of how to define and use subroutines and functions, as well as how
to pass arguments in VB.NET:
Subroutines:
vb.net
End Sub
vb.net
Dim x As Integer = 5
Dim y As String = "Hello"
MySubroutine(x, y)
Functions:
vb.net
Return result
End Function
vb.net
Dim a As Integer = 10
Dim b As Integer = 20
Passing Arguments:
● When passing arguments to a procedure or function, you specify the argument
names and their data types.
● You can use the ByVal keyword to pass arguments by value (a copy of the data is
passed), or the ByRef keyword to pass arguments by reference (the actual data is
passed).
● ByVal is the default and is used when you don't specify ByVal or ByRef.
vb.net
num = num + 1
End Sub
Dim x As Integer = 5
ChangeValue(x)
vb.net
num = num + 1
End Sub
Dim x As Integer = 5
ChangeValue(x)
To define an optional argument in VB.NET, you use the Optional keyword when declaring
a parameter. You can also provide a default value for the parameter.
vb.net
End Function
When calling this function, you can provide a value for quantity if you want to override
the default:
vb.net
Structures:
Structures in VB.NET are used to define custom data types that group related fields
together. They are similar to classes but have some differences. Structures are value
types, which means they are copied when assigned to a new variable or passed as
function arguments. They are often used for lightweight objects that don't require
inheritance or complex behavior.
vb.net
Structure Point
Public X As Integer
Public Y As Integer
End Structure