Selection with the if statement
When using selection in our programs, we could argue that the application uses some sort of intelligence because it can now make decisions and do different things depending on various conditions. So, how can we make our applications smart? Well, the easiest way is with the use of
if statements, which are, sometimes, just referred to as conditions. In most languages, they will have a similar structure.
If we write a small program that asks the user for their age, the first part might look something like this:
print "Enter your age: " input age
Here, the program prints
Enter your age: on the screen. The user then enters an age and presses the Enter key to confirm the input. The value entered will be stored in the
Now we want to give different feedback depending on the age entered:
if age < 18 then print "You are young" end_if
Here, we have the condition where we check whether...