Conditional Statements in Java
Conditional statements allow us to change the default flow of a program. They transfer
the control of a program from one statement to another depending on the condition
provided. If the provided condition returns true, a block of specified statements is
executed. The conditional statements are also known as decision-making statements.
There are four decision making statements in Java as follows:
● if statement
● if... else statement
● if ... else if… statement
● switch statement
Let's learn about decision making statements in Java.
if statement:
The if statement is the most basic conditional statement in Java that allows us to test a
condition before executing a block of statements. If the provided conditional expression
returns true, the statements specified in the body of the if statement are executed.
The syntax of the if statement is
if(< conditional_expression >)
[statements]
}
For example:
public class IfStatement
{
public static void main(int num)
{
[Link]("Entered number is: "+ num);
if(num % 2 == 0)
{
[Link]("The number is even");
}
[Link]("Statement outside the if statement");
}
}
Run the program twice for values 22 and 7 respectively. When you enter 22, the condition
(22 % 2 == 0) returns true. So, the statement inside the body of the if statement gets
executed.
When you enter the value 7, then the output will appear as shown:
Because the condition (7 % 2 == 0) returns false. So, the statement inside the body of the if
statement does not gets executed and only the statement outside the if statement gets
executed.
If…else statement:
The if...else statement is used to execute either of the block of statements from if or else
statements. When the condition next to the if keyword evaluates to true, the statement(s)
inside the if block will be executed. Otherwise, the statement(s) inside the else block will be
executed.
The syntax of the if...else statement is:
if(< conditional_expression>)
{
[statements]
}
else
{
[statements]
}
For example,
public class IfElseStatement
{
public static void main(int num)
{
[Link]("Entered number is: + num);
if(num % 2==0)
{
[Link]("The number is Even");
}
else
{
[Link]("The number is odd");
}
[Link]("Statement outside the if...else statement");
}
}
As you did in the previous example, run the program twice for the values 22 and 7
respectively. When you enter 22 the condition (22 % 2 == 0) returns true. So, the output
will appear as shown:
When you enter the value 7, then the output will appear as shown:
This is because condition (7 % 2 == 0) with the if statement returns false, so the control
goes to the statements in the else block.
if...else if… statement:
The if...else if… statement helps us to test multiple conditions and execute one out of the
multiple blocks of statements. These blocks are specified by if and else if… keywords. The
else block will be executed if any of the given conditions in the if and the else if… blocks
does not evaluates to true. The if...else if… statement is also known as if… else if… ladder.
The syntax of the if...else...if statement is:
if(< conditional_expression >)
{
[statements]
}
else if(< conditional expression >)
{
[statements]
}
.
.
.
else
{
[statements]
}
For example,
public class IfElseIfStatement
{
public static void main(char ch)
{
char C=ch;
[Link]("Entered character is: " + ch);
if (C == 'W')
{
[Link]("Enjoy Winter");
}
else if (C == 'R')
{
[Link]("Enjoy Rainy");
}
else if (C=='S')
{
[Link]("Enjoy Summer");
}
else
{
[Link]("Invalid Character");
}
}
}
Run this program four times with the inputs 'W', 'R', 'S' and 'T' respectively. You will see the
output as "Enjoy Winter", "Enjoy Rainy", "Enjoy Summer" and "Invalid Character"
respectively. This is because each time condition is checked with the conditional expression
written after the if and else...if keywords and executes the statements written inside the
block where the perfect match is found.
The Switch statement:
Using the long if...else ladder becomes tedious. It makes the code more complex. Java
provides a new statement called switch. The switch statement is used as the substitute of
the if...else if… ladder. It is a multi-way branch statement that allows a variable to be
checked for equality with a list of values. Each value in the list is called a case. The case
keyword is used with the switch keyword to define a case. The switch statement also uses
the break and default keywords.
The break keyword is used to stop the execution of the switch statement when a match
case is found. On the other hand, the default keyword is used to specify some code to be
executed if there is no matched case found.
Syntax of the switch statement is:
switch (variable)
{
case value1:
[statements]
break;
case value2:
[statements]
break;
............
............
case valueN:
[statements]
break;
default:
[statements]
}
For example,
public class SwitchStatement
{
public static void main(int dayNumber)
{
switch (dayNumber)
{
case 1:
[Link]("Today is Monday");
break;
case 2:
[Link]("Today is Tuesday");
break;
case 3:
[Link]("Today is Wednesday");
break;
case 4:
[Link]("Today is Thursday");
break;
case 5:
[Link]("Today is Friday");
break;
case 6:
[Link]("Today is Saturday");
break;
case 7:
[Link]("Today is Sunday");
break;
default:
[Link]("Invalid day number");
}
}
}
After compiling the preceding program, run it twice and enter values 6 and 8. When, you
enter the value 6, the case 6 will be executed and the output "Today is Saturday" will be
displayed on the terminal as shown:
When you enter the value 8, none of the cases will be matched. Hence, the default case will
be executed and the output "Invalid day number" will be displayed on the terminal as
shown:
Programs:
Write a program to find greater of the two numbers accepted from user using Scanner
class.
import [Link];
public class GreaterNumber {
public static void main(String[] args) {
// Create Scanner object to take input
Scanner sc = new Scanner([Link]);
// Accept two numbers from user
[Link]("Enter the first number: ");
int num1 = [Link]();
[Link]("Enter the second number: ");
int num2 = [Link]();
// Compare and display the greater number
if (num1 > num2) {
[Link]("The greater number is: " + num1);
} else if (num2 > num1) {
[Link]("The greater number is: " + num2);
} else {
[Link]("Both numbers are equal.");
}
}
}
Write a program to find greater of the three numbers accepted from user using Scanner
class.
import [Link];
public class GreatestOfThree {
public static void main(String[] args) {
// Create Scanner object
Scanner sc = new Scanner([Link]);
// Accept three numbers from user
[Link]("Enter the first number: ");
int num1 = [Link]();
[Link]("Enter the second number: ");
int num2 = [Link]();
[Link]("Enter the third number: ");
int num3 = [Link]();
// Find the greatest number
int greatest;
if (num1 >= num2 && num1 >= num3) {
greatest = num1;
} else if (num2 >= num1 && num2 >= num3) {
greatest = num2;
} else {
greatest = num3;
}
// Display result
[Link]("The greatest number is: " + greatest);
}
}
Write a Java program to check whether the accepted number via agruments is even
or odd or zero.
public class CheckNumber {
public static void main(int number) {
[Link]("Enter a number: " + number);
if (number == 0) {
[Link]("The number is zero.");
} else if (number % 2 == 0) {
[Link]("The number is even.");
} else {
[Link]("The number is odd.");
}
}
}
Write a menu-driven program to accept the length and breadth of a rectangle.
Calculate and display the perimeter and diagonal of the rectangle as per the
user’s choice using Scanner class.
Program
import [Link];
public class RectangleMenu {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Accept length and breadth
[Link]("Enter the length of the rectangle: ");
double length = [Link]();
[Link]("Enter the breadth of the rectangle: ");
double breadth = [Link]();
// Display menu
[Link]("\n--- Menu ---");
[Link]("1. Calculate Perimeter");
[Link]("2. Calculate Diagonal");
[Link]("Enter your choice (1 or 2): ");
int choice = [Link]();
// Perform calculation based on choice
switch (choice) {
case 1:
double perimeter = 2 * (length + breadth);
[Link]("Perimeter of the rectangle: " + perimeter);
break;
case 2:
double diagonal = [Link]((length * length) + (breadth * breadth));
[Link]("Diagonal of the rectangle: " + diagonal);
break;
default:
[Link]("Invalid choice! Please enter 1 or 2.");
}
}
}
Write a Java program by using the switch statement to display month name according
to the number of the month.
public class Month
{
public static void main(int MonthNo)
{
switch (MonthNo)
{
case 1:
[Link]("Number " + MonthNo + " month is
January"); break;
case 2:
[Link]("Number " + MonthNo + " month is
February"); break;
case 3:
[Link]("Number " + MonthNo + " month is
March"); break;
case 4:
[Link]("Number " + MonthNo + " month is April");
break;
case 5:
[Link]("Number " + MonthNo + " month is May");
break;
case 6:
[Link]("Number " + MonthNo + " month is June");
break;
case 7:
[Link]("Number " + MonthNo + " month is July");
break;
case 8:
[Link]("Number " + MonthNo + " month is
August"); break;
case 9:
[Link]("Number " + MonthNo + " month is September");
break;
case 10:
[Link]("Number " + MonthNo + " month is
October"); break;
case 11:
[Link]("Number " + MonthNo + " month is November");
break;
case 12:
[Link]("Number " + MonthNo + " month is December");
break;
default:
[Link]("Number " + MonthNo + " month is invalid month number");
}
Write a program to input a number and check if the number is zero, positive or
negative using if…else…if statement.
Program
public class ZeroPosNeg
{
public static void main(int no)
{
[Link]("Entered number is: "+no);
if(no==0)
{
[Link]("The number is zero");
}
else if(no>0)
{
[Link]("The number is positive");
}
else
{
[Link]("The number is negative");
}
Write a program to find the greatest among the three numbers using logical operators
Program
public class Greatest
{
public static void main(int n1, int n2, int n3)
{
[Link]("First number is: "+n1);
[Link]("Second number is: "+n2);
[Link]("Third number is: "+n3);
if(n1>n2 && n1>n3)
{
[Link]("The first number is greatest");
}
else if(n2>n1 && n2>n3)
{
[Link]("The second number is greatest");
}
else if(n3>n1 && n3>n2)
{
[Link]("The third number is greatest");
}
else
{
[Link]("No single number is greatest");
}
}
Write a Java program by using the switch statement to display days of the week
according to the character entered.
public class WeekDays {
public static void main(char day) {
[Link]("Entered character out of (M, T, W, H, F, S, U) is: "+day);
switch (day) {
case 'M':
[Link]("Monday");
break;
case 'T':
[Link]("Tuesday");
break;
case 'W':
[Link]("Wednesday");
break;
case 'H':
[Link]("Thursday");
break;
case 'F':
[Link]("Friday");
break;
case 'S':
[Link]("Saturday");
break;
case 'U':
[Link]("Sunday");
break;
default:
[Link]("Invalid character. Please enter M, T, W, H, F, S, or U.");
}
}
}
********************