• 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

Clarify...while....

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
{
boolean a;
while(a = true)
{}
}

will it work...
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like if(a = true), will while(a = true) work?...............
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. They need expression and not declaration.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic