Conditional statement if-else
The instruction or if statement, controls the flow of program execution. Depending on a condition, a sequence of instructions is executed or not.
If statement syntax
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 because showInputDialog 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 an else block.
If - else syntax
You can add an else clause to the if 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"); } } }
Nesting statements
Within the block of statements we can include any instruction we want. It is very common in programming to find if statements within if 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 function showInputDialog for the parseInt 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.
Conditional function- Ternary operator
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 = 10 char 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); } }