Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Help me understand how a switch statement construct works?

    My book doesn't give a class impelementation example of a switch statement. As a result, I'm confused by the errors in the following code:
    Java Code:
    package switchStatements;
    
    public class SwitchStatements {
    
    	public static void main(String[] args){
    		switch(daysOfWeek){//error: daysOfWeek cannot be resolved to a variable
    		case 1:
    			day = "Monday";//error: day cannot be resolved to a variable
    			break;
    		case 2:
    			day = "Tuesday";
    		}
    	}
    }
    Do I need variable declarations outside of the switch statement construct here or what?

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: Help me understand how a switch statement construct works?

    The difference between if-else statement and a switch (of course, there are more differences, but that's a another subject) is that the switch statement needs a break in every code block of each case condition because if you don't put one, it's going check all the conditions one after another.
    The switch statements compare the variable 'X' inside switch (X) with the values of each case, and if a condition is true will execute the code in the case.

    So if we take your code and change the switch statement with a if statement the code we are going to get :

    Of course the code below will not compile but can you find the error? if you do then you will understand whats wrong with your code too...

    Java Code:
    public class TestClass{
    	
    	public static void main(String[] args){
    		if(daysOfWeek == 1){
    			day = "Monday";
    		}else if(daysOfWeek == 2){
    			day = "Tuesday";
    		}
        }
    	
    }
    Last edited by ShadowWalker; 07-03-2016 at 12:27 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help me understand how a switch statement construct works?

    Do I need variable declarations outside of the switch statement
    Yes, the daysOfWeek and day variables need to be defined in scope before they are used. That is always true, with or without a switch statement.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Help me understand how a switch statement construct works?

    I fixed my code:
    Java Code:
    package switchStatements;
    
    public class SwitchStatements {
    
    	public static void main(String[] args){
    		int dayOfWeek = 0;
    		String day;//warning: The value of the local variable day is not used
    		switch(dayOfWeek){
    		case 1:
    			day = "Monday";
    			break;
    		case 2:
    			day = "Tuesday";
    			break;
    		case 3:
    			day = "Wednesday";
    			break;
    		case 4:
    			day = "Thursday";
    			break;
    		case 5:
    			day = "Friday";
    			break;
    		case 6:
    			day = "Saturday";
    			break;
    		case 7:
    			day = "Sunday";
    			break;
    		default:
    			day = "InvalidDayNumber";
    			System.out.println("There are only 7 days in a week pal! Try a number between 1 and 7.");
    			break;
    		}
    	}
    }
    But for some reason, my program thinks that the day variable is not used. Why?

  5. #5
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: Help me understand how a switch statement construct works?

    Well, maybe because you don't use it anywhere? :P The only thing you do, is to give a value to that variable but you don't do anything with it like printing it or make any comparison with it

  6. #6
    Join Date
    Jun 2016
    Posts
    7
    Rep Power
    0

    Default Re: Help me understand how a switch statement construct works?

    I just want to point out that a break statement exits out of a switch statement and then executes the following code. If no break statement is provided, then the next case statement will be read. This could be useful if you have many case statements that produce the same output, and you don't want to repeat it. This functions as an OR.
    Java Code:
    char letter;
    switch (letter) {
    case 'a':
    case 'A':
      // Do something
      break;
    case 'b':
    case 'B':
      // Do something else
      break;
    }
    // switch statement exits to here

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  2. Help me understand how this polymorphism example works?
    By DylanDubya in forum New To Java
    Replies: 4
    Last Post: 09-24-2012, 11:10 AM
  3. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  4. switch statement
    By droidus in forum New To Java
    Replies: 2
    Last Post: 09-21-2011, 09:54 AM
  5. Program works but don't exactly understand it
    By Danieldcc in forum New To Java
    Replies: 4
    Last Post: 10-20-2010, 06:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •