Selection Statements, If and Switch

Selection Statements, If and Switch

Influencing Program Flow

There are a number of built-in mechanisms to help influence the program flow based on a number of conditionals. These types of statements are referred to as selection statements.

The if Statement

One of the most frequently used selection statements is the if statement. The if statement evaluates a condition and if that condition evaluates to be true then the execution will proceed inside of the associated code block.


using System;

public class Program
{
	public static void Main()
	{
		int x = 2;

		if (x == 2){ //Code block
			Console.WriteLine($"The value was {x}");
		}

	    if (x == 2) //No code block
			Console.WriteLine($"The value was {x}");

	}
}
                

If the condition in the if statement does not evaluate to true, then there can be an optional ‘else’ code block that can be specified. The else statement will always match with the very last if statement.


using System;

public class Program
{
	public static void Main()
	{
		int x = 3;

		if (x == 2){ //Code block
			Console.WriteLine($"The value was {x}");
		} else {
			Console.WriteLine($"The value was {x}, which is not 2");
		}
	}
}
                

The else if statement can pass execution to its code block if the original if statement evaluated to false, but satisfies another alternate condition. For example.

You can have as many else if statements associated with an if statement as you wish.

The switch Statement

The switch statement allows for the program execution to be influenced based on a value that a specific variable might be assigned. This is a good alternative to having many else if statements as it is generally considered to be cleaner code.

The default value is for when none of the switch cases are satisfied.


using System;

public class Program
{
	public static void Main()
	{
		int x = 45;

		switch(x)
		{
			case 43:
				Console.WriteLine("The number is 43");
				break;
			case 44:
				Console.WriteLine("The number is 44");
				break;
			case 45:
				Console.WriteLine("Bingo");
				break;
			case 46:
				Console.WriteLine("The number is 46");
				break;
			default:
				Console.WriteLine("Sorry there was not a condition for this number");
				break;
		}
	}
}
                

The break keyword is included so that when a condition is met and the code for that case is executed, the execution will break out of the switch statement. This means that time is not wasted checking all of the other conditions. This is the case as generally only one of the cases will ever be true.

Switch statement examples

Logic for switch statement

If you wish for more than one case to execute the same action, then you can stack the switch cases like so:


                    using System;

public class Program
{
	public static void Main()
	{
		int x = 44;

		switch(x)
		{
			case 43:
			case 44:
			case 45:
				Console.WriteLine("Bingo");
				break;
			case 46:
				Console.WriteLine("The number is 46");
				break;
			default:
				Console.WriteLine("Sorry there was not a condition for this number");
				break;
		}
	}
}
                

Switch Statements in C# 7.0

In C# 7 you can use switch statements to identify more than values. Now you can use a switch statement to differentiate on types.


using System;

public class Program
{
	public static void Main()
	{
		object x = 5;

		switch(x)
		{
			case int i:
				Console.WriteLine("This is an integer value");
				break;
			case string s:
				Console.WriteLine("This is a string value");
				break;
		}
	}
}