aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarify...while.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarify...while...." Watch "Clarify...while...." New topic
Author

Clarify...while....

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
{
boolean a;
while(a = true)
{}
}

will it work...


$Hi/\bEr
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
No it wont work. = is an assgnment operator not a boolean ope.


If you worry you cannot work... If you work you need not worry
Pinkal Patel
Ranch Hand

Joined: Jun 16, 2006
Posts: 57
It will work

while (a=true)

couse
1) var 'a' is boolean type
2) and while(a) using varialve not directly true so no compile time error


Pinkal Patel<br />SCJP 1.5<br />Preparing for SCWCD
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142

This will compile.

But if

If there is any statement after while(true) its unreachable.
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Like if(a = true), will while(a = true) work?...............
Anoop Kavalloor
Greenhorn

Joined: Sep 06, 2004
Posts: 15
The class will compile fine and when u run the class u ll get an infinite loop. The line while(a=true) make the variable a as true. It is equivalent of writing

boolean a = true;
while(a){
}


I am not completely useless.<br />I can be used as a bad example.<br /> <br />SCJP 1.4
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
while(boolean b = true)//1
if(boolean b = true)//2

Above two are correct or not..........
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
No. They need expression and not declaration.
Vladimir Scheglov
Greenhorn

Joined: Jun 19, 2006
Posts: 20
Interesting
This code not compile

while (false){
int i = 5;
}
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
This will not compile because i=5 is never reached
Vladimir Scheglov
Greenhorn

Joined: Jun 19, 2006
Posts: 20
Hi Joshua
But why this code compile fine?

if (false){
int i = 5;
}
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
Te compiler is not smart enough to identify not reachable code using if.
I think we have to simply remember this.
If there is any logic please let me know.
Joel Arnold
Greenhorn

Joined: Jun 21, 2006
Posts: 9
I read that there is a rationale for this seemingly inconsistant behavior, it is made this way to allow programmers to have code that runs only while debugging :

for example with the use of a constant DEBUG that will be set to false in production code :

if (DEBUG)
System.out.println("I wouldn't wanna see that in production code...");

Hope this helps !

Cheers !

Joel
Finner Jones
Ranch Hand

Joined: Jun 12, 2006
Posts: 39
Hi guys,
Here's my penny's worth.
Example:



The syntax for while is

while (expression) {
// do something
}

Therefore (a=false) is an expression which is left until runtime
whereas (false) is already known at compile time so there is an error.
Does that make sense ?

Finner
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Originally posted by S Thiyanesh:

If there is any statement after while(true) its unreachable.


Hi Thiyanesh..
i just checked it... this code also runs fine because it's while(true) so the statement just after while is always reachable but if it's while(false) then it will give compiler error of saying that the statement below while is unreachable.

code: while(true)
{
System.out.println("Compiles fine");
}

code: while(false)
{
System.out.println("Unreachable statement");
//since at compiletime the statements below while are unreachable
}


Regards
swapnil


SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


In if statements, the body of if will be compiled only if compiler can deduce the condition to be true.


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarify...while....