Results 1 to 8 of 8
  1. #1
    linkxs is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default switch statements & conditionals

    Hi,
    I'm a beginner in java. I was wondering if it's possible to have a conditional statement in a switch statement. For example:
    Java Code:
    switch(thing)
    case thing<80:
    i++;
    break;
    case thing<70:
    i--;
    break;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    linkxs is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: switch statements & conditionals

    Heh, you have a point. I don't know why I haven't tried it yet.
    I've tried googling it and looked through two java books, couldn't find anything on this.
    Java Code:
    import java.util.*;
    
    public class test
    {
    	public static void main(String[] args)
    	{
    		int n =0;
    		Scanner kbd = new Scanner(System.in);
    		n = kbd.nextInt();
    		
    		switch (n)
    		{
    			case (n<10):
    				System.out.println("less than 10");
    				break;
    			case n>10:
    				System.out.println("more than 10");
    				break;
    		}
    	}
    }
    javac "test.java" (in directory: C:\Users\linkxs\Desktop\tempjava)
    test.java:13: incompatible types
    found : boolean
    required: int
    case (n<10):
    ^
    test.java:16: incompatible types
    found : boolean
    required: int
    case n>10:
    ^


    I'll take that as a no.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: switch statements & conditionals

    I agree with your take on it. Also when in doubt, check out what the Java Language Specification or JLS has to say about this: JLS 14.11 The Switch Statement.

    It will tell you that case must be followed by a constant expression or an enum.

  5. #5
    linkxs is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: switch statements & conditionals

    Damn, I really wish that would be possible. Otherwise I have to do a ton of branching ifs..

  6. #6
    Aero is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: switch statements & conditionals

    Basicially a switch lets you choice, so if you want n>10 and n<10 and n=10 (because of default), well you have to declare all the choices. At least if I'm not mistaken.

    However if I'm correct you could use this
    Java Code:
    public class test
    {
    	public static void main(String[] args)
    	{
                 Scanner kbd = new Scanner(System.in);
    	     int n = kbd.nextInt();
    		if (n>10)
    		{
                       System.out.println("Number is more then 10 ");
    	        }
                    else                                                                  //n<=10
    		{
                        System.out.println("Number is less than 10 or equal to 10");
                    }
    	}
    }
    Then again if your work with more and larger n's it gets really branched.
    Last edited by Aero; 09-25-2011 at 03:25 AM. Reason: Addition

  7. #7
    linkxs is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: switch statements & conditionals

    Yeah, exactly, that's the issue I was having: I had a ton of branched if's, and I thought I'd put switch in there instead, but then it becomes even longer.

  8. #8
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: switch statements & conditionals

    you can do it, in visual basic :))

Similar Threads

  1. switch statements
    By jim01 in forum New To Java
    Replies: 7
    Last Post: 04-10-2011, 10:52 PM
  2. Using a switch/case with my if statements
    By coding in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 08:01 AM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  4. Replies: 2
    Last Post: 11-22-2008, 05:30 AM
  5. Do this without conditionals...
    By Arez in forum New To Java
    Replies: 4
    Last Post: 10-06-2008, 02:15 PM

Posting Permissions

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