Java if else Control 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, and uses a second if statement to test if the value is
greater-than-or-equal-to 72.
import javax.swing.*;
public class ifTest2
{
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 = 74;
String tall = "";
JTextArea myText = new JTextArea();
myText.setLineWrap(true);
if(height < 72)
{
tall = "Your height is too short.";
}
if(height >= 72)
{
tall = "Your height is tall enough.";
}
myText.append(tall);
frame.add(myText);
frame.setVisible(true);
}
}
However, exactly the same logical function can be performed by using an if else
control statement as shown below.
import javax.swing.*;
public class ifelseCont
{
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 ifelse Test");
int height = 74;
String tall = "";
JTextArea myText = new JTextArea();
myText.setLineWrap(true);
if(height < 72)
{
tall = "Your height is too short.";
}
else
{
tall = "Your height is tall enough.";
}
myText.append(tall);
frame.add(myText);
frame.setVisible(true);
}
}
The if else statement tells the computer to run on set of instructions based on one
result of a test, and a different set of instructions based on a different result of the test.
If height is less-than 72, the code within the brackets just below the line if(height < 72)
are executed, else (otherwise), if the height is 72 or greater, the code within the brackets
just below the line else are executed.
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 74, the test would be true and the code within the brackets just below the line
if(height < 72) would set the value of the variable tall to "Your height is too short.".
If the value of height is 72 or greater, the code within the brackets just below the line
else would set the value of the variable tall to "Your height is tall enough.".
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 |
|