Control Statement
Control Statement
Control Statement
We can define more than one condition to be evaluated by the program with statements. If the
defined condition is true, the statement or block executes according to the condition, and if the
condition is false, another statement is executed.
The following figure shows a common format of the decision control statements to validate and
execute a 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.
o If-Then Statement
o If-Then Else Statement
o If-Then ElseIf Statement
o Select Case 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
[Statement or block of Statement]
End If
In If-Then Statement, the condition can be a Boolean, logical, or relational condition, and the statement
can be single or group of statements that will be executed when the condition is true.
Module Module1
Sub Main()
Dim num As Integer = 10
If num > 0 Then
Console.WriteLine("Number is positive.")
End If
End Sub
End Module
if_statment2.vb
1. Module if_statement2
2. Sub Main()
3. ?Definition of variables
4. Dim no1, no2 As Integer
5. Console.WriteLine("Enter any two number:")
6. no1 = Console.ReadLine() ?read no1 from user
7. no2 = Console.ReadLine() ?read no2 from user
8. If no1 > no2 Then
9. Console.WriteLine("First number is greater than second number")
10. End If
11. If no1 < no2 Then
12. Console.WriteLine("Second number is greater than First number")
13. End If
14. Console.WriteLine("press any key to exit...")
15. Console.ReadKey()
16. End Sub
17. End Module
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
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 positive or not
Module Module1
Sub Main()
Dim num As Integer = 10
If num > 0 Then
Console.WriteLine("Number is positive.")
Else
Console.WriteLine("Number is not positive.")
End If
End Sub
End Module
Module Module2
Sub Main()
Dim temperature As Integer = 25
If_Else_statment.vb
1. Module If_Else_statement
2. Sub Main()
3. Dim num As Integer
4. Console.WriteLine("Enter the Number")
5. num = Console.ReadLine() 'read data from console
6.
7. If (num Mod 2 = 0) Then ' if condition is true, print the if statement
8. Console.WriteLine("It is an even number")
9.
10. Else 'otherwise, Else statement is executed.
11. Console.WriteLine("It is an odd number")
12. End If
13.
14. Console.WriteLine("press any key to exit...")
15. Console.ReadKey()
16. End Sub
17. End Module
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
1. If(condition 1)Then
2. ' Executes when condition 1 is true
3. ElseIf( condition 2)Then
4. ' Executes when condition 2 is true
5. ElseIf( boolean_expression 3)Then
6. ' Executes when the condition 3 is true
7. Else
8. ' executes the default statement when none of the above conditions is true.
9. 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 check whether the number is positive, negative or zero
Module Module1
Sub Main()
Dim num As Integer = 10
If num > 0 Then
Console.WriteLine("Number is positive.")
ElseIf num < 0 Then
Console.WriteLine("Number is negative.")
Else
Console.WriteLine("Number is zero.")
End IfEnd Sub
End Module
Syntax
Case value2 'defines the item or value that you want to match.
// Define a statement to execute
Case Else
// Define the default statement if none of the conditions is true.
End Select
The following flowchart represents the functioning of the Select case statement in the VB.NET
programming language.
In Flowchart, the Select Case statement represents the evaluating of the process start from top to bottom. If
the expression or value is matched with the first select case, statement -1 is executed else the control transfer
to the next case for checking whether the expression is matching or not. Similarly, it checks all Select case
statements for evaluating. If none of the cases are matched, the Else block statement will be executed, and
finally, the Select Case Statement will come to an end.
Example1:
Dim number As Integer
number = 3
Case 3
Console.WriteLine("The number is 3")
Case Else
Console.WriteLine("The number is not 1, 2, or 3")
End Select
Example 2: Write a program to display the Days name using the select case statement
Select_case.vb
1. Imports System
2. Module Select_case
3. Sub Main()
4. 'define a local variable.
5. Dim Days As String
6. Days = "Thurs"
7. Select Case Days
8. Case "Mon"
9. Console.WriteLine(" Today is Monday")
10. Case "Tue"
11. Console.WriteLine(" Today is Tuesday")
12. Case "Wed"
13. Console.WriteLine("Today is Wednesday")
14. Case "Thurs"
15. Console.WriteLine("Today is Thursday")
16. Case "Fri"
17. Console.WriteLine("Today is Friday")
18. Case "Sat"
19. Console.WriteLine("Today is Saturday")
20. Case "Sun"
21. Console.WriteLine("Today is Sunday")
22. Case Else
23. Console.WriteLine(" You have typed Something wrong")
24.
25. End Select
26. Console.WriteLine("You have selected : {0}", Days)
27. Console.WriteLine("Press any key to exit...")
28. Console.ReadLine()
29. End Sub
30. End Module