0% found this document useful (0 votes)
5 views2 pages

Lesson 3 - Java Control Structures

The document provides an overview of Java control structures, which include decision-making structures, looping structures, and jump statements. It details various types of decision structures such as if statements, switch statements, and different looping mechanisms like for, while, and do...while loops. Additionally, it covers nested control structures and the use of jump statements like break and continue.

Uploaded by

denniskibe169
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)
5 views2 pages

Lesson 3 - Java Control Structures

The document provides an overview of Java control structures, which include decision-making structures, looping structures, and jump statements. It details various types of decision structures such as if statements, switch statements, and different looping mechanisms like for, while, and do...while loops. Additionally, it covers nested control structures and the use of jump statements like break and continue.

Uploaded by

denniskibe169
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 Structures

Control structures allow the flow of execution in a program to be directed depending on


conditions and repetitions. Java has three main types of control structures:
 Decision-Making Structures
 Looping Structures
 Jump Statements (e.g., break, continue)
1. Decision Structures
a) if Statement
int score = 80;
if (score > 70) {
[Link]("Passed!");
}
b) if...else Statement
int score = 60;
if (score >= 70) {
[Link]("Passed!");
} else {
[Link]("Failed.");
}
c) if...else if...else Chain
int score = 85;
if (score >= 90) {
[Link]("Grade A");
} else if (score >= 80) {
[Link]("Grade B");
} else {
[Link]("Grade C or below");
}
d) switch Statement
int day = 3;
switch(day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
default:
[Link]("Another day");
}

2. Looping Structures
a) for Loop
Used when the number of iterations is known.
for (int i = 1; i <= 5; i++) {
[Link]("Count: " + i);
}
b) while Loop
Used when the number of iterations is unknown but condition is checked before the loop
starts.
int i = 1;
while (i <= 5) {
[Link]("Count: " + i);
i++;
}

2
c) do...while Loop
Executes at least once. Condition is checked after the loop.
int i = 1;
do {
[Link]("Count: " + i);
i++;
} while (i <= 5);

3. Nested Control Structures


Loops and decision structures can be nested inside one another.
for (int i = 1; i <= 3; i++) {
if (i % 2 == 0) {
[Link](i + " is even");
} else {
[Link](i + " is odd");
}
}

4. Jump Statements
a) break
Terminates a loop or switch.
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
[Link](i);
}
b) continue
Skips the current iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
[Link](i);
}

You might also like