Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# - Conditional Statements



C# offers a variety of conditional statements that allow you to control the flow of your program based on different conditions. Here are the main types of conditional statements in C#:

1. if Statement

The if statement evaluates a condition and executes a block of code if the condition is true.

csharp
int number = 10;

if (number > 0)
{
    Console.WriteLine("The number is positive.");
}

2. if..else Statement

The if..else statement executes one block of code if the condition is true, and another block if the condition is false.

csharp
int number = -5;

if (number > 0)
{
    Console.WriteLine("The number is positive.");
}
else
{
    Console.WriteLine("The number is non-positive.");
}

3. if..else if..else Statement

The if..else if..else statement allows you to test multiple conditions.

csharp
int number = 0;

if (number > 0)
{
    Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
    Console.WriteLine("The number is negative.");
}
else
{
    Console.WriteLine("The number is zero.");
}

4. Nested if Statements

You can nest if statements within each other to evaluate more complex conditions.

csharp
int number = 5;

if (number > 0)
{
    if (number % 2 == 0)
    {
        Console.WriteLine("The number is positive and even.");
    }
    else
    {
        Console.WriteLine("The number is positive and odd.");
    }
}
else
{
    Console.WriteLine("The number is non-positive.");
}

5. switch Statement

The switch statement provides an efficient way to dispatch execution to different parts of code based on the value of an expression.

csharp
char grade = 'B';

switch (grade)
{
    case 'A':
        Console.WriteLine("Excellent!");
        break;
    case 'B':
        Console.WriteLine("Well done");
        break;
    case 'C':
        Console.WriteLine("Good");
        break;
    case 'D':
        Console.WriteLine("Needs improvement");
        break;
    case 'F':
        Console.WriteLine("Failed");
        break;
    default:
        Console.WriteLine("Invalid grade");
        break;
}

6. Ternary Operator

The ternary operator (?:) is a shorthand for if..else statements.

csharp
int age = 18;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(status);  // Outputs: Adult

Example Code:

Here's a complete example illustrating different conditional statements:

csharp
using System;

class Program
{
    static void Main()
    {
        int number = 15;

        // if..else if..else statement
        if (number > 0)
        {
            Console.WriteLine("The number is positive.");
        }
        else if (number < 0)
        {
            Console.WriteLine("The number is negative.");
        }
        else
        {
            Console.WriteLine("The number is zero.");
        }

        // Nested if statement
        if (number > 0)
        {
            if (number % 2 == 0)
            {
                Console.WriteLine("The number is even.");
            }
            else
            {
                Console.WriteLine("The number is odd.");
            }
        }

        // switch statement
        char grade = 'B';
        switch (grade)
        {
            case 'A':
                Console.WriteLine("Excellent!");
                break;
            case 'B':
                Console.WriteLine("Well done");
                break;
            case 'C':
                Console.WriteLine("Good");
                break;
            case 'D':
                Console.WriteLine("Needs improvement");
                break;
            case 'F':
                Console.WriteLine("Failed");
                break;
            default:
                Console.WriteLine("Invalid grade");
                break;
        }

        // Ternary operator
        int age = 21;
        string status = (age >= 18) ? "Adult" : "Minor";
        Console.WriteLine("Status: " + status);
    }
}

These conditional statements in C# are powerful tools for controlling program flow based on various conditions. If you need more details or have specific questions about any of these, feel free to ask!




All rights reserved | Privacy Policy | Sitemap