The if and switch statement

The if and switch statement

The if statement

The if statement has the following general form:

if (expression) 
{
    statement;
}

The if keyword is used to check if an expression
is true. If it is true, a statement is then executed. The statement can
be a single statement or a compound statement. A compound statement
consists of multiple statements enclosed by the block. A block is code
enclosed by curly brackets.

using System;

public class CSharpApp
{
    static void Main()
    {
        int num = 31;

        if (num > 0)
        {
            Console.WriteLine("num variable is positive");
        }
    }
}

We have a num variable. It is assigned 31. The if keyword
checks for a boolean expression. The expression is put between square
brackets. 31 > 0 is true, so the statement inside the block is executed.

$ ./ifstatement.exe 
num variable is positive

The condition is met and the message is written to the console.

using System;

public class CSharpApp
{
    static void Main()
    {
        int num = 31;

        if (num > 0)
        {
            Console.WriteLine("num variable is positive");
            Console.WriteLine("num variable equals {0}", num);
        }
    }
}

More statements can be executed inside the block, enclosed by
curly brackets.


We can use the else keyword to create a simple branch.
If the expression inside the square brackets following the if keyword
evaluates to false, the statement following the else
keyword is automatically executed.

using System;

public class CSharpApp
{
    static void Main()
    {
        string sex = "female";

        if (sex == "male") {
          Console.WriteLine("It is a boy");
        } else {
          Console.WriteLine("It is a girl");
        }
    }
}

We have a sex variable. It has “female” string. The boolean expression
evaluates to false and we get “It is a girl” in the console.

$ ./branch.exe 
It is a girl

We can create multiple branches using the else if keyword.
The else if keyword tests for another condition, if and only if
the previous condition was not met. Note, that we can use multiple
else if keywords in our tests.

using System;

public class CSharpApp
{
    static void Main()
    {
        int a = 0;

        if (a < 0) {
          Console.WriteLine("a is negative");
        } else if (a == 0) { 
          Console.WriteLine("a equals to zero");
        } else {
          Console.WriteLine("a is a positive number");
        }
    }
}

We have a numerical variable and we test it, if it is a negative number or
positive or if it equals to zero. The first expression evaluates to false.
The second condition is met. The program prints ‘a equals to zero’ to the
console. The rest of the branch is skipped. If the previous conditions were
not met, than the statement following the else keyword would
be executed.

The switch statement

The switch statement is a selection control flow statement.
It allows the value of a variable or expression to control the flow of program
execution via a multi way branch. It creates multiple branches in a simpler way
than using the combination of if, else if
statements.

We have a variable or an expression. The switch keyword is used
to test a value from the variable or the expression against a list of values.
The list of values is presented with the case keyword.
If the values match, the statement following the case is executed.
There is an optional default statement. It is executed, if no other
match is found.

using System;

public class CSharpApp
{
    static void Main()
    {
        string domain = Console.ReadLine();

        switch (domain) {
            case "us":
                Console.WriteLine("United States");
            break;
            case "de":
                Console.WriteLine("Germany");
            break;
            case "sk":
                Console.WriteLine("Slovakia");
            break;
            case "hu":
                Console.WriteLine("Hungary");
            break;
            default:
                Console.WriteLine("Unknown");
            break;
        }
    }
}

In our program, we have a domain variable. We read a value for the variable
from the command line. We use the case statement to
test for the value of the variable. There are several options. If the value
equals for example to “us” the “United States” string is printed to the console.

$ ./selectcase.exe 
hu
Hungary

We have entered “hu” string to the console and the program responded with “Hungary”.

Get industry recognized certification – Contact us

Menu