if else statements in ASP.NET

The IF-ELSE conditional statement is utilized to evaluate conditions specified in the header of the if statement, thereby making decisions based on the outcome of those conditions. This statement enables developers to execute different blocks of code depending on whether a particular condition is true or false.

IF statement

When utilizing the IF-ELSE statement, conditions are assessed by employing comparison operators to compare values and logical operators to combine multiple conditions. Comparison operators, such as equality (==), inequality (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=), are employed to compare data values. Logical operators, such as AND (&&), OR (), and NOT (!), are utilized to combine and manipulate multiple conditions.

VB.Net
If [Condition] Then Statement Else Statement End If
C#
if (condition) statement; else statement;

When an IF statement is encountered, the provided condition is evaluated. If the condition evaluates to true, the program proceeds to execute the code within the body of the IF block. This allows for specific actions or instructions to be executed when the condition is met.

However, if the condition evaluates to false, the program follows an alternative path. If an ELSE block is present, the code within the ELSE block is executed instead. The ELSE block serves as a fallback or alternative execution path when the condition in the IF statement is not satisfied. On the other hand, if there is no ELSE block, the control simply moves to the next line of code after the IF statement, bypassing the execution of any specific code block associated with the ELSE condition.

ELSE block

The ELSE statement in the IF statement is not mandatory, allowing developers to utilize the IF statement independently without an associated ELSE statement if desired.

VB.Net
If [Condition] Then Statement End If
C#
if (condition) statement;

ELSE-IF statement

To evaluate multiple conditions, the ELSE-IF statement can be employed. This statement serves as an extension of the IF-ELSE construct, allowing developers to check additional conditions beyond the initial IF statement. By utilizing ELSE-IF, developers can specify alternative conditions and corresponding code blocks to be executed if those conditions are met. This construct provides a more comprehensive and flexible approach to conditionally executing code based on various scenarios.

VB.Net
If [Condition] Then Statement ElseIf [Condition] Then Statement Else Statement End If
C#
if (condition) statement; else if (condition) statement; else statement;

The following ASP.NET program declare and initialize values to two numbers , num1 and num2 and then check which is greater using if statement.

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Full Source | C#
using System; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { int num1, num2; num1 = 100; num2 = 200; if (num1 > num2) { Label1.Text = "Num1 is greater than num2"; } else { Label1.Text = "Num2 is greater than num1"; } } }
Full Source | VB.NET
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim num1, num2 As Integer num1 = 100 num2 = 100 If num1 > num2 Then Label1.Text = "Num1 is greater than num2 " Else Label1.Text = "Num2 is greater than num1 " End If End Sub End Class

Conclusion

The IF statement enables the program to execute different blocks of code based on the evaluation of a condition. If the condition is true, the code within the IF block is executed. If the condition is false, and an ELSE block is present, the code within the ELSE block is executed. If no ELSE block exists, the control proceeds to the next line of code after the IF statement.