Mon. Jan 23rd, 2023

Branches

One of the most fundamental abilities of code is being able to run sections of code based on conditions. For example, imagine a login page – this has to do two different things:

  • You got the login details correct : it should confirm this and proceed with the log in
  • You got the details incorrect : a message will be shown and the login will not proceed

IF

The keyword ‘if’ is used to ask whether a condition has been met, and if it has, run some code. The code will not be run if the condition is not met.

C#

int a = 5;
int b = 5;
if (a == b)
{
  //This code will run if a is the same as b
  Console.WriteLine("They are the same");
}

In C#, the condition goes inside the parentheses – in the case above, the condition is whether a is equal to b. Any arithmetic operator can be used here, and boolean operators can be used to create compound conditions.

After the if statement there is a pair of curly braces: these identify the code that will be run if the condition evaluates to true. If the condition is not true, then the code within the braces is skipped over, and execution continues from after the closing curly brace.

Python

a = 5
b = 5
if a == b:
  #This code (indented) will run if a is the same as b
  print("They are the same")

In Python, parentheses are not strictly required, although can be used and are needed for more complex conditions.

Following the condition there is a ‘:’ – this indicates that the following black of code will only execute if the condition is true. Note that this block is indented.

When the code ceases to be indented, it will execute regardless of the condition – it is the equivalent to the closing curly brace when coding in C#.

Else

Else is the programming keyword for ‘otherwise’. All conditions must be boolean – that is they can only be true or false. This means that the use of else will allow you to provide a code path that will execute if the original condition is not met. For example:

C#

Console.WriteLine("Enter a number:");
int number = Convert.ToInt32(Console.ReadLine());
if (number % 2 == 0)
{
  Console.WriteLine("That was an even number");
}
else
{ 
  Console.WriteLine("That was an odd number");
}


Python
number = int(input('Enter a number:'))
if number % 2 == 0:
  print("That was an even number")
else:
  print("That was an odd number")

In the example above, we ask the user to enter a number and use the mod operator to divide by two. If the remainder is 0, it was an even number. There is only one other possibility here – that it is an odd number – and therefore we can use ‘else’ to process any result other than even.

Note that the layout is the same as for the if statement.

Else if

Of course, there are many comparisons that may have more that two possible outcomes. For these cases, we use else-if: it is a way of saying ‘ok, the condition was not met, how about…’. The following example expands on the above, by first looking to see if the number is divisible by five, and then by two (even or odd).

C#

Console.WriteLine("Enter a number:");
int number = Convert.ToInt32(Console.ReadLine());
if (number % 5 == 0)
{
Console.WriteLine("Number is divisible by 5!");
}
else if (number % 2 == 0)
{
  Console.WriteLine("That was an even number");
}
else
{ 
  Console.WriteLine("That was an odd number");
}


Python
number = int(input('Enter a number:'))
if number % 5 == 0:
  print("Number is divisible by 5!")
elif number % 2 == 0:
  print("That was an even number")
else:
  print("That was an odd number")

Note that in C#, we use else if to add an addition condition. In Python, this is abbreviated to elif. Otherwise, the format remains the same.

The first condition to evaluate to true will be executed. If you enter the number ’10’, as it is a multiple of five, the message relating to this will be displayed. Because the condition was true, it will not attempt to see whether it is also a multiple of two. Remember else means otherwise!