0% found this document useful (0 votes)
81 views7 pages

Control Statement

VB.NET provides several control statements like If-Then, If-Then-Else, and If-Then-ElseIf to control program execution based on conditions. The If-Then statement executes code if a condition is true, while If-Then-Else executes one code block if true and another if false. If-Then-ElseIf provides multiple conditions and executes the first true block. These allow programs to make decisions and write conditional logic.

Uploaded by

Margie Villoria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views7 pages

Control Statement

VB.NET provides several control statements like If-Then, If-Then-Else, and If-Then-ElseIf to control program execution based on conditions. The If-Then statement executes code if a condition is true, while If-Then-Else executes one code block if true and another if false. If-Then-ElseIf provides multiple conditions and executes the first true block. These allow programs to make decisions and write conditional logic.

Uploaded by

Margie Villoria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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.
PlayNext
Unmute
Current Time 0:00
/
Duration 18:10
Loaded: 0.37%
 
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s

VB.NET provides the following conditional or decision-making statements.


o If-Then Statement
o If-Then Else Statement
o If-Then ElseIf Statement
o Select Case Statement
o 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:
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.
Example 1: Write a simple program to print a statement in VB.NET.
Module1.vb
Module Module1  
    ' Declaration of variable str  
    Dim str As String = "JavaTpoint"  
    Sub Main()  
        ' if str equal to "JavaTpoint", below Statement will be executed.  
        If str = "JavaTpoint" Then  
            Console.WriteLine("Welcome to the JavaTpoint")  
        End If  
    Console.WritLine("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:

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:

You might also like