Decision Statements in VB .Net
Definition
- A decision statement in VB .NET is used to control the flow of execution of a program statement by allowing different blocks of code to execute based on conditions or logical expressions.
Need for Decision Statements
- Decision statements are required to:
- Make programs dynamic and intelligent.
- Execute code based on user input or conditions.
- Perform branching in program flow.
- Decision statements enable branching.
Types of Decision Statements in VB .NET
- VB .NET provides the following decision-making statements:
- If Statement
- If…Else Statement
- If…ElseIf…Else Statement
- Nested If Statement
- Select Case Statement
If Statement
- The if statement executes a block of code only when a condition is true.
- Syntax
If condition Then
statement(s)
End If
- Example
Dim x As Integer = 10
If x > 5 Then
Console.WriteLine(“x is greater than 5”)
End If
If…Else Statement
- The If…Else statement executes one block if the condition is true and another block if it is false.
- Syntax
If condition Then
statement(s)
Else
statement(s)
End If
- Example
Dim num As Integer = 5
If num Mod 2 = 0 Then
Console.WriteLine(“Even number”)
Else
Console.WriteLine(“Odd number”)
End If
If…ElseIf…Else Statement
- This statement is used when multiple conditions need to be checked.
- Syntax
If condition1 Then
statement(s)
ElseIf condition2 Then
statement(s)
Else
statement(s)
End If
- Example
Dim marks As Integer = 75
If marks >= 80 Then
Console.WriteLine(“Distinction”)
ElseIf marks >= 60 Then
Console.WriteLine(“First Class”)
Else
Console.WriteLine(“Pass”)
End If
Nested If Statement
- A Nested If is an if statement written inside another if statement.
- Example
Dim age As Integer = 20
If age >= 18 Then
If age <= 60 Then
Console.WriteLine(“Eligible to work”)
End If
End If
Select Case Statement
- The Select Case statement is an alternative to multiple If…ElseIf statements and improves readability and efficiency.
- Select Case is best and preferred for multiple fixed conditions or values.
- It is more readable and cleaner.
- It is faster for many comparisons.
- Syntax
Select Case expression
Case value1
statement(s)
Case value2
statement(s)
Case Else
statement(s)
End Select
- Example
Dim day As Integer = 3
Select Case day
Case 1
Console.WriteLine(“Monday”)
Case 2
Console.WriteLine(“Tuesday”)
Case 3
Console.WriteLine(“Wednesday”)
Case Else
Console.WriteLine(“Invalid day”)
End Select
Looping Statements in VB .Net
Definition
- A looping statement in VB .NET is used to execute a block of statements repeatedly as long as a specified condition is true or for a fixed number of times.
Need for Looping Statements
- Looping statements are used to:
- Reduce code repetition.
- Perform repetitive tasks efficiently.
- Process arrays, collections, and records.
- Improve program readability and maintainability.
Types of Looping Statements in VB .NET
- VB .NET provides the following looping statements:
- For…Next Loop
- For Each…Next Loop
- While…End While Loop
- Do While Loop
- Do Until Loop
- Nested Loops
For…Next Loop
- The For…Next loop is used when the number of iterations is known in advance.
- For loops are used when iterations are known.
- Syntax
For counter = start To end [Step stepvalue]
statement(s)
Next
- Example
For i As Integer = 1 To 5
Console.WriteLine(i)
Next
For Each…Next Loop
- The For Each…Next loop is used to iterate through each element of a collection or array.
- This loop is best for collections.
- Syntax
For Each element In collection
statement(s)
Next
- Example
Dim names() As String = {“Amit”, “Ravi”, “Suman”}
For Each n As String In names
Console.WriteLine(n)
Next
While…End While Loop
- The While loop checks the condition before executing the loop body.
- While loops are used for condition-based repetition.
- Syntax
While condition
statement(s)
End While
- Example
Dim i As Integer = 1
While i <= 5
Console.WriteLine(i)
i += 1
End While
Do While Loop
- The Do While loop executes the loop body at least once, because the condition is checked after execution.
- Do loops are used for condition-based repetition.
- Syntax
Do
statement(s)
Loop While condition
- Example
Dim i As Integer = 1
Do
Console.WriteLine(i)
i += 1
Loop While i <= 5
Do Until Loop
- The Do Until loop continues execution until the condition becomes true.
- Syntax
Do
statement(s)
Loop Until condition
- Example
Dim i As Integer = 1
Do
Console.WriteLine(i)
i += 1
Loop Until i > 5
Nested Loops
- A nested loop is a loop inside another loop.
- Example
For i As Integer = 1 To 3
For j As Integer = 1 To 2
Console.WriteLine(“i=” & i & ” j=” & j)
Next
Next
Loop Control/Jumping Statements
Exit Statement
- Used to terminate a loop immediately.
If i = 3 Then
Exit For
End If
Continue Statement
- Used to skip the current iteration.
If i = 3 Then
Continue For
End If
![]()
0 Comments