0% found this document useful (0 votes)
6 views5 pages

Java Control Statements Explained

The document explains Java control statements, which are essential for decision making and controlling program flow. It covers various types of control statements including if, if-else, nested if-else, switch-case, break, and continue, along with examples for each. Additionally, it provides practice exercises and FAQs regarding the usage of these statements in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Java Control Statements Explained

The document explains Java control statements, which are essential for decision making and controlling program flow. It covers various types of control statements including if, if-else, nested if-else, switch-case, break, and continue, along with examples for each. Additionally, it provides practice exercises and FAQs regarding the usage of these statements in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Control Statements

Section 1: Learn
Control statements help in decision making and controlling the flow of
execution in a Java program.

1. if Statement
Executes a block of code if the condition is true.
if (marks > 40) {
[Link]("Pass");
}

2. if-else Statement
Adds an alternative block if the condition is false.
if (marks >= 50) {
[Link]("First Class");
} else {
[Link]("Second Class");
}

3. Nested if-else
if statements inside another if.
if (marks >= 90) {
[Link]("Excellent");
} else if (marks >= 60) {
[Link]("Good");
} else {
[Link]("Needs Improvement");
}

4. switch-case Statement
An alternative to if-else when checking multiple values.
int day = 3;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
default:
[Link]("Another day");
}

• break: Exits the switch block.


• default: Runs if no match is found.

5. break Statement
Used to exit loops or switch early.
for (int i = 1; i <= 5; i++) {
if (i == 3)
break;
[Link](i);
}

Output: 1, 2
6. continue Statement
Skips current iteration, continues with next.
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue;
[Link](i);
}

Output: 1, 2, 4, 5
Did You Know?: In old programming languages like BASIC, only goto was
available. Java improved this with structured control statements.

Section 2: Practice

Try: Pass or Fail Checker


int marks = 45;
if (marks >= 35) {
[Link]("Pass");
} else {
[Link]("Fail");
}

Try: Grade Using Nested if-else


int marks = 85;
if (marks >= 90) {
[Link]("A+");
} else if (marks >= 75) {
[Link]("A");
} else {
[Link]("B or below");
}

Try: Switch Case


int option = 2;
switch (option) {
case 1:
[Link]("Add");
break;
case 2:
[Link]("Edit");
break;
default:
[Link]("Invalid Option");
}

Section 3: Know More (FAQs)

Can I use if inside a switch?


Yes, you can combine both, but use with care for readability.

What happens if I forget break in switch?


It causes fall-through, meaning all the next cases will also run.

Is continue used in switch?


No. continue is used inside loops, not inside switch.

Which is better: if-else or switch?


Use switch when checking one variable against many constant values. Use if-else
for complex conditions.
Can I use switch with String?
Yes, from Java 7 onwards, switch supports String values.

You might also like