Let us first focus on determining if it is Spring. We write one condition to check if the value is greater than or equal to 3. We write a second condition to check if the value is less than or equal to 5. Finally we combine them with an and (&&) operator.
Here is some basic code to check if a given month falls within the Spring bracket:
The output will be:
If you change the month variable to 8, for instance, you will get:
Let us expand the code to cover the other seasons. Summer and Fall are similar in format to the above code blocks. We use the “else” condition to trap for Winter because it involves a non-contiguous set of months.
An advantage of the above way of checking if a value is in between a given range is that the && operator “short circuits”, i.e., if the first condition is not met then the second condition is not evaluated because there is no way for the overall AND expression to be true if one of them evaluates to false.
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.