223k views
3 votes
What is y after the following switch statement?

int x = 0;
int y = 0;
switch (x + 1) {
case 0: y = 0;
case 1: y = 1;
default: y = -1
{

Please explain so that I can learn from you.

1 Answer

4 votes

Answer:

The answer to this question is y=-1.

Step-by-step explanation:

First of all this code snippet will give error if used in the program.There is semi colon missing after y=-1 and after that there should be closing curly brace instead of open in last line.

x and y are initialized with 0.

In switch we have x+1 means we have switch(1).So case 1 will be executed the value of y will become 1.But there is no break statement after that so the compiler will not come out of the switch it will execute the next line in switch statement.default will be executed and the value of y will become -1.

User DaBlick
by
7.7k points