• 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

Is while (1) loop possible in Java ?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this while(1) is possible  in  C  and Python . Is this possible in Java ? please give reason
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try it out? What happened?
 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Maneesh Godbole i am attaching screenshot
file-today.png
[Thumbnail for file-today.png]
 
Ranch Hand
Posts: 624
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So there is a compilation error when attempting to use while(1).
And the error is self explanatory.
Did you understand why this error occurred?
 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tapas Chand   I understood partially  but in C & python that type of while loop is very common . I will be very happy if you shed some more light on this
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The argument to a while statement must be a boolean expression in Java.

1 is not a boolean, it's an int, so you cannot use it for a while statement.
 
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, boolean expressions use true and false rather than the binary 1 and 0 used in other languages.

Does that help, Samar?

 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

samar das wrote:. . . in C & python . . .

Dor Burd wrote:. . . used in other languages. . . .

The important bit is to realise that those are other languages. The designers of Java® went to a lot of effort to remove some features of other languages, and one of the features they removed completely was using numbers instead of boolean values.
It is the same with natural languages; if I go across the Channel to Germany and speak German, I do not use English words in the middle of sentences. Similarly, when you are speaking Java®, don't try to speak C or Python or C++.

The C books I have read would probably say that while (1) ... is bad style and you sho‍uld write while (TRUE) ...
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, C did not define a boolean primitive type. Instead the convention was for 0 to be false and not-zero to be true. This is also true of a lot of other languages, and "not-zero" is therefore sometimes 1, sometimes -1, and occasionally, something even stranger.

However, the conditional expressions for if, while, and similar statements require a boolean test even though the language may not support boolean data types. Hence, you get:



because the actual meaning of that expression is implicitly:


Which is a boolean expression, since equality and inequality are boolean operators.

Or for the more abstractly-minded:


or even:


And incidentally, a long time ago I was involved with a BASIC-like language that actually had a "forever" statement defined as part of the language.

 
Master Rancher
Posts: 4796
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Incidentally, there is at least one good reason that Java was designed this way - it helps eliminate a common bug.  Consider this code:

Note that it has a = rather than ==.  Probably, the author meant to use ==, to test if x is equal to 42.  However, since instead they used =, they are instead assigning x to be 42, and also returning 42 as a side effect.  In a language that considers 42 to be true, this will compile just fine, and doSomething() will always execute, since 42 is true.  Is that what the author intended?  Probably not - Java will instead give a compile error here, and make the author fix their error.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . it helps eliminate a common bug.  Consider this code:
. . .

It also means the C/C++ trick of writing if (42 == x) ... is no longer necessary in Java®. Of course, you can reintroduce that bug with == true or == false. Mike London demonstrated that nicely here last week.
I think another reason for removing 0 == false is that it shows the distinction between arithmetic and boolean algebra/predicate algebra.
reply
    Bookmark Topic Watch Topic
  • New Topic