Introduction:

In this article, we will learn how to use if, if...else, if...else if statement in C# to control the flow of our program’s execution with example.

Description:

In c#  if else statement is used for controlling the flow of program based on returning expression value. It evaluates the comparison operator and based on value executes the statements. For example, if you want to execute a  section of code, when the requirements meet then if else statement decide which section of code will be executed. Else is default condition and executes when no if condition matches. The following example will clear the concept of if else statement.

Example of if…else statement in c# 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1

    class Program
    {
        static void Main(string[] args)
        {
            int opt, value1, value2;
            float result;
           label:
            Console.WriteLine("\n\tMenu");
            Console.WriteLine("\nPress 1 for add");
            Console.WriteLine("Press 2 for subtraction");
            Console.WriteLine("Press 3 for multiplication");
            Console.WriteLine("Press 4 for Division");
 
            Console.Write("\n\nEnter first number:\t");
            value1 = Convert.ToInt32(Console.ReadLine());
 
            Console.Write("Enter second number:\t");
            value2 = Convert.ToInt32(Console.ReadLine());
 
            Console.Write("\nEnter your option:\t");
            opt = Convert.ToInt32(Console.ReadLine());
 
            if (opt == 1)
            {
                result = value1 + value2;
                Console.WriteLine("\n{0} + {1} = {2}", value1, value2, result);
            }
            else if (opt == 2)
            {
                result = value1 - value2;
                Console.WriteLine("\n{0} - {1} = {2}", value1, value2, result);
            }
            else if (opt == 3)
            {
                result = value1 * value2;
                Console.WriteLine("\n{0} x {1} = {2}", value1, value2, result);
            }
            else if (opt == 4)
            {
                result = (float)(value1 / value2);
                Console.WriteLine("\n{0} / {1} = {2}", value1, value2, result);
            }
            else
            {
                Console.WriteLine("Invalid option. Try again");
                goto label;
            }
            Console.ReadLine();
           
        }
    }
}

 

 Output:


How to use if else in c#


If you have more than one if statement then you can use else if statement for evaluating expression. The C# also supports nested if else statement. If you want to evaluate certain condition based on previous if, then you can use nested if-else statement in C#.

                         

I hope it will help to you after reading it.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like