Java Control Statements
Java Control Statements
Java compiler executes the code from top to bottom. The statements in the code
are executed according to the order in which they appear. However, Java provides
statements that can be used to control the flow of Java code. Such statements
are called control flow statements.
Decision-Making statements:
As the name suggests, decision-making statements decide which statement to
execute and when. Decision-making statements evaluate the Boolean expression
and control the program flow depending upon the result of the condition provided.
There are two types of decision-making statements in Java, i.e., If statement and
switch statement.
If statements:
The condition of the If statement gives a Boolean value, either true or false. In
Java, there are four types of if-statements given below.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
1. Simple if statement:
It evaluates a Boolean expression and enables the program to enter a block of
code if the expression evaluates to true.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Example: 1
if (20 > 18) {
System.out.println("20 is greater than 18");
}
Example: 2
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
2. If Else statements
The if-else statement is an extension to the if-statement, which uses another
block of code, i.e., else block. The else block is executed if the condition of the
if-block is evaluated as false.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example: 1
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
Example:2 Check whether a number is even or odd using if...else statement.
import java.util.Scanner;
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
# Ternary Operator:
The ternary operator (? :) consists of three operands. It is used to evaluate
Boolean expressions. The operator decides which value will be assigned to the
variable. It is the only conditional operator that accepts three operands. It can be
used instead of the if-else statement.
Example:4 Check whether a number is even or odd using ternary operator.
import java.util.Scanner;
public class EvenOdd {
}
}
3. If else if ladder
The if-else-if statement contains the if-statement followed by multiple else-if
statements. In other words, we can say that it is the chain of if-else statements
that create a decision tree where the program may enter in the block of code
where the condition is true.
Syntax:
x:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
Example: 1
public class CheckCity {
public static void main(String[] args) {
String city = "Delhi";
if (city == "Meerut") {
System.out.println("City is Meerut");
} else if (city == "Noida") {
System.out.println("City is Noida");
} else if (city == "Agra") {
System.out.println("City is Agra");
} else {
System.out.println(city);
}
}
}
int n1 = 5, n2 = 9, n3 = 2;
// Compare n1 with n2 & n3
if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + " is the largest number.");
// Compare n2 with n1 & n3
else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + " is the largest number.");
// If none of the above is true, then clearly n3 is the largest.
else
System.out.println(n3 + " is the largest number.");
}
}
Exercise
Solution:
public class Alphabet {
public static void main(String[] args) {
char c = '*';
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
System.out.println(c + " is an alphabet.");
else
System.out.println(c + " is not an alphabet.");
}
}
4. Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement
inside another if or else-if statement.
Syntax:
if (condition1) {
statement1; // Executes when condition1 is true
if (condition2) {
statement2; // Executes when condition2 is true
} else {
statement2; // Executes when condition2 is false
}
}
Example:1
public class Student {
public static void main(String[] args) {
String address = "Delhi, India";
if (address.endsWith("India")) {
if (address.contains("Meerut")) {
System.out.println("Your city is Meerut");
} else if (address.contains("Noida")) {
System.out.println("Your city is Noida");
} else {
System.out.println(address.split(",")[0]);
}
} else {
System.out.println("You are not living in India");
}
}
}