This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
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

doubt with while loop  RSS feed

 
Ranch Hand
Posts: 1083
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while(true)
{

}

now as you can see true is not assigned to any instance or class variable then how does it executes successfully
 
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "expression" in the while() loop condition is gets evaluated and which must be resulted in either "true" value or "false" value. 'true' is the Java boolean literal, and after evaluating this literal produces the same value, i.e 'true'. Hence it works..

Google for "Java boolean literals"

HTH
 
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while condition should always evaluate to a boolean value. So the end result that while has always is either true or false and not x>2 or x==5. Also in java we cannot check x = 0 like in C or C++ in a while of if. Its always a boolean value. So if you directly right a boolean value or indirectly by giving an expression its one and the same to get a boolean value.

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nitish Bangera wrote: Also in java we cannot check x == 0 like in C or C++ in a while of if.


Wrong. You can NOT use "x=0" as a boolean expression in conditional or loops in Java, whereas in C/C++, its valid assignment operation which always resulted in true condition..
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it was a mistype.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Problemo !! It happens when you jumbled up the things trying to compare the Java and C/C++ behaviour..
 
Vishal Hegde
Ranch Hand
Posts: 1083
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats the differnce between x=0 and x==0 arent they assigining value of 0 to x
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x=0 assigns the value 0 to the variable x

x==0 checks if the value of the variable x i equal to 0, if yes the result is true, if not the result is false...
 
Vishal Hegde
Ranch Hand
Posts: 1083
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sagar Rohankar ,Nitish Bangera and Irmin Okic for helping me out,i had one more query how will i cum to know whether i am prepared for SCJP6 exams..??

 
Marshal
Posts: 59101
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Irmin Okic wrote:x=0 assigns the value 0 to the variable x

Somebody earlier said "true" for that. As stated, it won't work in Java, but it will be interpreted as false in C/C++.
 
Campbell Ritchie
Marshal
Posts: 59101
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vishal Hegde wrote:Thank you . . . ,i had one more query how will i cum to know whether i am prepared for SCJP6 exams..??

Probably best to go to the SCJP forum and see what sort of questions they are asking there.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Irmin Okic wrote:x=0 assigns the value 0 to the variable x

Somebody earlier said "true" for that. As stated, it won't work in Java, but it will be interpreted as false in C/C++.


You got wrong this time, Campbell. In C/C++, the assignment operation always results into "true".
 
lowercase baba
Bartender
Posts: 12624
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sagar Rohankar wrote:You got wrong this time, Campbell. In C/C++, the assignment operation always results into "true".

Are you sure? I thought that the expression "A = 0" would be evaluated to be 0, which would be false...
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C the concept of booleans is by numerics 0 or other; 0 meaning false. Everytime a variable is intialized, it returns that value.
int x = 1; returns 1 where we don't care about it during normal intialization.

Let's get back to java. JVM has only numeric datatypes, but the compiler knows of booleans too; hence booleans are strictly required where a condition is to be evaluated.

Regards
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:

Sagar Rohankar wrote:You got wrong this time, Campbell. In C/C++, the assignment operation always results into "true".

Are you sure? I thought that the expression "A = 0" would be evaluated to be 0, which would be false...


ohh wait there, I think I was wrong, the example I checked on my VSC++ 6, is like this,

And as Himanshu said, in C/C++, value other than "0" is considered as true !!

Sorry for my wrong info and the little knowledge !!
 
Always look on the bright side of life. At least this ad is really tiny:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!