| Author |
Confusing Output
|
Akshay Singhvi
Ranch Hand
Joined: Nov 08, 2005
Posts: 93
|
|
Hello Ranchers, the code, the output is: case 1's i= 1 case 2's i= 2 case 3's i= 3 case 4's i= 4 case 5's i= 5 case default's i= -2147483647 Exception in thread "main" java.lang.AssertionError: error Mr. Akshay Fix it!!! at scjp.Code.TechnoSample.main(A.java:16) Well i can figure out till "case 5" but then why Integer.MIN_VALUE appears at default case together with AssertionError. It this something to do with infinite for loop???I can't figure out. According to my understanding ,when the application prints the value of i as 5, the control goes out of switch statement and passes to boolean expression of while loop where the expressionis true and value of i is now 6.then it should execute default statement and should print case default's i=6 toghter with AssertionError. Plz correct me if i'm wrong. Regards , Akshay
|
Regards,<br />Akshay Singhvi<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (86%)
|
 |
Philippe Saint-Just
Greenhorn
Joined: Dec 07, 2005
Posts: 15
|
|
After "case 5's i=5" prints, control returns to the for loop since i>=5. Every time the condition in the while loop is checked, i gets incremented by 1. Finally, when i==Integer.MAX_VALUE get incremented, i==Integer.MIN_VALUE. Since this value is negative, i<5. Which means you enter the while loop, go to the default condition and throw an exception. [ December 17, 2005: Message edited by: Philippe Saint-Just ]
|
 |
Niranjan Deshpande
Ranch Hand
Joined: Oct 16, 2005
Posts: 1277
|
|
what is the for(; in the loop
|
SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
|
 |
Akshay Singhvi
Ranch Hand
Joined: Nov 08, 2005
Posts: 93
|
|
It is for(;
|
 |
Akshay Singhvi
Ranch Hand
Joined: Nov 08, 2005
Posts: 93
|
|
Sorry for the reply, It is an infinite for loop.
|
 |
Akshay Singhvi
Ranch Hand
Joined: Nov 08, 2005
Posts: 93
|
|
Sorry for the reply, It is an infinite for loop.
|
 |
Philippe Saint-Just
Greenhorn
Joined: Dec 07, 2005
Posts: 15
|
|
lab1:for( ; ; ) { // same as lab1:while(true) { lab2: while(i++<5) { [ December 18, 2005: Message edited by: Philippe Saint-Just ]
|
 |
Balaji Sampath
Ranch Hand
Joined: Sep 30, 2005
Posts: 63
|
|
Just a doubt : shouldnt the above code in for loop be something like this: lab1:for(; { lab2: while(i++<5){ switch(i){ case 1:System.out.println("case 1's i= "+i); continue lab2; case 2: System.out.println("case 2's i= "+i); continue lab1; case 3: System.out.println("case 3's i= "+i); break; case 4: System.out.println("case 4's i= "+i); break lab2; case 5: System.out.println("case 5's i= "+i); break ; default:System.out.println("case default's i= "+i); assert false:"error Mr. Akshay Fix it!!!"; }// end switch }// end while } // end for regards Balaji.S
|
 |
 |
|
|
subject: Confusing Output
|
|
|