Java Control Statements (if, switch)
Quality Thought is recognized as the best software testing institute in Hyderabad, offering top-notch training in manual testing, automation testing, and full stack testing tools. With a focus on industry-relevant curriculum and hands-on practice, Quality Thought prepares students for real-world software testing challenges. The institute provides expert-led training on popular tools and frameworks like Selenium, TestNG, Jenkins, Git, Postman, JIRA, Maven, and Cucumber, covering both frontend and backend testing essentials.
Quality Thought’s Full Stack Testing course is specially designed to make students proficient in both manual testing fundamentals and automated testing tools, along with exposure to API testing, performance testing, and DevOps integration. The institute stands out for its experienced trainers, live projects, placement support, and flexible learning options including classroom and online modes.
Whether you are a fresher aiming for a job or a working professional looking to upskill, Quality Thought offers customized learning paths. Its strong industry connections ensure regular placement drives and job interview
Java Control Statements (if, switch)
In Java, control statements manage the flow of execution based on conditions. Two commonly used conditional control statements are if and switch.
1. if Statement
The if statement is used to execute a block of code only if a specified condition is true.
java
Copy
Edit
int age = 18;
if (age >= 18) {
System.out.println("Eligible to vote");
}
if-else Statement
Provides an alternative block of code when the condition is false.
java
Copy
Edit
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
else-if Ladder
Used for checking multiple conditions.
java
Copy
Edit
int marks = 75;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
2. switch Statement
The switch statement is used to execute one code block among many options based on a variable’s value.
java
Copy
Edit
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
break stops further case checks.
default runs if no match is found.
Conclusion
Java control statements like if and switch are essential for decision-making. Use if for range-based or complex conditions, and switch for fixed, known values. Both improve code flexibility and logic flow.
Read More
Comments
Post a Comment