TEP , The Engineering Projects , Author Welcome! Syed Zain Nasir Mechatronics Engineer TEP , The ENgineering Projects , TEP Badge TEP , The Engineering Projects , Ratting Background 7.5 7.5/10
TEP , The Engineering Projects , Rating Percentage Background Reputation Score 55 / 100
How to use IF Loop in C#, IF Loop in C#, if loop c#, c# if loop, if c#, C# if, c sharp if loop, if loop in c sharp
TEP , The Engineering Projects , Boxes

How to use IF Loop in C#

TEP , THe Engineering Projects , Shares 2.5K Views
TEP , The ENgineering Projects , Reaction 40
TEP , The ENgineering Projects , Reaction 700
TEP , The ENgineering Projects , Reaction 60
TEP , The ENgineering Projects , Reaction 25
TEP , The ENgineering Projects , Reaction 60
TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest
Shares: 691
TEP , The Engineering Projects , PCBWAY
How to use IF Loop in C#, IF Loop in C#, if loop c#, c# if loop, if c#, C# if, c sharp if loop, if loop in c sharp
Hello everyone, I hope you all are doing great. Today, we are gonna have a look at IF Loops in C# and it's 8th tutorial in C# series. So far, we have discussed some basic concepts in C# and now we are ready to get our hands on Loops in C#. C#, as any other programming language, supports a lot of loop structures, we will discuss each one of them separately in our coming tutorials. Today, we will discuss the simplest of them all, which is IF loop, so let's get started with 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:
How to use IF Loop in C#, IF Loop in C#, if loop c#, c# if loop, if c#, C# if, c sharp if loop, if loop in c sharp
  • 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:
How to use IF Loop in C#, IF Loop in C#, if loop c#, c# if loop, if c#, C# if, c sharp if loop, if loop in c sharp
  • 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:
How to use IF Loop in C#, IF Loop in C#, if loop c#, c# if loop, if c#, C# if, c sharp if loop, if loop in c sharp
  • 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:
How to use IF Loop in C#, IF Loop in C#, if loop c#, c# if loop, if c#, C# if, c sharp if loop, if loop in c sharp
  • 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 !!! :)
TEP , The Engineering Projects , Tags
How to use IF Loop in C#
IF Loop in C#
if loop c#
c# if loop
if c#
C# if
c sharp if loop
if loop in c sharp
TEP , The Engineering Projects , Comments

Write a Comment

TEP , The Engineering Projects , WordPress TEP , The Engineering Projects , Google TEP , The Engineering Projects , Twitter TEP , The Engineering Projects , Facebook TEP , The Engineering Projects , User
TEP , The Engineering Projects , Robot
TEP , The Engineering Projects , Comments Comments on ‘’ How to use IF Loop in C# ‘’ (0)
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Top PCB Design Service

PCBA
TEP , The Engineering Projects , JLCPCB
PCB
TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Embedded Tools

ARDINO TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Subscribe Now !!!

Learn Free Pro Tricks

TEP , The Engineering Projects , Mail Receive Quality Tutorials Straight in your Inbox by Submitting your Email ID Below
TEP , The Engineering Projects , Mail TEP , The Engineering Projects , Mail Shadow
TEP , The Engineering Projects , Mail Robot TEP , The Engineering Projects , Mail Robot Shadow TEP , The Engineering Projects , Mail Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Engineering Books

TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (3.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (4.7)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , TEP Robot TEP , The Engineering Projects , TEP Robot Shadow TEP , The Engineering Projects , TEP Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Categories

TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K