-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlFlow.java
More file actions
52 lines (48 loc) · 1.56 KB
/
ControlFlow.java
File metadata and controls
52 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// ControlFlow.java
// This file demonstrates control flow statements in Java, including if-else statements, switch cases, and loops.
public class ControlFlow {
public static void main(String[] args) {
// Example of if-else statement
int number = 10;
if (number > 0) {
System.out.println(number + " is positive.");
} else if (number < 0) {
System.out.println(number + " is negative.");
} else {
System.out.println("The number is zero.");
}
// Example of switch case
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Well done!");
break;
case 'C':
System.out.println("Good job!");
break;
case 'D':
System.out.println("You passed.");
break;
case 'F':
System.out.println("Better luck next time.");
break;
default:
System.out.println("Invalid grade.");
}
// Example of for loop
System.out.println("For loop example:");
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
// Example of while loop
System.out.println("While loop example:");
int count = 1;
while (count <= 5) {
System.out.println("Count is: " + count);
count++;
}
}
}