Control Statement
Control Statement
The above diagram shows that if the defined condition is true, statement_1 will be executed, and if the condition is false,
statement_2 will be executed.
PlayNext
Unmute
Current Time 0:00
/
Duration 18:10
Loaded: 0.37%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
As we can see in the above example, if the value of str is equal to JavaTpoint, the condition is true, and it prints the Statement.
Example 2: Write a program to print a number is greater than another number in VB.NET.
if_statment2.vb
Module if_statement2
Sub Main()
?Definition of variables
Dim no1, no2 As Integer
Console.WriteLine("Enter any two number:")
no1 = Console.ReadLine() ?read no1 from user
no2 = Console.ReadLine() ?read no2 from user
If no1 > no2 Then
Console.WriteLine("First number is greater than second number")
End If
If no1 < no2 Then
Console.WriteLine("Second number is greater than First number")
End If
Console.WriteLine("press any key to exit...")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
In the above program, we enter two numbers to find the greater number using the relational operator. And if the first number is
greater than the other, the first statement is executed; otherwise, the second statement will be executed.
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
'This statement will execute if the Boolean condition is true
Else
'Optional statement will execute if the Boolean condition is false
End If
Flow chart
The above diagram represents that if the Boolean expression (condition) is true, the if statement will execute, and if the Boolean
expression is false, Else code or statement will be executed. After that, the control transfer to the next statement, which is
immediately after the If-Then-Else control statement.
Example 1: Write a program to check whether the number is even or odd.
If_Else_statment.vb
Module If_Else_statement
Sub Main()
Dim num As Integer
Console.WriteLine("Enter the Number")
num = Console.ReadLine() 'read data from console
If (num Mod 2 = 0) Then ' if condition is true, print the if statement
Console.WriteLine("It is an even number")
Else 'otherwise, Else statement is executed.
Console.WriteLine("It is an odd number")
End If
Console.WriteLine("press any key to exit...")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
Example 2: Write a program to print the larger and smaller of the two numbers.
if_else_statment2.vb
Module if_else_statement2
Sub Main()
Dim a As Integer
Dim b As Integer
Console.WriteLine("Enter the first number : ")
a = Console.ReadLine()
Console.WriteLine("Enter the second number : ")
b = Console.ReadLine()
If a > b Then
Console.WriteLine(" larger number = {0} and smaller number = {1} ", a, b)
Else
Console.WriteLine(" larger number = {0} and smaller number = {1} ", b, a)
End If
Console.WriteLine("press any key to exit...")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
VB.NET If-Then-ElseIf statement
The If-Then-ElseIf Statement provides a choice to execute only one condition or statement from multiple statements. Execution
starts from the top to bottom, and it checked for each If condition. And if the condition is met, the block of If the statement is
executed. And if none of the conditions are true, the last block is executed. Following is the syntax of If-Then-ElseIf Statement
in VB.NET as follows:
Syntax
If(condition 1)Then
' Executes when condition 1 is true
ElseIf( condition 2)Then
' Executes when condition 2 is true
ElseIf( boolean_expression 3)Then
' Executes when the condition 3 is true
Else
' executes the default statement when none of the above conditions is true.
End If
Flowchart
The following diagram represents the functioning of the If-Else-If Statement in the VB.NET programming language.
If this condition is true in the flowchart of the if-else-if statement, the statement is executed within the if block. If the condition is
not true, it passes control to the next ElseIf condition to check whether the condition is matched. And if none of the conditions
are matched, the else block is executed.
Example 1: Write a program to show the uses of If... ElseIf statements.
if_elseIf.vb
Module if_elseIf
Sub Main()
Dim var1 As Integer
Console.WriteLine(" Input the value of var1: ")
var1 = Console.ReadLine()
If var1 = 20 Then
'if condition is true then print the following statement'
Console.WriteLine(" Entered value is equal to 20")
ElseIf var1 < 50 Then
Console.WriteLine(" Entered value is less than 50")
ElseIf var1 >= 100 Then
Console.WriteLine(" Entered value is greater than 100")
Else
'if none of the above condition is satisfied, print the following statement
Console.WriteLine(" Value is not matched with above condition")
End If
Console.WriteLine(" You have entered : {0}", var1)
Console.WriteLine(" press any key to exit...")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
Example 2: Write a program to use the If-Then-ElseIf Statement for calculating the division obtained by the student. Also, take
the marks obtained by the student in 5 different subjects from the keyboard.
if_elseIf2.vb
Module If_elseIf2
Sub Main() ' execution start from Main() method
Dim m1, m2, m3, m4, m5, per As Integer
Console.WriteLine("Enter marks in five subjects ")
' Read the marks of five subject
m1 = Console.ReadLine()
m2 = Console.ReadLine()
m3 = Console.ReadLine()
m4 = Console.ReadLine()
m5 = Console.ReadLine()
per = (m1 + m2 + m3 + m4 + m5) / 5
If (per >= 70) Then
'if condition is true, print the first division
Console.WriteLine(" First division")
ElseIf (per >= 60) Then
'if ElseIf condition is true, print the second division
Console.WriteLine(" Second division")
ElseIf (per >= 50) Then
'if ElseIf condition is true, print the third division
Console.WriteLine(" Third division")
ElseIf (per >= 40) Then
'if ElseIf condition is true, print only pass with grace
Console.WriteLine(" Only Pass with Grace")
Else
'if none of the condition is true, print the Failed
Console.WriteLine(" Failed")
End If
Console.WriteLine("press any key to exit...")
Console.ReadKey()
End Sub
End Module
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output: