Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In daily life we always have to make decisions and most often we do not really think about them. Making a decision means that there is always at least one alternative we could have chosen instead. The entire process is based on certain factors or parameters we have to evaluate to make the final decision. With programming the process is just the same. You have to distinguish between different conditions, which will influence the way the script finally goes. For this purpose, programming languages recognise a construction called “if and else“. You know this from your own daily experience:

“If the weather is fine, I go out for a walk, if not I will stay at home and read a book.”

That is exactly the way “if-else” works. If a certain condition is fulfilled, the script follows a given route to achieve a desired result. If the condition is not satisfied, the program has to follow another path. A very nice example for this type of decision-making is planning a holiday trip. This is something where you really have to consider many things, because holidays depend on so many factors: your partner, money, destination, time, personal preferences, the hotel, airline and so on. The first question is whether you can afford a certain destination or not:

...