1

so I just started programming and I started with c#. In the book I'm reading (learning c# 3.0), one of the exercises was this.

Exercise 5-2. Create a program that prompts a user for input, accepts an integer, then evaluates whether that input is zero, odd or even, a multiple of 10, or too large (more than 100) by using multiple levels of if statements.

I managed to to this but the next exercise was

Exercise 5-3. Rewrite the program from Exercise 5-2 to do the same work with a switch statement.

I understand how switch statements work, but, I'm not sure how to work out if the user input number is odd/even, multiple of 10 and so on, and not use an if statement. Thank you for any help.

6
  • 1
    Could you show us what you have tried??? Commented Aug 18, 2013 at 11:04
  • A switch with a boolean thing swithced on, and a case true and case false. Commented Aug 18, 2013 at 11:04
  • Here's a link to that page in the book. Commented Aug 18, 2013 at 11:10
  • I can understand your problem. It seems like a bad exercise. It doesn't seem to be suited for a switch statement. Of course @BenjaminGruenbaum is right any if (b) { ... } else { ... } can be rewritten into switch (b) { case true: ...; break; default: ...; break; } but that is not a realistic use. You could take "odd or even" and "multiple of 10" together, assuming you made sure the number was not negative, by switching on num % 10, but I don't think that is a realistic use either. Commented Aug 18, 2013 at 11:17
  • @JeppeStigNielsen exactly. Commented Aug 18, 2013 at 11:24

1 Answer 1

1

You can do this:

int input = ...
switch (input)
{
    case 0:
        Console.WriteLine("Zero");
        default;
    default:
        switch (input < 100)
        {
            case true:
                switch (Math.Abs(input) % 10)
                {
                    case 0:
                        Console.WriteLine("Multiple of 10");
                        break;
                    case 2:
                    case 4:
                    case 6:
                    case 8:
                        Console.WriteLine("Even");
                        break;
                    default:
                        Console.WriteLine("Odd");
                        break;
                }
                break;
            default:
                Console.WriteLine("Too large");
                break;
        }
        break;
}

I don't think you can do this with a single switch in C#—unless you make it so massive as to account from every number from 0-100. You might be able to do it with a single Select statement in VB.NET, which is similar to a C# switch but has significantly different semantics.

Sign up to request clarification or add additional context in comments.

6 Comments

That should be never used in production code, but nice job :)
What about input==0 ? Also try to respect the order in which the conditions were given
@TGO Thanks, I'd forgotten about the zero case. Although I don't think you can handle the conditions exactly in the order they were given. You have to check < 100 before you check whether it is divisible by 10 or 2.
@p.s.w.g whatever, as Jeppe Stig Nielsen commented in the question, the exercise itself is ill formed
@TGO Agreed. This is not at all the kind of code I'd write in any real-world situation. There are much more relevant cases where a switch can be legitimately applied.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.