Why is initializing a string in an if statement different than in a switch statement?












7














I'm learning Java and I'm making simple programs to find the season that a month is in, based off some book examples. These two classes demonstrate two ways of testing a value: if/else if statement, and switch statement. The thing i'm confused with is the string that is used to hold the season. When I declare it as just String season; it works with the if statements. But with the switch statement, doing that produces a "The local variable season may not have been initialized" error.



public class IfElse {
public static void main(String args) {
int month = 5;
String season;
// isn't initialized, works fine

if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else
season = "Fall";

// this is okay
System.out.println("May is a " + season + " month.");
}
}


Not initializing season at the same time as declaration works fine for the above code, but the season variable in the last println() for the switch produces an error if it's declared the same way.



The following code doesn't work:



public class Switch {
public static void main(String args) {
int month = 5;
String season;
// HAS to be initialized, currently causes error
switch(month) {
case(12):
case(1):
case(2):
season = "Winter";
break;
case(3):
case(4):
case(5):
season = "Spring";
break;
case(6):
case(7):
case(8):
season = "Summer";
break;
case(9):
case(10):
case(11):
season = "Fall";
break;

default:
System.out.println("Invalid month");
break;
}
System.out.println("May is a " + season + " month");
} // produces an error if season isn't initialized to null or ""
}


What causes this? Is it the braces enclosing the switch statement? How is initializing a string inside an if statement any different than initializing it inside a switch statement?



Sorry if this is extremely obvious or if it seems like a dumb question.










share|improve this question







New contributor




jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    7














    I'm learning Java and I'm making simple programs to find the season that a month is in, based off some book examples. These two classes demonstrate two ways of testing a value: if/else if statement, and switch statement. The thing i'm confused with is the string that is used to hold the season. When I declare it as just String season; it works with the if statements. But with the switch statement, doing that produces a "The local variable season may not have been initialized" error.



    public class IfElse {
    public static void main(String args) {
    int month = 5;
    String season;
    // isn't initialized, works fine

    if(month == 12 || month == 1 || month == 2)
    season = "Winter";
    else if(month == 3 || month == 4 || month == 5)
    season = "Spring";
    else if(month == 6 || month == 7 || month == 8)
    season = "Summer";
    else
    season = "Fall";

    // this is okay
    System.out.println("May is a " + season + " month.");
    }
    }


    Not initializing season at the same time as declaration works fine for the above code, but the season variable in the last println() for the switch produces an error if it's declared the same way.



    The following code doesn't work:



    public class Switch {
    public static void main(String args) {
    int month = 5;
    String season;
    // HAS to be initialized, currently causes error
    switch(month) {
    case(12):
    case(1):
    case(2):
    season = "Winter";
    break;
    case(3):
    case(4):
    case(5):
    season = "Spring";
    break;
    case(6):
    case(7):
    case(8):
    season = "Summer";
    break;
    case(9):
    case(10):
    case(11):
    season = "Fall";
    break;

    default:
    System.out.println("Invalid month");
    break;
    }
    System.out.println("May is a " + season + " month");
    } // produces an error if season isn't initialized to null or ""
    }


    What causes this? Is it the braces enclosing the switch statement? How is initializing a string inside an if statement any different than initializing it inside a switch statement?



    Sorry if this is extremely obvious or if it seems like a dumb question.










    share|improve this question







    New contributor




    jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      7












      7








      7







      I'm learning Java and I'm making simple programs to find the season that a month is in, based off some book examples. These two classes demonstrate two ways of testing a value: if/else if statement, and switch statement. The thing i'm confused with is the string that is used to hold the season. When I declare it as just String season; it works with the if statements. But with the switch statement, doing that produces a "The local variable season may not have been initialized" error.



      public class IfElse {
      public static void main(String args) {
      int month = 5;
      String season;
      // isn't initialized, works fine

      if(month == 12 || month == 1 || month == 2)
      season = "Winter";
      else if(month == 3 || month == 4 || month == 5)
      season = "Spring";
      else if(month == 6 || month == 7 || month == 8)
      season = "Summer";
      else
      season = "Fall";

      // this is okay
      System.out.println("May is a " + season + " month.");
      }
      }


      Not initializing season at the same time as declaration works fine for the above code, but the season variable in the last println() for the switch produces an error if it's declared the same way.



      The following code doesn't work:



      public class Switch {
      public static void main(String args) {
      int month = 5;
      String season;
      // HAS to be initialized, currently causes error
      switch(month) {
      case(12):
      case(1):
      case(2):
      season = "Winter";
      break;
      case(3):
      case(4):
      case(5):
      season = "Spring";
      break;
      case(6):
      case(7):
      case(8):
      season = "Summer";
      break;
      case(9):
      case(10):
      case(11):
      season = "Fall";
      break;

      default:
      System.out.println("Invalid month");
      break;
      }
      System.out.println("May is a " + season + " month");
      } // produces an error if season isn't initialized to null or ""
      }


      What causes this? Is it the braces enclosing the switch statement? How is initializing a string inside an if statement any different than initializing it inside a switch statement?



      Sorry if this is extremely obvious or if it seems like a dumb question.










      share|improve this question







      New contributor




      jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm learning Java and I'm making simple programs to find the season that a month is in, based off some book examples. These two classes demonstrate two ways of testing a value: if/else if statement, and switch statement. The thing i'm confused with is the string that is used to hold the season. When I declare it as just String season; it works with the if statements. But with the switch statement, doing that produces a "The local variable season may not have been initialized" error.



      public class IfElse {
      public static void main(String args) {
      int month = 5;
      String season;
      // isn't initialized, works fine

      if(month == 12 || month == 1 || month == 2)
      season = "Winter";
      else if(month == 3 || month == 4 || month == 5)
      season = "Spring";
      else if(month == 6 || month == 7 || month == 8)
      season = "Summer";
      else
      season = "Fall";

      // this is okay
      System.out.println("May is a " + season + " month.");
      }
      }


      Not initializing season at the same time as declaration works fine for the above code, but the season variable in the last println() for the switch produces an error if it's declared the same way.



      The following code doesn't work:



      public class Switch {
      public static void main(String args) {
      int month = 5;
      String season;
      // HAS to be initialized, currently causes error
      switch(month) {
      case(12):
      case(1):
      case(2):
      season = "Winter";
      break;
      case(3):
      case(4):
      case(5):
      season = "Spring";
      break;
      case(6):
      case(7):
      case(8):
      season = "Summer";
      break;
      case(9):
      case(10):
      case(11):
      season = "Fall";
      break;

      default:
      System.out.println("Invalid month");
      break;
      }
      System.out.println("May is a " + season + " month");
      } // produces an error if season isn't initialized to null or ""
      }


      What causes this? Is it the braces enclosing the switch statement? How is initializing a string inside an if statement any different than initializing it inside a switch statement?



      Sorry if this is extremely obvious or if it seems like a dumb question.







      java string if-statement switch-statement






      share|improve this question







      New contributor




      jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      jkofskie

      383




      383




      New contributor




      jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      jkofskie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          4 Answers
          4






          active

          oldest

          votes


















          7














          That is because you did not specify what season has to be in the default case. What happens when month is not within 1-12? season will not be initialized.



          if you are expecting strictly only 1-12 as month input, then you might want to consider throwing an Exception in default:



          default:
          throw new IllegalArgumentException("Invalid month");





          share|improve this answer



















          • 2




            And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to Fall. If month was set to 42, it would print Fall.
            – DrZoo
            1 hour ago










          • @DrZoo that is correct
            – mkjh
            1 hour ago










          • @DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
            – jkofskie
            57 mins ago



















          1














          In your if/else code, there is an assurance that the variable season will get a value. That is, the else statement.



          Your switch code does not have it. Look what will happen to the variable season if the given value for month is 13 -- it will not get a value, and will remain un-initialised.






          share|improve this answer





























            1














            In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.






            share|improve this answer





























              0














              You should use this



              public class Switch {
              public static void main(String args) {
              int month = 5;
              String season;
              // HAS to be initialized, currently causes error
              switch(month) {
              case 12:
              case 1:
              case 2:
              season = "Winter";
              break;
              case 3:
              case 4:
              case 5:
              season = "Spring";
              break;
              case 6 :
              case 7 :
              case 8 :
              season = "Summer";
              break;
              case 9 :
              case 10 :
              case 11 :
              season = "Fall";
              break;

              default:
              season = "Invalid";
              break;
              }
              System.out.println("May is a " + season + " month");
              } // produces an error if season isn't initialized to null or ""
              }





              share|improve this answer








              New contributor




              Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.


















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });






                jkofskie is a new contributor. Be nice, and check out our Code of Conduct.










                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032729%2fwhy-is-initializing-a-string-in-an-if-statement-different-than-in-a-switch-state%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                7














                That is because you did not specify what season has to be in the default case. What happens when month is not within 1-12? season will not be initialized.



                if you are expecting strictly only 1-12 as month input, then you might want to consider throwing an Exception in default:



                default:
                throw new IllegalArgumentException("Invalid month");





                share|improve this answer



















                • 2




                  And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to Fall. If month was set to 42, it would print Fall.
                  – DrZoo
                  1 hour ago










                • @DrZoo that is correct
                  – mkjh
                  1 hour ago










                • @DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
                  – jkofskie
                  57 mins ago
















                7














                That is because you did not specify what season has to be in the default case. What happens when month is not within 1-12? season will not be initialized.



                if you are expecting strictly only 1-12 as month input, then you might want to consider throwing an Exception in default:



                default:
                throw new IllegalArgumentException("Invalid month");





                share|improve this answer



















                • 2




                  And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to Fall. If month was set to 42, it would print Fall.
                  – DrZoo
                  1 hour ago










                • @DrZoo that is correct
                  – mkjh
                  1 hour ago










                • @DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
                  – jkofskie
                  57 mins ago














                7












                7








                7






                That is because you did not specify what season has to be in the default case. What happens when month is not within 1-12? season will not be initialized.



                if you are expecting strictly only 1-12 as month input, then you might want to consider throwing an Exception in default:



                default:
                throw new IllegalArgumentException("Invalid month");





                share|improve this answer














                That is because you did not specify what season has to be in the default case. What happens when month is not within 1-12? season will not be initialized.



                if you are expecting strictly only 1-12 as month input, then you might want to consider throwing an Exception in default:



                default:
                throw new IllegalArgumentException("Invalid month");






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 58 mins ago

























                answered 1 hour ago









                mkjh

                615316




                615316








                • 2




                  And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to Fall. If month was set to 42, it would print Fall.
                  – DrZoo
                  1 hour ago










                • @DrZoo that is correct
                  – mkjh
                  1 hour ago










                • @DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
                  – jkofskie
                  57 mins ago














                • 2




                  And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to Fall. If month was set to 42, it would print Fall.
                  – DrZoo
                  1 hour ago










                • @DrZoo that is correct
                  – mkjh
                  1 hour ago










                • @DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
                  – jkofskie
                  57 mins ago








                2




                2




                And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to Fall. If month was set to 42, it would print Fall.
                – DrZoo
                1 hour ago




                And to point out a small bug in the question askers if/else implementation, the else will set any invalid month to Fall. If month was set to 42, it would print Fall.
                – DrZoo
                1 hour ago












                @DrZoo that is correct
                – mkjh
                1 hour ago




                @DrZoo that is correct
                – mkjh
                1 hour ago












                @DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
                – jkofskie
                57 mins ago




                @DrZoo You're completely right. I did that out of laziness because I knew I would never set the month value out of range, but that also meant that there would never be a situation where season is uninitialized. Changing the else to an else if also gave me the same error as in the switch statement. So I now realize my problem was with when the variable is (or isn't) being given a value.
                – jkofskie
                57 mins ago













                1














                In your if/else code, there is an assurance that the variable season will get a value. That is, the else statement.



                Your switch code does not have it. Look what will happen to the variable season if the given value for month is 13 -- it will not get a value, and will remain un-initialised.






                share|improve this answer


























                  1














                  In your if/else code, there is an assurance that the variable season will get a value. That is, the else statement.



                  Your switch code does not have it. Look what will happen to the variable season if the given value for month is 13 -- it will not get a value, and will remain un-initialised.






                  share|improve this answer
























                    1












                    1








                    1






                    In your if/else code, there is an assurance that the variable season will get a value. That is, the else statement.



                    Your switch code does not have it. Look what will happen to the variable season if the given value for month is 13 -- it will not get a value, and will remain un-initialised.






                    share|improve this answer












                    In your if/else code, there is an assurance that the variable season will get a value. That is, the else statement.



                    Your switch code does not have it. Look what will happen to the variable season if the given value for month is 13 -- it will not get a value, and will remain un-initialised.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    KaNa0011

                    130110




                    130110























                        1














                        In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.






                        share|improve this answer


























                          1














                          In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.






                          share|improve this answer
























                            1












                            1








                            1






                            In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.






                            share|improve this answer












                            In your first example, there is no path through the code that fails to assign a value to 'season'. In the second example, the default case does not assign a value, so the last print ("May is...") can be executed with an uninitialized value.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            another-dave

                            4245




                            4245























                                0














                                You should use this



                                public class Switch {
                                public static void main(String args) {
                                int month = 5;
                                String season;
                                // HAS to be initialized, currently causes error
                                switch(month) {
                                case 12:
                                case 1:
                                case 2:
                                season = "Winter";
                                break;
                                case 3:
                                case 4:
                                case 5:
                                season = "Spring";
                                break;
                                case 6 :
                                case 7 :
                                case 8 :
                                season = "Summer";
                                break;
                                case 9 :
                                case 10 :
                                case 11 :
                                season = "Fall";
                                break;

                                default:
                                season = "Invalid";
                                break;
                                }
                                System.out.println("May is a " + season + " month");
                                } // produces an error if season isn't initialized to null or ""
                                }





                                share|improve this answer








                                New contributor




                                Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.























                                  0














                                  You should use this



                                  public class Switch {
                                  public static void main(String args) {
                                  int month = 5;
                                  String season;
                                  // HAS to be initialized, currently causes error
                                  switch(month) {
                                  case 12:
                                  case 1:
                                  case 2:
                                  season = "Winter";
                                  break;
                                  case 3:
                                  case 4:
                                  case 5:
                                  season = "Spring";
                                  break;
                                  case 6 :
                                  case 7 :
                                  case 8 :
                                  season = "Summer";
                                  break;
                                  case 9 :
                                  case 10 :
                                  case 11 :
                                  season = "Fall";
                                  break;

                                  default:
                                  season = "Invalid";
                                  break;
                                  }
                                  System.out.println("May is a " + season + " month");
                                  } // produces an error if season isn't initialized to null or ""
                                  }





                                  share|improve this answer








                                  New contributor




                                  Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.





















                                    0












                                    0








                                    0






                                    You should use this



                                    public class Switch {
                                    public static void main(String args) {
                                    int month = 5;
                                    String season;
                                    // HAS to be initialized, currently causes error
                                    switch(month) {
                                    case 12:
                                    case 1:
                                    case 2:
                                    season = "Winter";
                                    break;
                                    case 3:
                                    case 4:
                                    case 5:
                                    season = "Spring";
                                    break;
                                    case 6 :
                                    case 7 :
                                    case 8 :
                                    season = "Summer";
                                    break;
                                    case 9 :
                                    case 10 :
                                    case 11 :
                                    season = "Fall";
                                    break;

                                    default:
                                    season = "Invalid";
                                    break;
                                    }
                                    System.out.println("May is a " + season + " month");
                                    } // produces an error if season isn't initialized to null or ""
                                    }





                                    share|improve this answer








                                    New contributor




                                    Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    You should use this



                                    public class Switch {
                                    public static void main(String args) {
                                    int month = 5;
                                    String season;
                                    // HAS to be initialized, currently causes error
                                    switch(month) {
                                    case 12:
                                    case 1:
                                    case 2:
                                    season = "Winter";
                                    break;
                                    case 3:
                                    case 4:
                                    case 5:
                                    season = "Spring";
                                    break;
                                    case 6 :
                                    case 7 :
                                    case 8 :
                                    season = "Summer";
                                    break;
                                    case 9 :
                                    case 10 :
                                    case 11 :
                                    season = "Fall";
                                    break;

                                    default:
                                    season = "Invalid";
                                    break;
                                    }
                                    System.out.println("May is a " + season + " month");
                                    } // produces an error if season isn't initialized to null or ""
                                    }






                                    share|improve this answer








                                    New contributor




                                    Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    share|improve this answer



                                    share|improve this answer






                                    New contributor




                                    Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    answered 1 hour ago









                                    Alperen Gezgin

                                    11




                                    11




                                    New contributor




                                    Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.





                                    New contributor





                                    Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






                                    Alperen Gezgin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






















                                        jkofskie is a new contributor. Be nice, and check out our Code of Conduct.










                                        draft saved

                                        draft discarded


















                                        jkofskie is a new contributor. Be nice, and check out our Code of Conduct.













                                        jkofskie is a new contributor. Be nice, and check out our Code of Conduct.












                                        jkofskie is a new contributor. Be nice, and check out our Code of Conduct.
















                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032729%2fwhy-is-initializing-a-string-in-an-if-statement-different-than-in-a-switch-state%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Plaza Victoria

                                        Puebla de Zaragoza

                                        Musa