In this article Control Statements in Java we give the information about Control Statements in Java are used to control the flow of a program. These enable the program to decide which code block should be executed when and how many times.
Control Statements in Java:
Control Statements in Java are used to control the flow of a program. These enable the program to decide which code block should be executed when and how many times. Control statements are mainly of three types:
- Decision-Making Statements
- Looping Statements
- Jump Statements
1. Decision-Making Statements
These statements help the program decide which block should be executed. It includes the following types:• if statement: If the condition is true, the code will execute.
if (condition) {
// Code to execute if condition is true
}
// Example
public class AgeCheck {
public static void main(String[] args) {
int age = 18; // You can change this value to test with different ages
if (age >= 18) {
System.out.println(“You are eligible for voting.”);
} else {
System.out.println(“You are not eligible for voting.”);
}
}
}
//Output
You are eligible for voting.
if-else statement: If the condition is true then one code block will execute, otherwise the other.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
// Example
import java.util.Scanner;
public class OddEvenCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number: “);
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + ” is an even number.”);
} else {
System.out.println(number + ” is an odd number.”);
}
scanner.close();
}
}
//Output:
Enter a number: 10
10 is an even number.
if-else if ladder:
For checking multiple conditions.
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if all conditions are false
}
// Example
import java.util.Scanner;
public class AgeCategory {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter your age: “);
int age = scanner.nextInt();
if (age < 13) {
System.out.println(“You are a child.”);
} else if (age >= 13 && age < 18) {
System.out.println(“You are a teenager.”);
} else if (age >= 18 && age < 60) {
System.out.println(“You are an adult.”);
} else {
System.out.println(“You are a senior citizen.”);
}
scanner.close();
}
}
// Output:
Enter your age: 30
You are a teenager.
switch statement:
To select different cases based on a value.
switch (variable) {
case value1:
// Code if variable == value1
break;
case value2:
// Code if variable == value2
break;
default:
// Default code
}
// Example:
import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number (1-7) for the day of the week: “);
int day = scanner.nextInt();
switch (day) {
case 1:
System.out.println(“Sunday”);
break;
case 2:
System.out.println(“Monday”);
break;
case 3:
System.out.println(“Tuesday”);
break;
case 4:
System.out.println(“Wednesday”);
break;
case 5:
System.out.println(“Thursday”);
break;
case 6:
System.out.println(“Friday”);
break;
case 7:
System.out.println(“Saturday”);
break;
default:
System.out.println(“Invalid day number. Please enter a number between 1 and 7.”);
break;
}
scanner.close();
}
}
//Output:
Enter a number (1-7) for the day of the week: 2
Monday
2. Looping Statements
These statements are used to execute a code block repeatedly. There are following types of loops in Java:
for loop: For a fixed number of iterations.
for (initialization; condition; increment/decrement) {
// Code to execute repeatedly
}
// Example
public class ForLoopExample {
public static void main(String[] args) {
// Using a for loop to print numbers from 1 to 10
for (int i = 1; i<= 10; i++) {
System.out.println(i);
}
}
}
Output:
1
2
3
4
5
6
7
8
9
10
while loop: As long as the condition is true, the code will execute.
while (condition) {
// Code to execute while condition is true
}
// Example:
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number: “);
int number = scanner.nextInt();
int sum = 0;
// While loop to calculate sum of digits
while (number != 0) {
int digit = number % 10; // Extract the last digit
sum += digit; // Add the digit to the sum
number /= 10; // Remove the last digit
}
System.out.println(“Sum of digits: ” + sum);
scanner.close();
}
}
Output:
Enter a number: 123
Sum of digits: 6
do-while loop:
The code will execute at least once, after which the condition is checked.
do {
// Code to execute
} while (condition);
// Example:
import java.util.Scanner;
public class DoWhileExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
// Do-while loop example
do {
System.out.print(“Enter a positive number: “);
number = scanner.nextInt();
} while (number <= 0); // Loop will repeat if number is not positive
System.out.println(“You entered: ” + number);
// Print numbers from 1 to the entered number
int i = 1;
do {
System.out.println(i);
i++;
} while (i<= number);
scanner.close();
}
}
Jump Statements
Jump statements are used to immediately change the execution flow of a program.
break statement: To exit the loop or switch statement.
break;
// Example
public class BreakExample {
public static void main(String[] args) {
// Loop through numbers 1 to 10
for (int i = 1; i <= 10; i++) {
// If i equals 5, break out of the loop
if (i == 5) {
System.out.println(“Break at i = ” + i);
break;
}
System.out.println(i);
}
System.out.println(“Loop exited.”);
}
}
Output:
1
2
3
4
5
Loop exited.
continue statement: To skip the current iteration of the loop and go to the next iteration.
continue;
// Example:
public class ContinueExample {
public static void main(String[] args) {
// Loop through numbers 1 to 10
for (int i = 1; i <= 10; i++) {
// If i equals 5, skip the rest of the current iteration
if (i == 5) {
System.out.println(“Skipping i = ” + i);
continue;
}
System.out.println(i);
}
System.out.println(“Loop completed.”);
}
}
Output:
1
2
3
4
6
7
8
9
10
Loop completed.
return statement:
To return control from the method.
return value;
public class ReturnExample {
// Method to add two numbers and return the result
public static int add(int a, int b) {
return a + b; // Return the sum of a and b
}
public static void main(String[] args) {
// Call the add method and store the result
int sum = add(5, 7);
// Print the result
System.out.println(“The sum of 15 and 17 is: ” + sum);
}
}
//Output:
The sum of1 5 and 17 is: 32
These control statements are important components of programming in Java and by using them properly you can easily implement complex logic.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining