0

This is some part of the code.

I am getting the error "Control cannot fall through from one case label to another in case 3".

In spite of using the break statement, it is not getting detected. What is the right way to do it?

Update: Error is in case 3. Don't bother to waste your time on other cases.

switch (output)
{
    case 1:
        int num, reverse = 0;
        Console.WriteLine("Enter a Number : ");
        num = int.Parse(Console.ReadLine());
        while (num != 0)
        {
            reverse = reverse * 10;
            reverse = reverse + num % 10;
            num = num / 10;
        }
        Console.WriteLine("Reverse of Number is : "+reverse);
        Console.ReadLine();
        break;
    case 2:
        int number, sum = 0, r,square;
        Console.WriteLine("Enter a Number : ");
        number = int.Parse(Console.ReadLine());
        while (number != 0)
        {
            r = number % 10;
            number = number / 10;
            square = r * r;
            sum = sum + square;
        }
        Console.WriteLine("Sum of square of Digits of the Number : "+sum);
        Console.ReadLine();
        break;
    case 3:
        Console.WriteLine("Enter 1 for AND 2 for OR and 3 for XOR Operation");
        int answer = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Your 2 inputs are?");
        int inp1= Convert.ToInt32(Console.ReadLine());
        int inp2= Convert.ToInt32(Console.ReadLine());
        switch (answer)
        {
            case 1:
                int input3 = inp1 * inp2;
                System.Console.WriteLine("Output is" + input3);
                Console.ReadLine();
                break;
            case 2:
                int input4 = inp1 + inp2;
                System.Console.WriteLine("Output is" + input4);
                Console.ReadLine();
                break;
            case 3:
                if (inp1 == inp2)
                {
                    System.Console.WriteLine("OUTPUT IS 0");
                    Console.ReadLine();
                }
                else
                {
                    System.Console.WriteLine("Output is 1");
                    Console.ReadLine();
                }
                break;
Tim
  • 28,212
  • 8
  • 63
  • 76
YoMama
  • 41
  • 11
  • 1
    Show us the entire `switch` block. – SLaks Jan 23 '17 at 18:10
  • 7
    You probably need a `break;` outside of the if statement, not in it. – Tim Jan 23 '17 at 18:10
  • @Tim, that ain't working either. – YoMama Jan 23 '17 at 18:15
  • @Slaks, i have uploaded it. – YoMama Jan 23 '17 at 18:16
  • perhaps you're looking at the wrong case 3 switch statement. You seem to have two in your code snipped. Can you post ALL of your switch statement(s), not just the one where you think the error is happening? – sous2817 Jan 23 '17 at 18:17
  • 1
    Or better yet please read [MCVE] guidance and post minimal code demonstrating the problem. – Alexei Levenkov Jan 23 '17 at 18:19
  • Have you tried stepping through the code in the debugger? Which case 3 - the outer or the inner? – Tim Jan 23 '17 at 18:25
  • Possible duplicate of [Use a 'goto' in a switch?](http://stackoverflow.com/questions/4756084/use-a-goto-in-a-switch) – Matthew Whited Jan 23 '17 at 18:25
  • @MatthewWhited, Yes i already reffered to those answers,before posting this. I just wanna know, why doesn't break statement works here? Other than that, there are various ways of solving a similar question. – YoMama Jan 23 '17 at 18:28
  • Yeah, the title made it look like you wanted a fall though not that you were trying to fix a bug in your code. you need a second break. Use blocks to make this obvious. – Matthew Whited Jan 23 '17 at 18:29

2 Answers2

3

Your problem is that you only break out of the inner case and not the outer, so you're getting a fall through issue.

case 3 ...
             case 3:

                     if (inp1 == inp2)
                     {
                         System.Console.WriteLine("OUTPUT IS 0");
                         Console.ReadLine();


                     }

                     else
                     {
                         System.Console.WriteLine("Output is 1");
                         Console.ReadLine();

                     }
                     break;
 break; //break the outer case3
CDove
  • 1,940
  • 10
  • 19
1

Add a goto case X in place of the break for where you want the fall though to occur.

... never mind. you need an expression block on the first case 3.

case 3
{
    /// your other switch here
    break;
}

By not using scoping blocks you overlooked the outer case statement. It needs break as well as the inner statement.

Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
  • 1
    If you want to fall though that is the syntax. – Matthew Whited Jan 23 '17 at 18:28
  • Intentional fall-through wasn't stated as a goal, but even if it were, I'm not sure I'd use a `goto` in 2017. It's probably better to not use `switch`/`case` and instead use an `if` series if fallthrough is desirable – CDove Jan 23 '17 at 18:30
  • 2
    If you don't know how to use a language in 2017 it's probably best you aren't in development. There is nothing wrong with using a `goto`. It is part of the language for a reason. The if/else syntax would bloat code like this out even more. – Matthew Whited Jan 23 '17 at 18:31
  • http://stackoverflow.com/questions/5485029/best-practice-for-using-goto – CDove Jan 23 '17 at 18:36