• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusing Output

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the for(;
in the loop
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is
for(;
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the reply,
It is an infinite for loop.
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the reply,
It is an infinite for loop.
 
Philippe Saint-Just
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lab1:for( ; ; ) { // same as lab1:while(true) {
lab2: while(i++<5) {
[ December 18, 2005: Message edited by: Philippe Saint-Just ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic