C# if Conditional Statement
Conditional statement in C# used to take decision on based the conditions is true or false.
Simple if statement
Simple if statement is used when we have only one condition and the code will be executed only and only if the condition is true. There is not code for execution the if condition is not true.
C# – simple if condition
if (condition)
{
// condition is true then code to be executed
}
In above if condition we have some code for execution but it will only execute the condition is true. If the condition is not true there will be nothing to execution.
Adding if statement in C#.Net
Open visual studio and create new website with c#. Open a web form code part and right click anywhere between brackets of the page load portion. On the right click menu select Insert Snippet and click on it as shows in below figure.
After click on Insert Snippet option from menu, you will see a list of items, Now double click on Visual C#.
After double click on Visual C# option from items, you have list of snippets, Now double click on if.
After double click on if option from list of snippets, you will have code of if statement code on your web page.
let’s take C# example to understood more about simple if statement.
simple if statement example
int a=10;
int b=7;
if(a>b)
{
Response.Write ("a value is greater than b");
}
The output is:
a value is greater than b
in above c# example we have two integer variables a and b with values 10 and 7 respectively.
Here, if condition is true so code will be executed and result will be “a value is greater than b”.
if – else statement in C#
In if-else statement execute some code the if condition is true or execute another code the if condition is false.
Here we can write two answer for one condition if the condition is true or false.
C# if-else statement
if(condition)
{
// condition is true
}
else
{
// condition is false
}
Example of if-else statement in C#
string uname = "meera";
string upass = "ram";
if (uname == "meera" && upass == "ram")
{
Response.Write("welcome to system");
}
else
{
Response.Write("Login Error, Please try again");
}
The output is:
welcome to system
In above c# example we have two string variable uname and upass. Here, if condition we check the uname and upass value is right or wrong. The if condition is true then print “welcome to system” message and if false then print “Login Error, Please try again” message.
if-else example
int a=10;
int b=17;
if(a>b)
{
Response.Write("a is greater than b");
}
else
{
Response.Write("a is less than b");
}
The output is:
a is less than b
In above example value a is 10 and b is 17, so if condition is false so execute else code and print “a is less than b” message in output.
Related ASP.Net Topics :
Nested If Statement in C#.Net
Switch Statement in C#.Net
Subscribe us
If you liked this c#.net post, then please subscribe to our YouTube Channel for more c#.net video tutorials.
We hope that this asp.net c# tutorial helped you to understand simple If Statement.
Next asp.net tutorial we will learn about If-Else If-Else Statement.