Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Clarify...while....  RSS feed

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{
boolean a;
while(a = true)
{}
}

will it work...
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it wont work. = is an assgnment operator not a boolean ope.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will compile.

But if

If there is any statement after while(true) its unreachable.
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like if(a = true), will while(a = true) work?...............
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){
}
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while(boolean b = true)//1
if(boolean b = true)//2

Above two are correct or not..........
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. They need expression and not declaration.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting
This code not compile

while (false){
int i = 5;
}
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will not compile because i=5 is never reached
 
Vladimir Scheglov
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joshua
But why this code compile fine?

if (false){
int i = 5;
}
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In if statements, the body of if will be compiled only if compiler can deduce the condition to be true.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
free 1 hour java lesson
https://coderanch.com/t/695988/free-hour-java-lesson
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!