Switch Statement


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Switch Statement

  1. #1
    Join Date
    Oct 2005
    Posts
    36

    Switch Statement

    Hello,

    Is it possible to make a variable in, say, Case 1, recognised from, say, Case 2 in a Switch statement?

    Example:

    Code:
    import java.io.*;
    
    class Switch
    {
    	public static void main (String [] args) throws IOException
    	{
    		BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    		String x = in.readLine ();
    		int y = Integer.parseInt(x);
    		switch (y)
    		{
    		   case 1 : {int a = 5;int b = 6;}
    		   case 2 : {int c = a*b;}
    		}
    	}
    }
    In this case, the compiler complains that it cannot find variables a and b. Is there a method to solve this matter so that something declared in a case can be accessed from another one? Thanks.

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    Just put your declarations outside of the switch statement and initialize them inside. For example:
    Code:
    int a = 0, b = 0, c = 0;
    switch (y)
    		{
    		   case 1 : a = 5;b = 6;
    		   case 2 : c = a*b;
    		}
    You just have to make sure that you have good default values.

    Hope this helps.
    ~evlich

  3. #3
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    ....
    Last edited by mr1yh1; 11-13-2005 at 06:32 PM. Reason: same as above evlich wrote

  4. #4
    Join Date
    Oct 2005
    Posts
    36
    Quote Originally Posted by evlich
    Just put your declarations outside of the switch statement and initialize them inside. For example:
    Code:
    int a = 0, b = 0, c = 0;
    switch (y)
    		{
    		   case 1 : a = 5;b = 6;
    		   case 2 : c = a*b;
    		}
    You just have to make sure that you have good default values.

    Hope this helps.
    But if you run this code, case 2 doesn't return 30 (i.e. 5x6), it multiplies the values of a and b outside the switch statement (i.e. 0x0).

    What I would like is that case 2 multiplies the values of a and b in case 1. I don't know if this is possible.

  5. #5
    Join Date
    Aug 2003
    Posts
    313
    That is basically because of the nature of a switch statement, it is only going to execute the code that corresponds to the value that you are switching on. If you always want c to be 30 if y is 2, you just say something like c = 30. It sounds like a switch statement is not what you should be using for this, exactly what functionality are you trying to achieve?
    ~evlich

  6. #6
    Join Date
    Oct 2005
    Posts
    36
    Quote Originally Posted by evlich
    That is basically because of the nature of a switch statement....
    I thought so but wasn't sure....... thanks anyway..... I'll use some other statement.

  7. #7
    Join Date
    Aug 2003
    Posts
    313
    What exactly is it that you are trying to do?
    ~evlich

  8. #8
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    Quote Originally Posted by Dreamer
    But if you run this code, case 2 doesn't return 30 (i.e. 5x6), it multiplies the values of a and b outside the switch statement (i.e. 0x0).

    What I would like is that case 2 multiplies the values of a and b in case 1. I don't know if this is possible.
    it happens there what you want to do , if y = 1..
    becos you dont use "break" statements.
    be careful about it, if you dont use "break"s , it will be a error-prone code.

  9. #9
    Join Date
    Oct 2005
    Posts
    36
    Quote Originally Posted by evlich
    What exactly is it that you are trying to do?
    I'm having this assignment where you have to create sort of a forum where users can choose to register or to login. So I created Case 1 for Registration and Case 2 for Login. I put no 'break' in Case 1 so that the user, after registration, goes to Case 2 to Login. Since the username was input from the user in Case 1, the function to match the username in Case 2 couldn't find the username variable since it's in Case 1.

    Anyway, I managed to do it by creating blocks. Thanks.

Similar Threads

  1. How do you loop a switch statement?
    By 72chevy in forum Java
    Replies: 3
    Last Post: 11-01-2005, 10:48 AM
  2. Replies: 2
    Last Post: 05-18-2003, 11:16 PM
  3. Use of Select Case or Switch Statement
    By Daryl Shockey in forum Database
    Replies: 2
    Last Post: 05-28-2002, 04:50 PM
  4. Does VB.NET have a C# like "using" statement?
    By Cali LaFollett in forum .NET
    Replies: 24
    Last Post: 09-16-2001, 04:43 PM
  5. switch statement
    By James Lin in forum .NET
    Replies: 9
    Last Post: 08-15-2000, 11:54 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center