The instruction orif statement, controls the flow of program execution. Depending on a condition, a sequence of instructions is executed or not.
if (<condition>) <instruction>
The <condition> is a Boolean expression; the result may be true or false. The instruction will be executed, only if the result is true. The <instruction> is usually replaced by a block of statements, which is nothing more than a sequence of statements separated by semicolons.
if (<condition>) {<sequence of statements separated by semicolons>}The indentation of the statements inside the if, is not mandatory. It is recommended for a better understanding of the code, as it makes it visible that statements are affected by the condition.
Example:
package com.edu4java.javatutorials;import javax.swing.JOptionPane;public class IfSentence {public static void main(String[] args) {String input = JOptionPane.showInputDialog("Enter your age");int age = Integer.parseInt(input);if (age >= 18) {JOptionPane.showMessageDialog(null, "You are an adult");}}}This program asks your age and if you are older or equal 18, it shows the message "You are an adult".
The statement int age = Integer.parseInt (input); is necessary becauseshowInputDialog returns a String type and we need to convert it to a number, to compare to 18.
If you are under 18, we do not display any messages. This can be improved using anelse block.
You can add anelse clause to theif statement, which allows you to run a statement or block of statements if the condition is not met.
if (<condition>) <instruction>else<instruction>
Using a block of instructions it would look like this:
if (<condition>) {<sequence of statements separated by semicolons>} else {<sequence of statements separated by semicolons>}If the condition is true, the first block of statements is executed, if the condition is false, the second block of statements is executed.
Example:
package com.edu4java.javatutorials;import javax.swing.JOptionPane;public class IfSentence {public static void main(String[] args) {String input = JOptionPane.showInputDialog("Enter your age");int age = Integer.parseInt(input);if (age >= 18) {JOptionPane.showMessageDialog(null, "You are an adult");}else {JOptionPane.showMessageDialog(null, "You are a minor");}}}Within the block of statements we can include any instruction we want. It is very common in programming to findif statements withinif statements. We exemplify this by solving a classic problem.
Problem: Given three sides of a triangle, say if it is equilateral, isosceles or scalene:
Equilateral triangle: three equal sides
Isosceles triangle: two equal and one different side
Scalene triangle: three different sides
package com.edu4java.javatutorials;import javax.swing.JOptionPane;public class Triangle {public static void main(String[] args) {int side1 = Integer.parseInt(JOptionPane.showInputDialog("Enter the first side"));int side2 = Integer.parseInt(JOptionPane.showInputDialog("Enter the second side"));int side3 = Integer.parseInt(JOptionPane.showInputDialog("Enter the third side"));if (side1 == side2 && side2 == side3) {JOptionPane.showMessageDialog(null, "It is an equilateral triangle");} else {if (side1 == side2 || side2 == side3) {JOptionPane.showMessageDialog(null, "It is an isosceles triangle");} else {JOptionPane.showMessageDialog(null, "It is a scalene triangle");}}}}The "int side1 = Integer.parseInt (JOptionPane.showInputDialog ("Enter the first side"));" uses as an argument the functionshowInputDialog for theparseInt function. We are replacing the two lines we used to enter a number in the previous example, for a single line.
Explanation of conditions:
If (side1 == side2 && side2 == side3) is true, all of the sides are equal. It is equilateral.
If (side1 == side2 || side2 == side3) is true, there are at least two equal sides.As we know that is not equilateral since we are inside the else block, we can conclude that it is isosceles.
If (side1 == side2 || side2 == side3) is false, we conclude that none of the sides are equal. It is scalene.
There is a conditional version that is a function instead of an instruction. It receives three parameters; a condition and two expressions. If the condition evaluates to true, the operator returns the first value after the question mark. If it is false it returns the second value.
(condition) ? <expression for true> : <expression for false>
Expressions can be of any type, but both have to be the same type.
Example:
int i = (5>4) ? 10 : 0; // i = 10char c = (false) ? 'a' : 'b'; // c = 'b'
We can rewrite the first example, when we asked about the age, using this function.
package com.edu4java.javatutorials;import javax.swing.JOptionPane;public class IfSentenceConditional {public static void main(String[] args) {int age = Integer.parseInt(JOptionPane.showInputDialog("Enter your age"));String answer = (age >= 18) ? "You are an adult" : "You are a minor";JOptionPane.showMessageDialog(null, answer);}}