GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Infinite Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Infinite Loop" Watch "Infinite Loop" New topic
Author

Infinite Loop

Srini Hari
Greenhorn

Joined: Jun 20, 2000
Posts: 11
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 15, 2000
Posts: 25
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

Joined: May 10, 2000
Posts: 98
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

Joined: Nov 22, 2008
Posts: 18944
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
 
GeeCON Prague 2014
 
subject: Infinite Loop