reset password
Author Message
jgarc629
Posts: 76
Posted 23:04 Feb 10, 2017 |

I'm having issues writing the code since it says I need a boolean variable but doubles cannot be booleans. I am also having trouble with the if and else statements. Can someone help me get started with this problem? 

sdo8
Posts: 54
Posted 02:23 Feb 11, 2017 |

Booleans can only be true or false. If statements tests whether or not the condition(between the parenthesis) is true, and if it is, it runs the code between the brackets after the condition. The else if statement is similar to the if statement, however, it is used more often if you are looking for very specific criteria or more requirements for the code to be run. The else if statement doesn't run when the condition(between the parenthesis, such as x==2) is false. The else statement ends the if statement and runs only IF none of the if statements are satisfied. You have to have an else statement if you have an if statement.

Basically (just an example)
double x = 2

if (x == 3) { System.out.println("this statement x=3 is true, so x is " + x)} // as you can see, this condition is NOT true, so the code between the brackets will NOT run. This if statement ends up being false, so the code goes to the next line.

else if ( x==4) {System.out.println("this statement x=4 is true, so x is " + x)] // same with this one. The condition is NOT true, so the code within the brackets do not run. 

else { System.out.println("x is actually just equal to " + x)} // When all of the if and else if statements are false, the else statement runs the code in the brackets. So the output should be "x is actually just equal to 2".

Within the specified conditions, you don't necessarily have to use == all the time. You can use < , > , or <= and >=. Hope this helps, and I hope this doesn't qualify as cheating

Last edited by sdo8 at 02:23 Feb 11, 2017.
kknaur
Posts: 540
Posted 09:49 Feb 11, 2017 |

No cheating here.  This was a good explanation.