-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchingIterationDemo.java
More file actions
47 lines (42 loc) · 1.53 KB
/
BranchingIterationDemo.java
File metadata and controls
47 lines (42 loc) · 1.53 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
public class BranchingIterationDemo {
public static void main(String[] args) {
int number = 7;
String role = "admin";
// if / else if / else: choose one branch based on boolean conditions
if (number < 0) {
System.out.println("number is negative");
} else if (number == 0) {
System.out.println("number is zero");
} else {
System.out.println("number is positive");
}
// switch: select a branch based on a single expression's value
switch (role) {
case "admin":
System.out.println("Welcome, admin");
break; // prevents falling through to the next case
case "editor":
System.out.println("Welcome, editor");
break;
case "viewer":
System.out.println("Welcome, viewer");
break;
default:
System.out.println("Role not recognized");
}
// for loop: run a known number of iterations
for (int i = 0; i < 3; i++) {
System.out.println("for loop i=" + i);
}
// while loop: run until the condition becomes false
int count = 2;
while (count > 0) {
System.out.println("while loop count=" + count);
count--; // ensure the loop eventually ends
}
String a[] = {"1", "2", "c", "d", "5"};
for (String i : a) {
System.out.println("Enhanced for loop value=" + i);
}
}
}