aspose file tools*
The moose likes Beginning Java and the fly likes If-then Statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If-then Statements" Watch "If-then Statements" New topic
Author

If-then Statements

Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

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
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40



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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

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!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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 ==.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Dec 22, 2010
Posts: 23

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

Joined: Oct 24, 2010
Posts: 560
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.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If-then Statements