Lesson 2.2.1 The IF statement
In broad terms, it’s an “if”. You use it day in and day out in real life.
“If I’m not home when you call round, get the key from under the flower pot.”
“If you find that the soup is bit bland, add salt.”
“If I’m not out in 20 minutes, leave without me.”
In order to understand how the code for an IF statement works we need to dissect how a normal “if” statement in everyday language works. Take the first of my examples.
“If I’m not home when you call round, get the key from under the flower pot.”
It has three parts:
- The word “if”, obvious I know, but it is how other people know what’s coming next.
- A condition. Something for the other person to check. This can only result in a yes or no. In this case it’s a check to see whether the person is home or not.
- Something to do. It’s all very well giving a conditional statement. It’s no good if there’s no action attached to it.
Lucky for us, that’s how C# phrases it too. The following is an example of a conditional program. It’s a broad extension of the “Hello World” program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GoodAfternoon1
{
class Program
{
static void Main(string[] args)
{
if (DateTime.Now.Hour >= 12)
{
System.Console.WriteLine("Good afternoon World");
}
System.Console.ReadKey(true);
}
}
}
This program will now be a bit more intelligent on what it is outputting; this code will only offer a greeting if it is the afternoon. As before, I have highlighted the main section of the program.
The line we’re interested in is the statement checking to see if it is afternoon or not.
if (DateTime.Now.Hour >= 12)
First of all, note that it begins with an “if”. I don’t need to add anything on that. The second point is the bit between the brackets. This is the condition; the bit to make the check on. C# always expects this in brackets. Try removing the brackets from the example and see the stink it kicks up.
Just like an “if” statement in real life, it should always be possible to boil the condition down to a yes or a no. In tech speak, the condition should always evaluate to a Boolean value. In the statement above the condition boils down to a simple question: Is the hour larger than or equal to twelve?
The condition uses what’s known as a range comparison operation. There are a few of these and they are mighty useful.
| Symbol |
Meaning |
| == |
Is equal to |
| > |
Is larger than |
| < |
Is smaller than |
| != |
Is not equal to |
| <= |
Is less than or equal to |
| >= |
Is larger than or equal to |
Most of these might be fairly familiar. Some of them are used in maths. As conditions can be complicated in real life, so the language for specifying the conditions in C# is very extensible. This will be covered shortly.
Finally, there’s the actual code to run should the condition be true.
{
System.Console.WriteLine("Good afternoon World");
}
They’re plastered all over the code but this is the first acknowledgement of the curly brackets. The {} brackets are for enclosing chunks of logic, an opening { bracket to denote the start and closing } bracket to mark the end.
As mentioned, the computer has no common sense. English, like nearly all spoken languages, has a lot of ambiguity involved in it. The thing is that we are very rarely aware of it. Take the following instructions on changing a car gearbox.
- Drive the car into the garage
- If the gear box is broken
- Replace it
- Throw it away
Every reasonable human being would take the “throw it away” section to be referring to if the gearbox is broken. Therefore it is telling the reader to throw away the broken gearbox. However, technically, there were no limits on exactly where the “if” covered. It could just as easily refer only to the instruction to remove the gearbox. In which case “throw it away” suddenly refers to the car and it suddenly becomes a three stooges joke.
If we were to apply the brackets to it in a hybrid code fashion we could remove all of the ambiguity.
drive car into garage;
if (gearbox is broken)
{
replace gearbox;
throw it away;
}
There, no question as to what needs to be done. The more eagle eyed amongst you will note that the block inside an IF statement is made up more executive commands. There is nothing to stop you putting in extra IF statements. This is why the code inside the curly brackets is indented. It is done automatically by the code editor. Also, it doesn’t matter how many spaces there are either. In fact C#, when it processes your code, strips out all spaces anyway.
To go back to our gearbox example, it could be expanded upon.
drive car into garage;
if (gearbox is broken)
{
if (gearbox is repairable)
{
repair it;
}
if (gearbox is not repairable)
{
replace gearbox;
throw it away;
}
}
This technique is called nesting. You could go on and on nesting IF statements inside IF statements and C# is quite able to understand it. From a human readability standpoint it would be a nightmare to follow. There are ways to mitigate this; these will be covered later on.