Moses 7D
Moses 7D

Reputation: 39

Switch-case making NOT statement

class dd
    {
        public static void main(String args[])
            {
                short dd = 5;
                switch (dd)
                    {
                        case !1:
                        case !2:
                        case !3:
                        case !4:
                            System.out.println("Pf");
                    }
            }
    }

So I have this small part of code, I know it is wrong, all I want to know is how I can make the if ( SomeVariable ! = SomeNumber ) using the swtich-case command. Thank you in advance.

Upvotes: 0

Views: 56

Answers (1)

janos
janos

Reputation: 124646

A case cannot be about not-something. But you can use the default: case instead:

short dd = 5;
switch (dd)
    {
        case 1:
        case 2:
        case 3:
        case 4:
            break;
        default:
            // matched none of the above cases
            System.out.println("Pf");
    }

Upvotes: 3

Related Questions