Conditionals

A conditional is something that is either true or false, like a boolean value.

Often, you want your program to make decisions. You want something to happen if a condition is true, or something else to happen if the condition is false.

Consider a program that takes four different test grades and averages them. Then it uses printLine() to show the user what the average grade is. Suppose instead, we want to print "Pass" or "Fail" depending on if the grade is a passing or failing grade. Think of what you want to do in English before you convert it to Java:
If average grade is less than 60, print "Fail", otherwise, print "Pass".
Java accomplishes this with an if/else statement: ``` ```

``````if (average < 60) {
printLine("Fail");
}
else {
printLine("Pass");
}
``````
``` ```
With the if/else statement, you first put the word "if" followed by the condition you are testing for between parentheses. In this example, the condition is "average < 60". In Java, you can use "<" and ">" to mean "less than" and "greater than" just like in math. To say "less than or equal to", you use a less than sign followed immediately by an equal sign "<=". Greater than or equal to works the same way. For testing if two things are equal, use the equal() function, like so: ``` if ( equal(name1, name2) ) { printLine("The two names are equal."); } else { printLine("Those names are different."); } ```
Conditionals can by used to react to input. You can have your program use a read function, like readDouble(), and then do something based on what the user typed in. Here is an example program that quizzes the user on multiplication, with a condition based on what they input to the program: ``` void main() { double operand1; double operand2; double product; double usersAnswer; printLine("Please enter a number"); operand1 = readDouble(); printLine("Please enter another number"); operand2 = readDouble(); product = operand1 * operand2; printLine("What is " + operand1 + " times " + operand2 + "?"); usersAnswer = readDouble(); // check to see if users answer to multiplication // problem is correct or incorrect and let him/her know if ( equal(product, usersAnswer) ) { printLine("That is correct!"); } else { printLine("Sorry, " + operand1 + " * " + operand2 + " = " + product); } } ```

Now is a good time to tell you about comments. Comments are notes to yourself that you write while writing your program. You use them so that later on when you go back and look at your program, you or someone else can easily tell what you were trying to do in your code.
To make a comment, put two slashes right next to each other (like this //) on a line, and after that write whatever you want. In the above program, there is a comment before the line with the `if` statement.

You can view the equal() functions in the JUDO Functions Reference.

Back to Input / Output     Forward to Loops