Selection with the if statement

Learn how statements are selectively executed based on if conditions.

The if statement

Now we know that we have selection and iteration statements and that they work with conditions, so let’s see how we can write code that uses them. Let’s start with the most common selection statement, if.

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’ll 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:

Get hands-on with 1200+ tech skills courses.