Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If-then Statements

 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have run into a problem with the program i am using for my first project.As you can see I am useing some fif-then statements to test if a condition is true or not and then print to the screen the result. There is a syntax error in each statement that I can't figure out. if someone could strieghten me out that would be great ty.
Here is my code:

 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You may be mixing languages -- Java doesn't have a "then" keyword. The "then" is implied.

Henry
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Mike!

Champ, the problem is that the sintax you're using is wrong. It's pretty simple:



The flow only enters the "if" statement if the boolean expression evaluates to true. For instance:



And finally, please take a look here. I think it will be helpful!
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another issue is that you seem to be trying to use = for comparison. While this is fine in languages like Visual Basic and Delphi / Pascal, in many other languages like Java and C you can only use ==.
 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks hope all are having a great Christmas. I am reporting that i solved the problem with the syntax and boolean statements. See code below:



But as you can see I now have a problem with my print statements ...they all print. So I was thinking that I need a different control statement to print the one "true" statement. eh? Any way i was hoping that someone would steer me in the right direction.
As always thanks in advance.
Michael

 
Paul Clapham
Sheriff
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can use the control statements you already have. You just need to adjust them so that they control something rather than having them control nothing, which is what your current code does:

This says "If yield-cost is zero then don't do anything" and the statement after that is executed unconditionally. (Which is what you said.) So review the syntax of the if-statement, preferably looking at some working examples.
 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:No, you can use the control statements you already have. You just need to adjust them so that they control something rather than having them control nothing, which is what your current code does:

This says "If yield-cost is zero then don't do anything" and the statement after that is executed unconditionally. (Which is what you said.) So review the syntax of the if-statement, preferably looking at some working examples.


Thank you Paul... I understood after I looked at the code again... It was obvious. I think I have corrected that problem...but they still fall thru instead of just printing the one "true" statement. there is something I am Missing about the "if then" control. Anyway here is my corrected code for your perusal at your earliest opportunity. (smiles)


 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean to have a variable 'yield' and another 'yeild'?

If you have a number of 'if' statements in a row with actions applicable for only the one true case, then you want to set them apart with 'else' (or 'else/if') to avoid "fall through" behavior.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic