c# if, c# if else, c# if statement, c# is else statementHello friends, I hope you all are fine and having fun with your lives. Today, I am going to show you How to use C# if else statement. Previously we have seen the Introduction of C# Windows Forms an then we have also discussed different data types and variables used for programming in C#, you can read these tutorials on C# Tutorial page.

So, before starting this C# if else tutorial, I would suggest you to first read all of those basic C# tutorials so that you know how to use different data types. Before starting this tutorial, you should also read How to add C# Control in Windows Form because we are gonna use three C# controls in this tutorial.So, let’s get started with How to use C# if else statement:

How to use C# if else Statement ???

  • First of all, let’s have an overview of C# if else statement.
  • If Else Statements are named as conditional logic.
  • C# If statements are essential for any kind of project , IF statement is the most basic conditional logic of C# and is used in almost every project.
  • It acts as if and then you have to specify some statement which is a check statement. In simple words it acts as if this condition is true then do this task. 🙂
  • Let’s have a look at its pseudo code:

IF ( Condition is TRUE )  ====>  Then do this TASK.     

  • Now let me give you an example suppose you are designing some calculator then it must have some buttons on it like Multiply.
  • And you want to multiply any two integers when you click this MULTIPLY button.
  • Now in your code you need to add something like:

IF ( MULTIPLY button is pressed ) ====> Then multiply the two Integer variables

  • So, you can see in the above C# IF statement that clicking of MULTIPLY button is the condition and the multiplication of two integers is the TASK.
  • This C# IF Statement is making the relation between the Condition and the Task.
  • So, now let’s have a look at the real C# IF Statement syntax:
C# IF Syntax
  • In C# IF Statements, first of all we place the word “IF”.
  • After that, we place the condition in small brackets ( ), the condition must be a comparison which should be either true or false. We will shortly have a look at some example.
  • Now after specifying the condition, next thing we need to do is to specify the task, which comes in curly brackets { }.
  • So, the overall if statement looks something as shown below:

If ( CONDITION comes here ) {

TASKS are specified here.


  • Now, I hope that you have got the complete understanding of this C# IF Statement so let’s design a simple example so that I can show you How to use this IF Statement.
  • Now let’s design a simple C# Windows Form application and add some Control in it.
  • I have added one Button, one TextBox and one Label and it looks like as shown in below figure:

c# if, c# if else, c# if statement, c# is else statement

  • Now I have created a small IF condition that if TEP is written in the Text box then the Label Text will be changed to My Blog.
  • Its just a random logic but quite simple so I thought that it will better explain the logic.
  • So, when you click this button then if TEP is written in the Text Box then we will get My Blog as shown in below figure:

c# if, c# if else, c# if statement, c# is else statement

  • You can see in the above figure that when I click the button then it checked the textbox and because TEP is written there that’s why Label text is changed to My Blog.
  • Here’s the code for the button click function which is quite simple:

  • So, that’s our IF logic as I discussed earlier the condition is in ( ) small brackets and then in curly brackets { } we have our task.
  • But there’s a little problem in above condition that I have mentioned to display My blog on TEP but I haven’t mentioned what to type when there’s no TEP in Text Box.
  • So, what should happen when there’s something else in Text box.
  • That’s where we encountered with ELSE Statement.
  • In ELSE we specify that TASK which will occur when IF condition is FALSE.
  • So ,let’s change our code a little:

  • Now you can see in the above code that I have added the ELSE statement, in which I have mentioned that Not my Blog.
  • So, now when the IF condition is TRUE then Label text will be My Blog and when its FALSE then ELSE will work and we will get NOT My Blog.
  • Check the below image:

c# if, c# if else, c# if statement, c# is else statement

  • So, that’s how C# if else statement works.

That’s all for today. I hope you have enjoyed today’s tutorial. I will cover more conditional logic in the coming tutorials. Till then Take care and have fun !!! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment