If Then Else

Computers are dumb – understanding this is fundamental to learning how to tell them to do what you want.

If Then Else
If Then Else
They can’t think for themselves – what they can do is complete lots of little tasks, which you can string together to solve larger problems.
Stringing the tasks together is where humans are needed.
‘Control Flow’ or ‘Program Flow’ basically means deciding the order in which things happen in your program – what happens when, and under what circumstances.
Conditional statements like ‘IF THEN ELSE’ are a powerful way of controlling your program flow. They are a way of saying

“If a certain condition is true, do this.  Otherwise, do this”.

The condition has to be something that is either true or false – it has to be a question with a yes or no answer.

 Is it raining?

Imagine you want to add up the price of only the blue items in a shopping basket.
Taking each item in turn, you want to add it to your total if it is blue, or discard the item if it is not blue.
In plain English, your IF THEN ELSE statement would be:
IF this item is blue
THEN add the price of the item to your total
OTHERWISE discard the item

 

or as a simple set of statements in PHP:

 

if($colour == 'blue')
{
   $total = $total + $price;
}

This might seem rather limited – but when combined with some other control structures, it gives you the power to do pretty much anything you can conceive of.

Leave a Reply