Java if Comparison Statement
By Stephen Bucaro
A computer program is a list of instructions for the computer to follow. Some instructions
tell the computer to input data from a file or a device like a keyboard. Some instructions
tell the computer to output data to a file or a device like s display screen. Some
instructions tell the computer to test a variable and to follow a group of instructions based
on the result of that test. This type of instruction is called a conditional test. The most
basic conditional test is the if statement.
The code shown below uses an if statement to test if the value stored in the variable
height is less-than 72.
import javax.swing.*;
public class ifTest
{
public static void main(String s[])
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setTitle("Java if Test");
int height = 62;
String tall = "";
JTextArea myText = new JTextArea();
myText.setLineWrap(true);
if(height < 72)
{
tall = "Your height is too short.";
}
myText.append(tall);
frame.add(myText);
frame.setVisible(true);
}
}
The if statement uses the < less-than comparison operator to test the value
of height against the value 72. In this case, since the variable height was initialized
with the value 62, the test would be true and the code in the body of the if statement
would set the value of the variable tall to "Your height is too short.", which following code
would display in the program window.
Shown below is a list of comparison operators.
operator | tests for |
< | less-than |
> | greater-than |
<= | less-or-equal-to |
>= | greater-or-equal-to |
== | equal-to |
!= | not-equal-to |
You can experiment with the code by changing the value of the variable height in the line
int height = 62; or by changing the comparison operator, for example to > greater-than,
in which case the program would have no output because the if test would fail. Note: see the
article about the if-else comparison operator.
Note: I generally hate it when people add a bunch of extra code to an example. It confuses things.
But in this case I've included the javax.swing library and added code to create a
JFrame and a JTextArea. If I didn't add this extra code, you would need to use
the System.out.print command to output text, and I don't think that's an interesting
or realistic way to learn Java programming.
In the code above, a JTextArea object named myTextis created, and the line
myText.append(tall) places the tall string in the JTextArea for display
in the program window.
If you're a beginner, for this example, I recommend downloading and installing the Java Development Kit from
Oracle's web site.
Type the code into a text file using a basic text editor like Window's Notepad (Do not use a word processor
because they insert formatting codes into the file), saving the file with the .java file extension. Then
use a batch file to compile the program. The batch file needs to contain the path to the compiler and the
path to the source file, an example of compile.bat is shown below.
c:\Program Files\java\jdk1.8.0_121\bin\javac c:\Users\Stephen\Desktop\program_name.java
pause
The pause command on the second line will cause the DOS shell to stay open until you press any key,
allowing you to read any errors. If successful, the compiler will create an executable file with the
.class file extension. Then use a batch file to run the program. The batch file needs to contain
the start command, the name of the java program launcher, and the name of the compiled java class
file (without the .class extension). An example of run.bat is shown below.
start javaw program_name
The javaw launcher displays the result of your program, or error information if it fails, in a window.
|