Part-6 if-else-Statement

  1. if else Statement
  2. Nested if else condition
  3. if else (ternary operator)
 1. if else Statement:if statement identifies which statement to run based on the condition.  

 Example- Use if statement applied inside the condition to be executed, then condition is satisfied , statement runsIf condition is not satisfied , then Use else to specify a block of code condition  to be executed.
    Syntax  :
// if-else statement if (condition) { then-statement; } else { else-statement; }
  • both if-else statement can't be run at the same time. first check if condition is satisfied then-statement runs or not satisfied else-statement runs , control is transferred to the next statement.
  • Note: if is in Uppercase letters(IF or If) it will generate an error.

Example 

In this example we use two variables, x and y, to test whether x is greater than y (using the > operator). As x is 20, and y is 18, and we know that 24 is greater than 18, we print to the screen that "x is less than y".

                   using System;

                  class Fullstack

                   {
                     //Main method
                     public static void Main(string[] args)
                     {   
                        int x = 20;
                          if (x < 18)
                          {
                             Console.WriteLine("x is greater than y");
                          }
                       else
                        {
                             Console.WriteLine("x is less than y");
                        }
                              Console.ReadLine();
                  }
            }

2. Nested if-else Statement:

if else statement to specify a new condition can exist within another if...else statement. Such statements are called nested if...else statement.

Simple Word
Use the if statement to specify a new condition if the first condition is not Satisfy then else if this condition is also not satisfy then next else if condition so on.

 Syntax  :
        
          // if-else statement
           if (condition1)
           {
               then-statement;
           }
           else if (condition 2)
           {
               else-statement;
           }
            .
            .
            else
            {
                //block of code condtion 1 and condition 2 not satisfy then else Condition
            }
Example 

class Program

    {
         public static void Main(string[] args)
        {
            Console.WriteLine("Enter a character: ");
            char c = (char)Console.Read();
            if (Char.IsLetter(c))
            {
                if (Char.IsLower(c))
                {
                    Console.WriteLine("The character is lowercase.");
                }
                else
                {
                   Console.WriteLine("The character is uppercase.");
                }
                
            }
            else
            {
                Console.WriteLine("The character isn't an alphabetic character.");
                Console.ReadLine();
            }
            Console.ReadLine();
        }

    }

3 if-else (Ternary Operation)

There is a short-hand if else statement, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line.its easy to write code and simple view and more readable. It is often used to replace if else statements:

syntax :     data = (condition) ? expressionTrue : expressionFalse;

int x = 20, y = 10;

var result = x > y ? "x is greater than y" : "x is less than or equal to y";



Console.WriteLine(result);