0% found this document useful (0 votes)
3 views6 pages

Java Control Statements

The document outlines control statements in programming, categorized into decision-making, looping, and jump statements. It provides syntax, usage scenarios, and real-life examples for each type, including if, if-else, switch, for, while, and do-while statements. Additionally, it explains break and continue jump statements, along with a summary table comparing their characteristics.

Uploaded by

mirzapuramrakesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Java Control Statements

The document outlines control statements in programming, categorized into decision-making, looping, and jump statements. It provides syntax, usage scenarios, and real-life examples for each type, including if, if-else, switch, for, while, and do-while statements. Additionally, it explains break and continue jump statements, along with a summary table comparing their characteristics.

Uploaded by

mirzapuramrakesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

🎯 Control Statements

Control statements are instructions that control the flow of execution in a program. provides control flow
statements in three main categories:

🚥 1. Decision-Making Statements

🔹 if Statement

✅ Syntax:

if (condition) {

// block of code to execute if condition is true

💡 Explanation:

 The simplest form of decision-making.

 Executes a block only if the given condition is true.

🧠 When to Use:

 When you want to do something based on a single condition.

🌍 Real-Life Example:

If the door is locked, use the key.

🔧 Example:

int age = 20;

if (age >= 18) {

System.out.println("You can vote.");

🔹 if-else Statement

✅ Syntax:

if (condition) {

// code if true

} else {

// code if false

🧠 When to Use:
 When there are two possibilities, and you want one to execute based on the condition.

🌍 Real-Life Example:

If it’s raining, carry an umbrella; else, wear sunglasses.

🔧 Example:

int marks = 40;

if (marks >= 35) {

System.out.println("Pass");

} else {

System.out.println("Fail");

🔹 if-else if Ladder

✅ Syntax:

if (condition1) {

// block 1

} else if (condition2) {

// block 2

} else {

// default block

🧠 When to Use:

 When there are multiple conditions and only one block needs to execute.

🌍 Real-Life Example:

If it’s morning, drink coffee; if it’s afternoon, have lunch; else, go to bed.

🔧 Example:

int score = 85;

if (score >= 90) {

System.out.println("Grade A");

} else if (score >= 80) {

System.out.println("Grade B");

} else {

System.out.println("Grade C");
}

🔹 switch Statement

✅ Syntax:

switch (expression) {

case value1:

// code block

break;

case value2:

// code block

break;

default:

// default block

🧠 When to Use:

 When you have to compare a single variable against multiple constant values.

 Works better than if-else-if when comparing equals values (like menu options, days, grades).

🌍 Real-Life Example:

Choose your drink: 1 – Tea, 2 – Coffee, 3 – Juice

🔧 Example:

int choice = 2;

switch (choice) {

case 1: System.out.println("Tea"); break;

case 2: System.out.println("Coffee"); break;

case 3: System.out.println("Juice"); break;

default: System.out.println("Invalid choice");

🔁 2. Looping Statements

Used to repeat a block of code multiple times.


🔹 for Loop

✅ Syntax:

for (initialization; condition; update) {

// block to execute

🧠 When to Use:

 When the number of iterations is known.

 Most commonly used loop.

🌍 Real-Life Example:

Print numbers from 1 to 10.

🔧 Example:

for (int i = 1; i <= 10; i++) {

System.out.println("i = " + i);

🔹 while Loop

✅ Syntax:

while (condition) {

// code to execute

🧠 When to Use:

 When the number of iterations is unknown but depends on a condition.

🌍 Real-Life Example:

Keep walking until you reach your destination.

🔧 Example:

int i = 1;

while (i <= 5) {

System.out.println("i = " + i);

i++;

🔹 do-while Loop
✅ Syntax:

do {

// code to execute

} while (condition);

🧠 When to Use:

 When the loop must execute at least once, regardless of the condition.

🌍 Real-Life Example:

Try the dish first, then decide if you want more.

🔧 Example:

int i = 1;

do {

System.out.println("i = " + i);

i++;

} while (i <= 5);

⛔ 3. Jump Statements

🔹 break

 Used to exit from a loop or switch early.

for (int i = 1; i <= 5; i++) {

if (i == 3) break;

System.out.println(i);

🔹 continue

 Skips the current iteration.

for (int i = 1; i <= 5; i++) {

if (i == 3) continue;

System.out.println(i);

📝 Summary Table
Statement Use When… Executes At Least Once Known Iterations

if One condition No N/A

if-else Two outcomes No N/A

if-else-if Multiple outcomes No N/A

switch Comparing single variable against values No N/A

for Fixed loop No Yes

while Condition-controlled loop No Maybe

do-while Condition-controlled loop with 1st run Yes Maybe

You might also like