c# if loop code example

Example 1: c# else if

string emotions;
emotions = Console.ReadLine();
//for emotions say the user input was either
//happy,sad and mad
if (emotions == "happy")
{
  Console.Write("You are happy");
}
else if (emotions == "sad")
{
    Console.Write("You are sad");
}
else if (emotions == "mad")
{
    Console.Write("You are mad");
}
else
{
    Console.Write("Unknown input");
}

Example 2: c# if else

using System;

namespace DecisionMaking 
{
   class Program 
   {
      static void Main(string[] args) 
      {
         /* local variable definition */
         int a = 100;
         int b = 20
         
         /* check the boolean condition */
         if (a < b) 
         {
            /* if condition is true then print the following */
            Console.WriteLine("a is less than " + b);
         } 
        else 
         {
            /* if condition is false then print the following */
            Console.WriteLine("a is not less than " + b);
         }
         Console.ReadLine();
      }
   }
}
                       =======OUTPUT========
output >> a is not less than 20

Example 3: c# if

int x;
int y;

if ( x == y)
	{
  		Console.WriteLine("x and y is equal");
	}
else
	{
  		Console.WriteLine("x and y is not equal");
	}

// Same, but shorter ( and harder XD )
Console.WriteLine($"x and y {(x == y? "is equal" : "is not equal")}");

Example 4: if statement c#

if (condition)
{
  print("Yes");
}
else
{
  print("No");
}

Example 5: different types of if statements in c#

//Default If condition
                if (comEnvList.Items.Count > 0)
                {
                    comEnvList.SelectedIndex = 1;
                }
                else
                {
                    comEnvList.SelectedIndex = 0;
                }
                //IIf statement
                comEnvList.SelectedIndex = comEnvList.Items.Count > 0 ? 1 : 0;