Java Control Structures - Conditional Branches
Java Control Structures - Conditional Branches
Conditional Control Structures allows the program to select between the alternatives during the program execution.
If Statement
It will go inside the block only if the condition is true otherwise, it will not execute the block.
Syntax:
if (condition) {
//statements (if block)
}
//other statements - executed irrespective of the condition
Example:
If the condition is true then, it will execute the if block. Otherwise, it will execute the else block.
Syntax:
if (condition) {
statements (if block)
} else {
statements (else block)
}
Example:
If - Else - If Statement
If the condition is true, then it will execute the if block. Otherwise, it will execute the else-if block. Again, if the condition
is not met, then it will move to the else block.
Syntax:
if (condition 1) {
statements (if block)
} else if (condition 2) {
statements (else - if block)
} else {
statements (else block)
}
Example:
int number = 2;
int anotherNumber = 3;
int theOtherNumber = 4;
Switch Statement
Switch statement allows program to select one action among multiple actions during the program execution.
Syntax:
Example:
int option = 2;
switch(option){
case 1:
System.out.println("Output.");
break;
case 2:
System.out.println("Another output.");
break;
default:
System.out.println("The other output.");
break;
}
Other program examples
Program #1
import java.util.Scanner;
public class PositiveOrNegative {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int inputNumber;
System.out.println("Positive or negative number");
System.out.print("Enter a number: ");
inputNumber = scanner.nextInt();
if(inputNumber >= 0) {
System.out.println("The input number is positive.");
} else {
System.out.println("The input number is negative.");
}
}
}
Program #2
import java.util.Scanner;
public class PositiveOrNegative {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int inputNumber;
System.out.println("Positive or negative number");
System.out.print("Enter a number: ");
inputNumber = scanner.nextInt();
if(inputNumber == 0) {
System.out.println("The input number is not positive or negative.");
} else if(inputNumber > 0){
System.out.println("The input number is positive.");
} else {
System.out.println("The input number is negative.");
}
}
}
Program #3
import java.util.Scanner;
public class ColorEquivalent {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int inputNumber;
System.out.println("Color Equivalent");
System.out.print("Enter a number: ");
inputNumber = scanner.nextInt();
switch(inputNumber) {
case 1: case 2: case 3:
System.out.println("The equivalent color is red.");
break;
case 4: case 5: case 6:
System.out.println("The equivalent color is yellow.");
break;
case 7: case 8: case 9:
System.out.println("The equivalent color is blue.");
break;
default:
System.out.println("Input error, there is no equivalent color.");
break;
}
}
}
Program #4
import java.util.Scanner;
public class InchesToCentimeters {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
final double CENTIMETERS_PER_INCH = 2.54;
double inches;
double centimeters;
System.out.println("Inches to Centimeters");
try {
System.out.print("Enter value in inches: ");
inches = scanner.nextDouble();
if(inches <= 0) {
System.out.println("Zero or negative input is not allowed.");
} else {
centimeters = inches * CENTIMETERS_PER_INCH;
System.out.println("Output is " + String.format("%,.2f" ,centimeters) + ".");
}
} catch(Exception e) {
System.out.println("Invalid input, letters or special symbols in inches are not allowed.");
}
}
}
Program #5
import java.util.*;
public class AreaOfTheRectangle {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
double length;
double width;
double area;
import java.util.*;
public class AreaOfTheRectangle {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
double length;
double width;
double area;
import java.util.Scanner;
public class ColorEquivalent {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int inputNumber;
System.out.println("Color Game");
try {
System.out.print("Enter a number: ");
inputNumber = scanner.nextInt();
switch(inputNumber) {
case 1: case 2:
System.out.println("The equivalent color is red.");
break;
case 3: case 4:
System.out.println("The equivalent color is yellow.");
break;
case 5: case 6:
System.out.println("The equivalent color is blue.");
break;
default:
System.out.println("Input error, please input from 1 to 6 only.");
break;
}
} catch(Exception e) {
System.out.println("Invalid input, letters or special symbols in input number are not allowed.");
}
}
}
Quick review
Including a semicolon before the statement in a one-way selection creates a semantic error. In this case, the action of the if
statement is empty.
There is no stand-alone else statement in Java programming language. Every else has a related if.
An else is paired with the most recent if that has not been paired with any other else.
A sequence of statements enclosed between braces, { and }, is called a compound statement or block of statements. A
compound statement is treated as a single statement.
When the value of the expression is matched against a case value, the statements execute until either a break
statement is found or the end of the switch structure is reached.
If the value of the expression does not matched any of the case values, the statements following the default
label execute. If the switch structure has no default label, and if the value of the expression does not match any of the
case values, the entire switch statement is skipped.