Repository navigation
Expand file tree
/
Copy pathControlStructuresDemo.java
More file actions
319 lines (282 loc) · 10.1 KB
/
Copy pathControlStructuresDemo.java
File metadata and controls
319 lines (282 loc) · 10.1 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package basics;
/**
* ControlStructuresDemo - Demonstrates Java control flow statements
*
* This class covers:
* - Conditional statements (if, if-else, switch)
* - Loops (for, while, do-while, enhanced for)
* - Jump statements (break, continue, return)
* - Nested structures and best practices
*
* @author Java Examples Project
* @version 1.0
*/
public class ControlStructuresDemo {
/**
* Demonstrates if-else conditional statements
*/
public static void demonstrateIfElse() {
System.out.println("=== IF-ELSE STATEMENTS ===");
int score = 85;
// Simple if statement
if (score >= 90) {
System.out.println("Excellent! Grade: A");
} else if (score >= 80) {
System.out.println("Good! Grade: B");
} else if (score >= 70) {
System.out.println("Average. Grade: C");
} else if (score >= 60) {
System.out.println("Below Average. Grade: D");
} else {
System.out.println("Failed. Grade: F");
}
// Ternary operator (conditional operator)
String result = (score >= 60) ? "Pass" : "Fail";
System.out.println("Result using ternary operator: " + result);
// Logical operators in conditions
int age = 25;
boolean hasLicense = true;
boolean hasInsurance = true;
if (age >= 18 && hasLicense && hasInsurance) {
System.out.println("Eligible to drive");
} else {
System.out.println("Not eligible to drive");
}
// Complex conditions
boolean isWeekend = false;
boolean isHoliday = true;
if ((isWeekend || isHoliday) && age >= 18) {
System.out.println("Can sleep in today!");
}
}
/**
* Demonstrates switch statements
*/
public static void demonstrateSwitch() {
System.out.println("\n=== SWITCH STATEMENTS ===");
// Traditional switch with break statements
int dayOfWeek = 3;
String dayName;
switch (dayOfWeek) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
}
System.out.println("Day " + dayOfWeek + " is: " + dayName);
// Switch with multiple cases
char grade = 'B';
switch (grade) {
case 'A':
case 'a':
System.out.println("Excellent work!");
break;
case 'B':
case 'b':
System.out.println("Good job!");
break;
case 'C':
case 'c':
System.out.println("You can do better!");
break;
case 'D':
case 'd':
System.out.println("Need improvement!");
break;
case 'F':
case 'f':
System.out.println("Please see instructor!");
break;
default:
System.out.println("Invalid grade!");
}
// Switch with strings (Java 7+)
String season = "spring";
switch (season.toLowerCase()) {
case "spring":
System.out.println("Time for planting!");
break;
case "summer":
System.out.println("Time for vacation!");
break;
case "fall":
case "autumn":
System.out.println("Time for harvest!");
break;
case "winter":
System.out.println("Time for hot cocoa!");
break;
default:
System.out.println("Unknown season!");
}
}
/**
* Demonstrates different types of loops
*/
public static void demonstrateLoops() {
System.out.println("\n=== LOOP STATEMENTS ===");
// For loop
System.out.println("For loop - counting from 1 to 5:");
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
System.out.println();
// While loop
System.out.println("\nWhile loop - countdown from 5:");
int count = 5;
while (count > 0) {
System.out.print(count + " ");
count--;
}
System.out.println("Blast off!");
// Do-while loop (executes at least once)
System.out.println("\nDo-while loop - user input simulation:");
int userInput = 0;
int attempts = 0;
do {
attempts++;
userInput = (int) (Math.random() * 10) + 1; // Simulate random input 1-10
System.out.println("Attempt " + attempts + ": Generated number = " + userInput);
} while (userInput != 7 && attempts < 5);
if (userInput == 7) {
System.out.println("Success! Found the lucky number 7!");
} else {
System.out.println("Max attempts reached.");
}
// Enhanced for loop (for-each)
System.out.println("\nEnhanced for loop - array iteration:");
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
System.out.print(number + " ");
}
System.out.println();
// Nested loops
System.out.println("\nNested loops - multiplication table:");
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.printf("%d x %d = %d\t", i, j, i * j);
}
System.out.println();
}
}
/**
* Demonstrates jump statements (break, continue, return)
*/
public static void demonstrateJumpStatements() {
System.out.println("\n=== JUMP STATEMENTS ===");
// Break statement
System.out.println("Break statement - find first even number:");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
System.out.println("First even number found: " + i);
break; // Exit the loop
}
System.out.println("Checking: " + i);
}
// Continue statement
System.out.println("\nContinue statement - print only odd numbers:");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip the rest of this iteration
}
System.out.print(i + " ");
}
System.out.println();
// Labeled break and continue (for nested loops)
System.out.println("\nLabeled break - exit nested loops:");
outer: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
System.out.println("Breaking out of both loops at i=" + i + ", j=" + j);
break outer; // Break out of the outer loop
}
System.out.println("i=" + i + ", j=" + j);
}
}
}
/**
* Demonstrates practical examples combining control structures
*/
public static void demonstratePracticalExamples() {
System.out.println("\n=== PRACTICAL EXAMPLES ===");
// Example 1: Number guessing game logic
System.out.println("Number guessing game simulation:");
int secretNumber = 7;
int[] guesses = {3, 5, 7, 9};
for (int i = 0; i < guesses.length; i++) {
int guess = guesses[i];
System.out.print("Guess " + (i + 1) + ": " + guess + " - ");
if (guess == secretNumber) {
System.out.println("Correct! You win!");
break;
} else if (guess < secretNumber) {
System.out.println("Too low!");
} else {
System.out.println("Too high!");
}
}
// Example 2: Grade calculator
System.out.println("\nGrade calculator:");
int[] scores = {95, 87, 76, 64, 58};
int total = 0;
int count = 0;
for (int score : scores) {
total += score;
count++;
// Determine letter grade
char letterGrade;
if (score >= 90) {
letterGrade = 'A';
} else if (score >= 80) {
letterGrade = 'B';
} else if (score >= 70) {
letterGrade = 'C';
} else if (score >= 60) {
letterGrade = 'D';
} else {
letterGrade = 'F';
}
System.out.println("Score: " + score + " -> Grade: " + letterGrade);
}
double average = (double) total / count;
System.out.printf("Average: %.2f\n", average);
}
/**
* Main method demonstrating all control structure concepts
* @param args command line arguments
*/
public static void main(String[] args) {
System.out.println("Java Control Structures Demonstration");
System.out.println("====================================");
demonstrateIfElse();
demonstrateSwitch();
demonstrateLoops();
demonstrateJumpStatements();
demonstratePracticalExamples();
System.out.println("\n=== SUMMARY ===");
System.out.println("Control structures allow you to:");
System.out.println("1. Make decisions (if-else, switch)");
System.out.println("2. Repeat actions (for, while, do-while)");
System.out.println("3. Control flow (break, continue, return)");
System.out.println("4. Create complex logic by combining them");
}
}