Part of a series of posts explaining programming for the lay-person.
This is part of a series of posts; see the introduction to this series. This entry overviews conditional statements, a type of control constructs.
The most common control construct is the decision statement of conditional. In it’s simplest form, it provides a condition under which a statement is to be followed. “If you are Canadian, add a ‘q’ to your ID”. They can also be posed as an either-or: “If you are under 18, enter ‘M’; otherwise enter ‘A’.”
There are conditional expressions as well as conditional statements: for example, the Kronecker delta is defined as “1 if i = j, 0 otherwise”. They are, however, not as popular as conditional statements largely because popular languages express them in ugly ways. A conditional statement is composed of a guard expression, a consequent statement, and possibly a default statement. The guard expression is always of type “boolean”, meaning its value is either true or false, nothing in between. The conditional statement can take many forms, such as:
There’s nothing complicated about conditionals, though they are quite powerful. They allow the simplest element of intelligence: the ability to react differently to different situations.
Looking for comments…