Kodeclik Blog


What are Conditional Statements?

Conditional statements are very common in English and even in computer languages. Consider the conditional statement made by a parent to a child: “If it is sunny, we can go out for ice cream”. This statement indicates that we should assess the weather and if the weather is sunny, we can go to the ice cream shop.
Similarly, we need conditional statements in computer programs whenever we need to do something different if a condition is true.
Conditional statements are statements of the form: “if …. do …” or “if… then”. They are a staple ingredient in any complex program.
Below are examples of conditional statements in real programming situations.

Checking a person’s age on a website

Consider a form on a website that is asking you to enter your age. If you enter an invalid age, e.g., “-3” what should happen? The website will need to disallow that input and give some useful feedback such as “invalid age; age cannot be negative”. This is conditional logic: “if input age is negative, then print invalid age message”.

Validating a user in a login form

Similar to the above, if you mistype your login and/or password the system must output: “Invalid username/password”. This is exactly what is accomplished by a conditional statement. “If username or password or both are invalid, then print “invalid username/password””.
Sometimes you need conditionals in two directions, e.g., do something if a condition is true and do something different if the same condition is false. We accomplish this using an if..then..else statement which is a more complex conditional statement.

Classifying a number as even or odd

We can encode the logic for classifying a number as even or odd in the following manner:
Note that if you execute this conditional statement you are guaranteed to obtain either an “even” or “odd” determination because a condition is either true or false. Also observe that the statement above could also be written as:
Here we have simply changed the condition to the negation of what it used to be. As a result the conclusions are also flipped.
Note that the else clause is executed only if the if clause’s condition does not hold. Similarly, the “then” clause is executed only if the if clause’s condition is true. As a result either the “then” or “else” clauses are executed, not both.

Checking for leap years

Let us use conditional statements to encode more complex constraints about how the real world works. For instance, “if year is divisible by 4, then number of days in February is 29”. Of course this logic is in reality more complicated, e.g., if the year is divisible by 100 or 400, etc. then the conclusion is more nuanced. Below is the complete logic for finding the number of days in February.
First note that this conditional statement has one if clause and multiple else clauses. Thus, in the above conditional statement we are testing for multiple conditions, not just one condition. There is a test in the “if” clause and there are also tests in two “else” clauses before the last else clause.
Also observe that we are testing for the strictest possible condition first as otherwise we will be making wrong conclusions. This is because once one of the conditions becomes true, we stop executing the rest of the conditional statement. Thus, if a year is divisible by 400, we conclude that it is a leap year and do not check if it is divisible by 100 (it will be, but you really want to ask that question only for those years where the year was not divisible by 400, like 300). This is the purpose of the “else” statement as you might recall. In other words the “else” clause is really adding a lot of implicit conditions to your check. It is as if you had written four separate, independent checks like the below:
Note that the last conditional could be simplified to:
because if the year is not divisible by 4, it will not be divisible by 100 or 400 either. Similarly, the third conditional could be simplified to:
These simplifications above arise due to our understanding of numbers and divisibility rather than from the structure of the conditional logic itself. In other cases, it is important to faithfully render the original if..then..else” structure as we did so above.

Determining the year of college

Let us write a complex conditional statement to classify college students based on the year of college they are in. Here is how that might work.
In the above logic we are checking for years systematically and then concluding their classification. Note that unlike the leap year example students can be in only one year at a time so you do not have to worry about multiple conditions being true and thus to properly sequence them. This means that you could have written the above logic as follows:
Note that the last statement now has a proper “if” clause associated with it. Without it, the “else” would have been paired with the previous “if” (i.e., the check for year 3) and as a result if a student is not a junior the program would have erroneously concluded that the student is a senior (he/she could have been a freshman or sophomore which was tested earlier.)

Troubleshooting your car

Conditional statements are very popular in real life. If you look at your car’s manual, it likely contains a flow chart comprising a lot of conditional statements such as the picture below:
For instance, the manual would ask you “is the key in the ignition?”, “is there fuel in the car?”, “is the battery dead?” etc and based on the answers to these questions would give you a conclusion as to what might be wrong with the car.
If you liked this blogpost, checkout our article on sequences.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2024. All rights reserved.