
How to use IF Loop in C#


How to use IF Loop in C#
- IF Loop in C# takes a Boolean expression as a condition and if this condition is TRUE, the compiler executes the code inside IF loop { } and if the condition is wrong, the compiler simply ignores the code.
- Here's the syntax of IF Loop in C#:
if (boolean-expression) { // Code will come here, execute if the Condition is TRUE. }
- Let's create a simple program, where we ask the user to enter his Physics marks and the program will tell the user whether he is passed or failed.
- I have used If loop and the condition I have used is, if the marks are less than 50 then student has failed & if its equal or greater than 50 then the user has passed.
- Here's the code for IF loop in C#, shown in below figure:

- You can see in above figure that I have entered 25, now when the compiler will come to first IF loop, it will check the condition (Marks >= 50), which is not true as the number we entered is less than 50, so the compiler will simply ignore this IF loop and will move on to second IF Loop.
- The condition in second IF Loop is (Marks < 50), and clearly this condition is TRUE, so our compiler will enter in this IF Loop, enclosed by curly brackets { }, and will print out "You failed the exam.".
- So, in simple words:
- IF condition is TRUE => Execute.
- IF condition is FALSE => Ignore.
- Few commonly used conditional operators of IF Loops are:
- Equal to ( == )
- Not Equal to ( != )
- Greater than ( > )
- Less than ( < )
- Greater than or Equal to ( >= )
- Less than or Equal to ( <= )
- We could also use multiple conditions in single IF Loop using these two operators:
- && ( AND ) => it returns TRUE, if both conditions are TRUE.
- || ( OR ) => it returns TRUE, if either of two conditions is TRUE.
- Let's edit out code a little and add a 3rd condition, as shown in below figure:

- Here, I have used three IF Loops in above code and have used && operator in second IF Loop.
- So, if the number is in between 50 & 90, then second loop will execute, both conditions have to be TRUE.
- I hope you have understood How to use IF Loop in C#, now let's move a little forward and have a look at IF Else Loop in C#, which is kind of an extension of IF Loop.
IF-Else Loop in C#
- Instead of using separate IF Loops for different conditions, we can use single IF-Else Loop.
- In our first code, we have used two IF Loops to check whether student has passed or failed.
- Let's rewrite that code using IF-Else Loop, as shown in below figure:

- As you can see in above figure, first I have used IF Loop and placed a condition that if (Marks >= 50) then print "you passed the exam" otherwise simply print "you failed the exam".
- Else condition is like the default option, if "IF" Loop failed to execute then "ELSE" will be executed and if "IF" loop is successful, then "ELSE" will be ignored.
- That was the case for two IF Loops, but what if we have more than two IF Loops, as in our second code, we have 3 IF Loops, let's rewrite that code using IF-Else Loop:

- The above code is working exactly the same as our code with 3 IF Loops but here we are using IF-Else Loop.
- In IF-Else Loop, the first loop will be "if" loop and the last one will be "else" loop while all the intermediary loops will be "else if" loops.
- Here's the final code for today's lecture:
using System; namespace TEPProject { class Program { static void Main(string[] args) { Console.Write("Enter your Physics marks: "); int Marks = int.Parse(Console.ReadLine()); if (Marks >= 90) { Console.WriteLine("You got A+ grade. \n\n"); } else if (Marks >= 50 && Marks < 90) { Console.WriteLine("You passed the exam. \n\n"); } else { Console.WriteLine("You failed the exam. \n\n"); } } } }So, that was all for today. I hope now you can easily use IF-Else Loop in C#, if you have any problems, ask in comments. In our next tutorial, we will have a look at How to use Switch statement in C#. Till then take care & have fun !!! :)
×
![]()