Computer Science, asked by ashutosh4138, 1 year ago

find the output of the following code.
int i=1
while(i<=6)
{
switch (i++)
{
case 2: system. out. println(i);
case 3: i++;
case 4; system. out. println(i+4);
case 5; ++i;
case 6; system. out. println(i);
default; system. out. println(i++);
}
}

Answers

Answered by nitish8089
1

.........Program..........                                      

class New{

public static void main(String[] args) {

 int i=1;

while(i<=6)

{

switch (i++)

{

case 2: System.out.println(i);

case 3: i++;

case 4: System.out.println(i+4);

case 5: ++i;

case 6: System.out.println(i);

default:System.out.println(i++);

}

}

}

}

....Output....

2

9

6

6


ashutosh4138: nitish . can you show me dry working. plz
nitish8089: please read three topic...
nitish8089: pre increment and pre decrement operator... how it works...
nitish8089: post increment and post decrement....
nitish8089: fall through in switch means when there is no break statement ... in switch...
nitish8089: if you know all this i would explain...
nitish8089: so ... first case execute is 1 due to post increment... so there is no any case match therefore default execute and here is also post increment of i... so
nitish8089: first output :2 and return the new value of i=3
nitish8089: now second time loop run i=3 : pass through switch remember here is post increment so go for check case 3: here is incremented
Similar questions