If Then Else Statement in VB.NET used to check the condition true or false. This logical operator is used to compare the data using comparison operator. If the condition is true then true statement will execute, if the condition is false then the false statement will execute. In this program user can check conditions by entering number 1 and number 2. The button will check the condition each and every time whenever user clicks submit button.
Syntax:
This is the syntax of if then else statement in vb.net:
If(condition)Then if block - true statement will execute Else else block - false statement will execute End If
In the syntax, If the condition is true true block will execute and if the condition is false else block will execute.
Source Code and screen shot:
In the screenshot number 1 entered 100 and number 2 entered 50. After checking the condition it checks and displays “Number 1 is greater than number 2”.
This is the source code. In the btnchkcondition button click event if else condition created. If the condition true then it will execute the true statement. If the condition is false then it will execute the false statement. True statement is between if and else block and false statement is between else and end if block.
Public Class frmifcondition Private Sub btnchkcondition_Click(sender As Object, e As EventArgs) Handles btnchkcondition.Click If Val(txtnum1.Text) > Val(txtnum2.Text) Then MessageBox.Show("Number 1 is greater than number 2") Else MessageBox.Show("Number 2 is greater than number 1..") End If End Sub End Class
Download Source Code: This source code works well in visual studio 2012.
Video Tutorial: This video tutorial explains how to write this if then else statement and it shows how the code is compiled, executed and its result.
Thanks for reading this article. If you have any queries kindly post in the comment box.