Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+20 votes

On 4.4 of the practice-its, can you include multiple months in one if statement since some months have the same number of days in them so would have the same result, or do you have to do individual else if statements for each one?

asked in CSC211_Winter2018 by (1 point)

3 Answers

+13 votes

Given what we've learned so far, the shortest way to write this code is to have IF / ELSE IF conditions for each of the months that do NOT have 31 days, and then use the "ELSE" clause to handle all the 31-day months (which is the most common). That way you don't need to have 12 conditional statements!

(However, if you want to look ahead to Chapter 5 in the textbook, you'll find that you can shorten your code some more by using OR statements... but that's not necessary.)

answered by (508 points)
+12 votes

You just need to make separate if/else statements for each month ( So a total of 12 statements).

answered by (1 point)
+10 votes

One trick I ended up doing is that dividing the months out into 3 group: February, From Jan to July, and from Aug to Dec. and I noticed from Jan to July all the odd number months have 31 days, and from Aug to Dec all the even number months have 31 days. So that can help you lower you code to 6 lines i think. I hope this helps

answered by (1 point)