• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is while (1) loop possible in Java ?  RSS feed

 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this while(1) is possible  in  C  and Python . Is this possible in Java ? please give reason
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it out? What happened?
 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Maneesh Godbole i am attaching screenshot
file-today.png
[Thumbnail for file-today.png]
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16048
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Dor Burd
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • 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?

 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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) ...
 
Tim Holloway
Bartender
Posts: 18781
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 56223
171
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!