Control Statements in Java - About and Types: Esha Gupta
Control Statements in Java - About and Types: Esha Gupta
Esha Gupta
Associat e Senior Execut ive
Updated on Sep 12, 2024 16:26 IST
Have you ever wondered how Java manages the flow of its programs so
efficiently? The three types of control statements, namely decision-making
statements, looping statements, and jump statements, allow Java programs
to execute different code paths based on conditions, loop through blocks of
code multiple times, and manage the flow of execution. Let's understand
more!
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Cont rol St at ement s in C | Meaning and Types
Co ntro l statements in C are used to determine the o rder in which the
instructio ns within a pro gram are executed. They are o f three types: Selectio n,
Iteratio n & Jump statements. Think...re ad m o re
Jump Statements
Let’s understand these three types of Control Statements in detail!
1. Decision-Making Statements
Decision-making statements in Java are constructs used to control the flow
of execution based on certain conditions. They allow a program to execute different
parts of code depending on whether a condition (or set of conditions) is true or
false. The decision-making statements in Java are primarily of 3 types, namely: if ,
if -else and switch statements.
Let's discuss each one of them in detail.
Syntax
if (condition) {
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
// Code to execute if the condition is true
}
Flowchart
For Example,
Consider a mobile recharge system where a user wants to recharge their phone. The
system checks if the user has sufficient balance in their account to make the
recharge. The cost of the recharge is Rs.299.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code
Output
Recharge successful. Remaining balance: Rs. 201
Syntax
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Flowchart
For Example,
Let's extend the mobile recharge system. If the user doesn't have enough balance
for the desired recharge, the system suggests a lower-cost recharge option (e.g.,
Rs.149).
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code
Output
Insufficient balance for Rs. 299 recharge. Would you like to recharge Rs.
149 instead?
Syntax
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
switch (expression) {
case value1:
// Code to be executed if expression equals value1
break;
case value2:
// Code to be executed if expression equals value2
break;
// ...
default:
// Code to be executed if expression does not match any case
}
Flowchart
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
For Example,
A customer orders coffee at a shop where there are three sizes available: small,
medium, and large. The cost of each size is different. The system needs to calculate
the bill based on the size of the coffee ordered by the customer. (Here, coffeeSize
is set to "medium")
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code
switch (coffeeSize) {
case "small":
price = 50; // Price for small size
break;
case "medium":
price = 70; // Price for medium size
break;
case "large":
price = 90; // Price for large size
break;
default:
price = 0; // Default case if no size matches
System.out.println("Invalid size selected.");
}
if (price != 0) {
System.out.println("Total bill for " + coffeeSize + " coffee: Rs. " + price);
}
}
}
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
2. Looping Statements
Looping statements in Java are used to execute a block of code repeatedly
based on a given condition or set of conditions. These statements are fundamental
to Java programming, allowing for iteration over data structures, repetitive tasks,
and more. The main types of looping statements in Java are f or loop, while
loop and do-while loop.
Let's discuss each one of them in detail.
Syntax
for (initialization; condition; update) {
// code block to be executed
}
Here,
Flowchart
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
For Example,
Let's say you are creating a program to calculate the factorial of a number. The
factorial of a number is the product of all positive integers up to that number. The
user will input a number, and you will calculate its factorial.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code
import java.util.Scanner;
Output
Enter a number: 7
Factorial of 7 is: 5040
Syntax
while (condition) {
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
// code block to be executed
}
Here,
Code Block: The statements inside the loop execute repeatedly until
the condition becomes f alse.
Flowchart
For Example,
You are given the task of developing a simple ATM PIN verification system in Java.
The system should prompt the user to enter their Personal Identification Number
(PIN) and validate it against a predefined correct PIN. This task simulates a common
real-world scenario encountered in Automated Teller Machine (ATM) operations,
focusing on user authentication through PIN verification.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code
if (enteredPin.equals(correctPin)) {
accessGranted = true; // Correct PIN entered, grant access
System.out.println("PIN accepted. Access granted.");
} else {
// Incorrect PIN, prompt the user to try again
System.out.println("Incorrect PIN. Please try again.");
}
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Output
Welcome to the ATM. Please enter your PIN:
Enter PIN: 1234
PIN accepted. Access granted.
Syntax
do {
// Statements to execute
} while (condition);
Here,
Statements to execute: These are the actions you want to perf orm.
This block of code is executed on each iteration of the loop, and it is
guaranteed to run at least once.
Flowchart
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
For Example,
Imagine you are creating a console-based application where users can select
different actions from a menu. The options might include performing some actions
(like viewing account details, making a transaction, etc.) and exiting the program. A
do-while loop is perfect for this scenario because you want to show the menu at
least once and then keep showing it after performing each action until the user
decides to exit.
Copy code
import java.util.Scanner;
do {
// Display the menu to the user
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
System.out.println("\n=== Menu ===");
System.out.println("1. View account balance");
System.out.println("2. Deposit money");
System.out.println("3. Withdraw money");
System.out.println("4 . Exit");
System.out.print("Enter your choice: ");
scanner.close();
}
}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Output
=== Menu ===
1. View account balance
2. Deposit money
3. Withdraw money
4. Exit
Enter your choice: 1
Account Balance: Rs.10,000
=== Menu ===
1. View account balance
2. Deposit money
3. Withdraw money
4. Exit
Enter your choice: 3
Withdrawal was successful.
3. Jump Statements
Jump statements in Java are used to transfer control to another part of the
program. They are particularly useful for breaking out of loops or skipping to the
next iteration of a loop. The three main types of jump statements are break,
continue , and return. Each serves a different purpose in controlling the flow of
execution.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Let's discuss each one of them in detail.
Syntax
break;
Flowchart
For Example,
Imagine you have a list of numbers, and you want to find out whether a specific
number exists in that list. Once you find the number, you want to stop the search
because there's no need to look further.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code
if(!found) {
System.out.println(searchFor + " not found in the array.");
}
}
}
Output
23 found at index 5
Syntax
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
continue;
Flowchart
For Example,
Let's say we have an array of daily sales amounts for a store, including both sales
and returns. Our goal is to calculate the total of all sales while ignoring the returns.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Underst anding Java Main Met hod
Have yo u ever wo ndered ho w a Java applicatio n begins its executio n? The key
lies in the Java main metho d, public static vo id main(String[] args), which serves
as the gateway fo r...re ad m o re
Syntax
Syntax f or Methods Returning a Value
return expression;
Here,
expression: This must evaluate to a value that is compatible with the method's
declared return type. If the method is supposed to return an int , for example, the
expression must evaluate to an integer value.
return;
For Example,
Let's consider a simpler real-life example using the return statement in Java.
Develop a Java program that can accurately determine whether a given year is a
leap year. A year is considered a leap year if it is divisible by 4 but not by 100 unless
it is also divisible by 400. This method takes an integer year as input and returns a
boolean value to be true if the year is a leap year and f alse otherwise.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code
if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
Output
2024 is a leap year.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Java Comment s | About , Types and Examples
Do yo u kno w what makes the co de mo re readable? Co mments, as they pro vide
valuable co ntext and explanatio ns abo ut the co de, making it easier fo r bo th the
o riginal develo pers and o thers...re ad m o re
Conclusion
Thus, understanding and effectively using control statements is a key skill for any
Java programmer, as they directly impact the logic, performance, and quality of the
software being developed. Through learning conditional, loop, and jump statements,
developers can craft solutions that are not only functional but also optimized and
easy to maintain. Keep Learning, Keep Exploring!
FAQs
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
How do looping statements work in Java?
Can you explain the dif f erence between break and continue
statements?
What are labeled statements in Java and how are they used with
loops?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
When is a f or-each loop more benef icial than a traditional f or loop?
Can a return statement be used in loops to exit both the loop and the
method?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.