VB.NET Control Statements
In VB.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.
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.
VB.NET provides the following conditional or decision-making statements.
- If-Then Statement
- If-Then Else Statement
- If-Then ElseIf Statement
- Select Case Statement
- Nested Select Case Statements
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:
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.
Example 1: Write a simple program to print a statement in VB.NET.
Module1.vb
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
As we can see in the above example, if the value of str is equal to tutoraspire, 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
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:
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
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
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
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
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
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
Select Case Statement
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.
Using the select case statement in VB.NET programming, you can replace the uses of multiple If-Then-Else If statement from the program for better readability and easy to use.
Syntax
Following is the syntax of the Select Case statement in VB.NET, as follows:
Furthermore, you can also set more than one condition in a single case statement, such as:
Flowchart of Select Case Statement
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.
Example 1: Write a program to display the Days name using the select case statement in VB.NET.
Select_case.vb
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
In the select case statement, the value of Days “Thurs” will compare all the available select cases‘ values in a program. If a value matched with any condition, it prints the particular statement, and if the value is not matched with any select case statement, it prints the default message.
Example 2: Write a program to perform an arithmetic operation using the Select case statement in VB.NET.
Operation.vb
Operation.vb
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
In the above example, we defined Select with multiple case statements, and if the user-defined input is matched with any defined case statement, it executes that statement. And if the condition is not matched with any case, it executes a default statement in VB.NET.
Here, we provide ‘M’ as input, which checks all case statements, and if any case is matched with M, it executes the statement within the respective Case statement.
VB.NET Nested Select Case statements
When a Select Case statement is written inside the body of another Select Case statement is called a nested Select Case statement.
Syntax:
Example 1: Write a program to use a nested select case statement in VB.NET.
Module1.vb
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 nested select case statement in VB.NET.
nested_selectcase.vb
Now compile and execute the above program by clicking on the Start or F5 button, it shows the following output:
In the above example, we have only defined the first three numbers 1-3 and if the number matches to any case statement, the select statement is executed. Here, we have entered 2 that is matched with case 2 and it executes a block as shown above. And this block executes the statement “Only the First two letters such as C and D”. Therefore, we enter a letter D, letter D is matched with the nested select case statement, and if a match is found, it executes the select case statement as shown above.