Visual Basic .NET - If Condition
In VB.NET, an "If" statement is used to check a specific condition and execute different blocks of code depending on whether the condition is true or false.
The basic syntax of an "If" statement is:
If condition Then
' Block of code to be executed if the condition is true
Else
' Block of code to be executed if the condition is false
End If
Here, "condition" represents the expression to be evaluated. If the condition evaluates to True, the code block following the "Then" statement is executed. If the condition evaluates to False, the code block following the "Else" statement is executed.
For example, consider the following VB.NET code snippet that uses an If statement to check if a number is even or odd:
Dim num As Integer = 10
If num Mod 2 = 0 Then
Console.WriteLine("The number is even")
Else
Console.WriteLine("The number is odd")
End If
In this example, the "If" statement checks if the number "num" is divisible by 2 without leaving a remainder (which means it's even). If the condition is true, the first code block is executed and the message "The number is even" is displayed on the console. If the condition is false, the second code block is executed and the message "The number is odd" is displayed.
In VB.NET, the If statement can be extended to include multiple conditions by using the ElseIf keyword. This is called an If...ElseIf...Else ladder or chain.
Here's the syntax for an If...ElseIf...Else statement:
If condition1 Then
' code to execute if condition1 is true
ElseIf condition2 Then
' code to execute if condition2 is true and condition1 is false
ElseIf condition3 Then
' code to execute if condition3 is true and conditions 1 and 2 are false
Else
' code to execute if all conditions are false
End If
Here's an example that uses an If...ElseIf...Else ladder to determine the grade for a student based on their exam score:
Dim score As Integer = 75
Dim grade As String
If score >= 90 Then
grade = "A"
ElseIf score >= 80 Then
grade = "B"
ElseIf score >= 70 Then
grade = "C"
ElseIf score >= 60 Then
grade = "D"
Else
grade = "F"
End If
Console.WriteLine("Score: " & score)
Console.WriteLine("Grade: " & grade)
In this example, the If...ElseIf...Else ladder checks the value of the score variable and assigns a grade based on the conditions specified. The Console.WriteLine statements display the score and grade to the console.
Example : Checking if a number is positive or negative
Dim number As Integer = -5
If number > 0 Then
Console.WriteLine("The number is positive.")
ElseIf number < 0 Then
Console.WriteLine("The number is negative.")
Else
Console.WriteLine("The number is zero.")
End If
In this example, we first declare a variable number and set it equal to -5. We then use an if statement to check whether number is greater than 0. If it is, we print out "The number is positive." If it's not, we move on to the next condition, which checks whether number is less than 0. If it is, we print out "The number is negative." If neither condition is true (i.e. number is equal to 0), we print out "The number is zero."
Example : Checking if a number is even or odd
Dim number As Integer = 7
If number Mod 2 = 0 Then
Console.WriteLine("The number is even.")
Else
Console.WriteLine("The number is odd.")
End If
In this example, we declare a variable number and set it equal to 7. We then use an if statement to check whether number is even or odd. We do this by checking whether number Mod 2 (i.e. the remainder when number is divided by 2) is equal to 0. If it is, we print out "The number is even." If it's not, we print out "The number is odd."
Example : Checking if a password is correct
Dim password As String = "password123"
If password = "password123" Then
Console.WriteLine("Welcome!")
Else
Console.WriteLine("Incorrect password.")
End If
In this example, we declare a variable password and set it equal to "password123". We then use an if statement to check whether password is correct. If it's equal to "password123", we print out "Welcome!" If it's not, we print out "Incorrect password."