C#Html
Introduction to C#
Variables and Data Types in C#
Operators in C#
Control Statements in C# – if, else, switch, and loops Explained
Jump Statements in C# – break, continue, return Explained with Examples
Conditional Statements in C# if, else if, else, switch Explained with Examples
Loops in C# – For, While, and Do While with Examples

Conditional Statements in C# if, else if, else, switch Explained with Examples

Conditional statements in C# are used to perform different actions based on different conditions. These are essential to control the flow of a program.

Example

using System;

namespace ConditionalDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int marks = 85;

            if (marks >= 90)
            {
                Console.WriteLine("Grade: A+");
            }
            else if (marks >= 75)
            {
                Console.WriteLine("Grade: A");
            }
            else if (marks >= 60)
            {
                Console.WriteLine("Grade: B");
            }
            else
            {
                Console.WriteLine("Grade: C");
            }

            // Switch Case Example
            int day = 3;

            switch (day)
            {
                case 1:
                    Console.WriteLine("Monday");
                    break;
                case 2:
                    Console.WriteLine("Tuesday");
                    break;
                case 3:
                    Console.WriteLine("Wednesday");
                    break;
                default:
                    Console.WriteLine("Another day");
                    break;
            }
        }
    }
}

Summary

The program starts by declaring an integer variable marks with a value of 85. It enters the if statement: First, it checks marks >= 90 ? False, so it moves to the next condition. Then, marks >= 75 ? True, so it prints Grade: A and skips the rest of the conditions. Next, the program defines a day variable with value 3. In the switch statement: Case 3 matches, so it prints Wednesday. These conditional structures are vital to make decisions in programming.

Example