Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Infinite Loop

 
Srini Hari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody explain to me the following
1:void infiniteLoop()
2:{
3:byte b = 1;
4:
5:while ( ++b > 0 );
6:
7:System.out.println("Welcome to Java");
8: }
A) The loop never ends(infiniteLoop).
B) Prints "Welcome to Java".
C) Compilation error at line 5. ++ operator should not be
used for byte type variables.
D) Prints nothing.
The answer is C,can anybody explain how
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Hari,
i checked out the code with the following program.
class demo
{
public static void main(String[] args)
{
infiniteLoop();
System.out.println("Hello World!");
}
static void infiniteLoop()
{
byte b = 1;

while ( ++b > 0 )
System.out.println("Welcome to Java" +b);
}
}
what this program does i print out "welcome to java" 126 times and then exits.
This is because the ++ operator promotes the byte to an integer and then adds 1. It then casts this integer back to a byte. when the byte is 127 however incrementing it by 1 makes it 128 and casting it back to a byte makes it loose data and make it -128.
to check out how 128 becomes -128 check out http://www.javaranch.com/ubb/Forum24/HTML/001303.html

since -128 is less than 0 the loops stops working and prints out "hello world". Hope this helps.
Regds.
Rahul.
[This message has been edited by rahul_mkar (edited June 20, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi srini,
I tried the program. The answer is not C. It is B. ie. It prints welcome to java. The reason is while loop is going infinitely at one point of time ++b > 0 fails and then it comes out of the loop. Hence it prints Welcome to Java.
Regards
Sripriya Srinivasan
 
rubna
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by srirpiya srinivasan:
Hi srini,
I tried the program. The answer is not C. It is B. ie. It prints welcome to java. The reason is while loop is going infinitely at one point of time ++b > 0 fails and then it comes out of the loop. Hence it prints Welcome to Java.
Regards
Sripriya Srinivasan

But i did'nt understand why the while loop fails.Can u please explain it to me
Thanks
Rubna
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rubna,
The answer is B.Regarding why the loop comes out the range of a byte variable is from -128 to 127.Hope this answers your question.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rupna,
i hope this clears up u r mind which has already been posted earlier.
This is because the ++ operator promotes the byte to an integer and then adds 1. It then casts this integer back to a byte. when the byte is 127 however incrementing it by 1 makes it 128 and casting it back to a byte makes it loose data and make it -128.
to check out how 128 becomes -128 check out http://www.javaranch.com/ubb/Forum24/HTML/001303.html

since -128 is less than 0 the loops stops working and prints out "hello world".
Regds
Rahul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic