Switch Statement Error Non Return of Character

Hi

I have just completed the console project using C#.
https://www.codecademy.com/practice/projects/console-game-challenge-project

However i had to amend my code due to an error message when I initially ran it. The code now works but I do not understand why the error appeared in the first place, could someone please explain this for me?

The error messages were:

Unreachable code detected / Not all paths return a value

This is the code that threw an error:

        public new static char UpdateCursor(string KeyPressed)
            {
            
            switch(KeyPressed)
            {
                case "DownArrow":
                    return 'v';
                    break;
                case "UpArrow":
                    return '^';
                    break;
                case "LeftArrow":
                    return '<';
                    break;
                case "RightArrow":
                    return '>';
                    break;
                default:
                    break;
            }
        }

I adjusted it to the following which now works

                public new static char UpdateCursor(string KeyPressed)
            {
            char key = 'x';
            switch(KeyPressed)
            {
                case "DownArrow":
                    key = 'v';
                    break;
                case "UpArrow":
                    key =  '^';
                    break;
                case "LeftArrow":
                    key = '<';
                    break;
                case "RightArrow":
                    key =  '>';
                    break;
            }
            return key;
        }

many thanks

the default:

                default:
                    break;

doesn’t return a value (not all paths return a value), which causes the error

Hey, @code3533172051. Consider what return does versus what break does. return hands a value, and control flow back to the caller. break simply exits the code block that contains it. The Unreachable code detected would be each break following a return. If the return is executed, no code after it in the same branch will ever be executed, and is therefore unreachable. Your function specifies a return type, so every branch/path inside the function must eventually reach a return. Your default case had no return. Another way to write your working code would be:

        public new static char UpdateCursor(string KeyPressed)
        {
            switch(KeyPressed)
            {
                case "DownArrow":
                    return 'v';
                case "UpArrow":
                    return '^';
                case "LeftArrow":
                    return '<';
                case "RightArrow":
                    return '>';
            }
        }

Edit: Fixed code. Got a little hasty originally. Oops!

1 Like

I thought that after i posted, but same error after i removed it.

Ah okay, that makes perfect sense now. I think this may be the first time i have seen an example of a switch statement without break, so my mind was focused on it requiring one.

I’ve updated my notes.

many thanks for support

1 Like