Conditions

Welcome!
In this page we will explain what are conditions in a program, and the different ways they are used in a computer program.

In general – conditions are used in order to separate different scenarios to different behaviors of the program.
For example, in the “Counting” program, there is a condition that determines whether we need to continue counting or if we need to stop the counting process.

Conditions compare the values of one or more variables to the values of other variables in order to determine what is the correct decision to take, and determine what should be the next steps of the program.

Types of conditions

if … else … anyway

The first and most strait forward type of condition is the “if” condition.
Using the if condition we check if a certain condition is taking place, or if a certain condition is true.

You can think of the “if” condition like you think of it in your day to day language. It is very similar to saying “If there’s something good on TV, I will watch it, else – I will go study, anyway – I’ll going to sleep afterwards”.

if (myOpinionOn(TV.currentShow()).isGood()) {
    watchTV();
} else {
    goStudy();
}
goToSleep();

So when you write an “if” condition in your program it’s like saying: “If I’m in this situation – I will do this, otherwise – I will do that, and anyway – I need to continue and do all the other things I have to do”.

While / Do … While

There are cases in which you need to repeat the same operation over and over while a specific condition is met. Like in our counting program in which we need to continue counting while our counter is not higher than our ending number.

In many “while” conditions, our intuition prefers to think using the “until” condition. For example, if you set a goal for yourself to walk 5 kilometers, you intuitively think you need to walk until you reach 5 kilometers distance. But when you write a computer program, you will often need to translate any “until” condition to a “while” condition.
So instead of saying: “I need to keep walking until I reach 5 kilometers” you will have to say “I need to keep walking while I don’t reach 5 kilometers“.

int distanceGoal = 5000;
while (distanceWalked < distanceGoal) {
    keepWalking();
}

The condition in which we use in our “Counting” program is actually a “while” condition, which says: “while counter is not higher than end – keep counting”.

Switch … Case … Default

Another type of condition is the “switch” condition.
The “switch” condition is used when you have several different cases that each one of them require different handling.

You can think of the “switch” condition like a call routing system that gives you several options to select from by pressing the relevant number on your phone. After you press a number – the system checks which number you pressed and transfer you to the relevant service.
For example, a call routing system can give you the following options:

For sales – press 1
For customer service – press 2
For billing – press 3

This system can be represented by the following “switch” condition:

int pressedButton = getCustomerSelection();
switch (pressedButtun) {
    case 1:
    transferCallToSales();
    break;
    case 2:
    transferCallToCustomerService();
    break;
    case 3:
    transferCallToBilling();
    break;
    default:
    say("The option you selected is not available.");
    sayAvailableOptions();
    break;
}

Scopes

If you look at all the examples above – you can see that all the conditions have curly brackets right after them. These curly brackets specify the scope of actions that are relevant for the condition above them.

For example, when you use the If … Else condition – all the commands between the left curly bracket and the right curly bracket will be executed if the condition in the if is true. If the condition in the “If” is false – all the commands in the scope will be “ignored” and the execution will jump to the “else” scope.

When you use the “While” condition – the commands in the scope of that “while” will be executed while the condition of the “while” is true. When the condition of the “while” is false – the next command that will be executed will be the first command which is after the scope of the “while” – meaning that all the commands between the curly brackets will be ignored, and the program execution will jump to the first command after the right curly bracket of the “while” scope.

Now we have enough information in order to be able to write the first program, which will be the simple counting program from first programming basics page.

You can find the instructions of writing this first program in the next page.