...

/

Understanding Selection Statement

Understanding Selection Statement

Learn how to use selection statements like if and switch in C# for branching code execution based on conditions, providing practical examples for both patterns.

Every application needs to be able to select from choices and branch along different code paths. The two selection statements in C# are if and switch. We can use it for all our code, but the switch can simplify it in common scenarios, such as when a single variable with multiple values requires different processing.

Branching with the if statement

The if statement determines which branch to follow by evaluating a boolean expression. If the expression is true, then the block executes. The else block is optional and executes if the if expression is false.

Press + to interact
if-else statement
if-else statement

Nested if statement

The if statement can be nested. The if statement can be combined with other if statements as else if branches, as shown in the following code:

Press + to interact
if (expression1)
{
// runs if expression1 is true
}
else if (expression2)
{
// runs if expression1 is false and expression2 if true
}
else if (expression3)
{
// runs if expression1 and expression2 are false
// and expression3 is true
}
else
{
// runs if all expressions are false
}

Code to explore if statement

Each if statement’s boolean expression is independent of the others and, unlike switch statements, does not need to reference a single value. Let’s write some code to explore selection statements like if:

Step 1: Use your preferred coding tool to add a new "Console App or console" project named SelectionStatements to the Chapter03 workspace/solution.

  • In Visual Studio Code, select SelectionStatements as the active OmniSharp project.

Step 2: In the Program.cs, delete the existing statements, and then add statements to check if a password is at least eight characters, as shown in the following code:

Press + to interact
string password = "ninja";
if (password.Length < 8)
{
WriteLine("Your password is too short. Use at least 8 characters.");
}
else
{
WriteLine("Your password is strong.");
}

Step 3: Run the code and note the result, as shown in the following output:

Your password is too short. Use at least 8 characters.

Try it yourself

Execute the provided code snippet to observe the corresponding output.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

	<ItemGroup>
		<Using Include="System.Console" Static="true" />
	</ItemGroup>

</Project>
Checking password length using if statement

Why we should always use braces with if statements

As there is only a single statement inside each block, the preceding code could be written without the curly braces, as shown in the following code:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

	<ItemGroup>
		<Using Include="System.Console" Static="true" />
	</ItemGroup>

</Project>
Code without curly braces

This style of if statement should be avoided because it can introduce serious bugs, for example, the infamous #gotofail bug in Apple’s iPhone iOS operating system.

For 18 months after Apple’s iOS 6 was released in September ...