Open In App

C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)

Last Updated : 06 Sep, 2025
Comments
Improve
Suggest changes
33 Likes
Like
Report

Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. 

A programming language uses control statements to control the flow of execution of program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program.

Conditional Statements of C#

There are few conditional statements of C# is mentioned below:

  • if
  • if-else
  • if-else-if
  • Nested if
  • Switch
  • Nested Switch

1. If Statement

The if statement checks the given condition. If the condition evaluates to be true then the block of code/statements will execute otherwise not. 

Syntax

if ( condition ) {
//code to be executed
}

Note: If the curly brackets { } are not used with if statements then the statement just next to it is only considered associated with the if statement. 

Flowchart:

if_statement
If statement

Example: Using if statement

C#
using System;

public class Geeks 
{
	public static void Main(string[] args)
	{
		string name = "Geek";
      
      	// Using if statement
		if (name == "Geek") {
			Console.WriteLine("GeeksForGeeks");
		}
	}
}

Output
GeeksForGeeks

2. If-else Statement

The if statement evaluates the code if the condition is true but what if the condition is not true, here comes the else statement. It tells the code what to do when the if condition is false.

Syntax

if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}

Flowchart:

if_else_statement
if-else

Example: Using if-else statement

C#
using System;

public class Geeks 
{
	public static void Main(string[] args)
	{
		string name = "Geek";
      
      	// Using if-else statement
		if (name == "Geeks") {
			Console.WriteLine("GeeksForGeeks");
		}
		else {
			Console.WriteLine("Geeks");
		}
	}
}

Output
Geeks

3. If-else-if ladder Statement

The if-else-if ladder is used when you need to test multiple conditions one after another.

  • The program evaluates each condition in order.
  • As soon as one condition is true, its block executes and the ladder ends.
  • If none of the conditions are true, the else block (if provided) executes.

Syntax

if(condition1)
{
// code to be executed if condition1 is true
}
else if(condition2)
{
// code to be executed if condition2 is true
}
else if(condition3)
{
// code to be executed if condition3 is true
}
...
else
{
// code to be executed if all the conditions are false
}

Flowchart:

if_else_if_ladder_statement
if-else-if

Example: Using if-else-if ladder

C#
using System;

class Geeks 
{
	public static void Main(String[] args)
	{
		int i = 20;

      	// Using If-else-if ladder
		if (i == 10)
			Console.WriteLine("i is 10");
		else if (i == 15)
			Console.WriteLine("i is 15");
		else if (i == 20)
			Console.WriteLine("i is 20");
		else
			Console.WriteLine("i is not present");
	}
}

Output
i is 20

4. Nested - If Statement

If statement inside an if statement is known as nested if. if statement in this case is the target of another if or else statement. When more than one condition needs to be true and one of the condition is the sub-condition of parent condition, nested if can be used.

Syntax

if (condition1)
{
// code to be executed
// if condition2 is true
if (condition2)
{
// code to be executed
// if condition2 is true
}
}

Flowchart:

first_if
Nested if

Example: Using Nested-if statement

C#
using System;

class Geeks 
{
	public static void Main(String[] args)
	{
		int i = 10;

		if (i == 10) {

			// nested - if statement will only be executed if statement above it is true
			if (i < 12)
				Console.WriteLine("i is smaller than 12 too");
			else
				Console.WriteLine("i is greater than 11");
		}
	}
}

Output
i is smaller than 12 too

5. Switch Statement

Switch statement is an alternative to long if-else-if ladders. The expression is checked for different cases and the one match is execute. Break Statement is used to move out of the switch. If the break is not used, the control will flow to all cases below it until break is found or switch comes to an end. There is default case (optional) at the end of switch, if none of the case matches then default case is executed.

Syntax

switch (expression)
{
case value1: // statement sequence
break;
case value2: // statement sequence
break;
.
.
.
case valueN: // statement sequence
break;
default: // default statement sequence
}

Flowchart:

switch_statement
Switch Statement

Example: Using Switch case

C#
using System;

public class Geeks 
{
	public static void Main(String[] args)
	{
		int number = 30;
      
      	// Using Switch case 
		switch(number)
		{
			case 10: 
            	Console.WriteLine("case 10");
				break;
			case 20: 
            	Console.WriteLine("case 20");
				break;
			case 30: 
            	Console.WriteLine("case 30");
				break;
			default: 
            	Console.WriteLine("None matches"); 
				break;
		}
	}
}

Output
case 30

6. Nested Switch

Nested Switch case are allowed in C# . In this case, switch is present inside other switch case. Inner switch is present in one of the cases in parent switch.

Example:

C#
using System;

public class Geeks
{
	public static void Main(String[] args)
	{
		int outter = 2;
      	int inner = 3;

      	// Using Nested Switch Case
		switch(outter){
          	case 1:
            	Console.WriteLine("Outter Case 1");
            	break;
          	case 2:
            	Console.WriteLine("Outter Case 2");
            	switch(inner)
            	{
              		case 1:
                		Console.WriteLine("Inner Case 1");
                		break;
              		case 2:
                		Console.WriteLine("Inner Case 2");
                		break;
              		case 3:
                		Console.WriteLine("Inner Case 3");
               			break;
              		default:
                		Console.WriteLine("Default Inner Run");
             			break;
            	}
            	break;
          	default:
            	Console.WriteLine("Default Outter Run");
            	break;
        }
	}
}

Output
Outter Case 2
Inner Case 3
Suggested Quiz
5 Questions

In C#, if curly braces { } are omitted in an if statement, which statement will be executed?

  • A

    All statements until the next else

  • B

    Only the statement immediately following the if

  • C

    None of the statements

  • D

    All statements until the end of method

Explanation:


What is the purpose of using a nested if statement in C#?

  • A

    To evaluate multiple conditions in parallel

  • B

    To execute a condition inside another condition

  • C

    To replace a switch statement

  • D

    To reduce the number of lines in code

Explanation:


What happens if break is omitted in a C# switch case?

  • A

    Compilation error

  • B

    Control automatically exits the switch

  • C

    Execution continues into the next case

  • D

    Default case is executed

Explanation:


Which of the following is NOT allowed in a C# switch case?

  • A

    Duplicate case values

  • B

    Constant values

  • C

    Default case

  • D

    String type as case

Explanation:


What is the effect of using goto default; inside a switch case?

  • A

    Terminates the program

  • B

    Exits the switch immediately

  • C

    Jumps to the default case execution

  • D

    Skips the current case and goes to the next one

Explanation:


Image
Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Explore