• Post Reply Bookmark Topic Watch Topic
  • New Topic

double value cannot be converted to boolean  RSS feed

 
Ann Yeong
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I keep getting the error message that shows, 'double cannot be converted to boolean'.

For example:

double value = 0;
double fruits;
        for (fruits = 1; fruits = 100; day++ )
        {
            value = (value + fruits) / 10 ;
      System.out.println ("Value is" + value);
}

I am trying to get the correct output from the below statement. Kindly help.
 
Henry Wong
author
Sheriff
Posts: 23033
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second expression of a "for" loop statement, is a condition.... meaning it needs to be a boolean. You have an int value.  Perhaps, you didn't mean to use the assignment operator?

Henry
 
Kunder Akshay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:The second expression of a "for" loop statement, is a condition.... meaning it needs to be a boolean. You have an int value.  Perhaps, you didn't mean to use the assignment operator?

Henry


Yes mate. A less than equal to would have worked(<=) .
 
Kunder Akshay
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ann Yeong wrote:Hi,

I keep getting the error message that shows, 'double cannot be converted to boolean'.

For example:

double value = 0;
double fruits;
        for (fruits = 1; fruits = 100; day++ )
        {
            value = (value + fruits) / 10 ;
      System.out.println ("Value is" + value);
}

I am trying to get the correct output from the below statement. Kindly help.



Use a less than equal to(<=) condition in the second expression of For (fruits < = 100).
Also what is the 'day' variable you are using for i.e the increment  condition of for (day ++) 
 
John Joe
Ranch Hand
Posts: 316
2
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ann Yeong, welcome to coderanch. 
Just a warm reminder, next time when you are pasting code , remember use the code tag. It will make your post more readable.

Have you seen the difference ?
 
Campbell Ritchie
Sheriff
Posts: 54089
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid using doubles with < > etc. Since floating‑point arithmetic is implicitly imprecise, you can be sure of problems from time to time. The following loop is a notorious example; run it and see how many times it runs:-Of course, your loop won't run 100×. It won't be out by one, so you won't get 99 iterations or 98 or 101. Look very closely at it and work out how long it will run for.

And again, welcome to the Ranch
 
Ann Yeong
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks and apologies for not using the code tag. The statement is able to run but I am still learning and figuring out about the incremental condition. How do I put the condition so that the output value only shows for 10 times?
 
Campbell Ritchie
Sheriff
Posts: 54089
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has nobody taught you the basic form of a for loop? Always start with this form of a for loop:-You may need to change the loop structure in special cases, but that is the basic form which everybody is familiar with. There is a reason for using that basic form: it works.
 
Campbell Ritchie
Sheriff
Posts: 54089
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and have you worked out how many times your loop will run?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!