Home > Net >  Why don't some if/else statements use the curly {} brackets?
Why don't some if/else statements use the curly {} brackets?

Time:10-15

I have a serious question that's been bugging me for the longest time now. I've recently been assigned to do the Logic-1 > alarmClock on CodingBat and I came up with this code as my solution:

public String alarmClock(int day, boolean vacation) {
  if (!vacation) {
    if (0 < day && day != 6) {
      return "7:00";
    } else {
        return "10:00";
      }
  } 
  
  if (vacation) {
    if (0 < day && day != 6) {
      return "10:00";
    } else{
      return "off";
    }
  }
   
}

When I go to run this code I keep getting this message saying "Compile Problems: missing return statement line:18." After tweaking the code and trying other solutions I gave up and eventually asked a friend how they solved the code to which they showed me this code:

public String alarmClock(int day, boolean vacation) {
  if (!vacation) {
    if(0 < day && day != 6)
      return "7:00";
    else
      return "10:00";
  } else if (day > 0 && day != 6)
      return "10:00";
    else
      return "off";
    
}

He gave me a few suggestions but the one I 've been curious about is why the second example doesn't have the curly brackets. I was taught that an if statement was to have the curly brackets to contain the body of code within the block and at this point I'm confused why the second code doesn't have any curly brackets next to some of the if/else statements.

CodePudding user response:

Look closely the 1st example. You have 1 method and all the return statements are used inside of some if blocks.

{
    if ()
    {
        return 
    }
    
    if ()
    {
        return
    }
}

Whats the problem over there? What would happen if these two if are false? What actually the method will return? You are missing a 3rd return statement there... just before the last '}'.

So, lets go to the 2nd example. You have 1 if statement with condition, 1 else if and finally the 'else' check.

{
    if ()
    {
        return 
    }
    
    else if ()
    {
        return
    }
    else
        return
}

It is covering all the scenarios in this case with the else check.

To understand that imagine this one:

private static String getDayName(int day)
{
    if (day == 1) {
        return "Monday";
    }
    
    if (day == 2) {
        return "Tuesday";
    }
    
    // What would returned if the day is equal to 3?
}

So, your code should cover all scenarios. With some changes, you can avoid the extra checks like that:

public String alarmClock(int day, boolean vacation){
    if (0 < day && day != 6){
        return vacation ? "10:00" : "7:00";
    }
    
    return "off";
}

P.s I'm not sure about the day checks, i used your condition instead of the second example

CodePudding user response:

When you replace if (vacation) with else every control flow returns a value. In fact it is a compiler limitation: it sees if (vacation) and assumes that when the condition does not hold (the else so to say), you reach the end of the method without a return of a value.

The compiler is not so clever that in that case the first if (!vacation) will have caused a return with value.

public String alarmClock(int day, boolean vacation) {
    if (!vacation) {
        if (0 < day && day != 6) {
            return "7:00";
        } else {
            return "10:00";
        }
    } else {
        if (0 < day && day != 6) {
            return "10:00";
        } else {
            return "off";
        }
    }  
}

The code assumes a day value 0 .. 6, with 0 probably Sunday and 6 proably Saturday. The DayOfWeek enum is nicer. DayOfWeek.getValue() will deliver the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).

public String alarmClock(DayOfWeek day, boolean vacation) {
    if (!vacation) {
        return switch (day) {
               case SATURDAY, SUNDAY -> "10:00";
               default -> "7:00";
               };
    } else {
        return switch (day) {
               case SATURDAY, SUNDAY -> "off";
               default -> "10:00";
               };
    }  
}

The switch expression is relatively new, but is ideal here.

(You could also keep in mind the localized WeekFields.)

CodePudding user response:

When there will be only one statement for any block (if,else,any loop etc.), then use of {} becomes optional. If there will be multiple statements, then we will have to use {}.

CodePudding user response:

if there is only one statement, you don't need to use { and }. But it can be dangerous. Apple had this problem in the past.

  • Related