controlflowjava.md(file created)
@@ -0,0 +1,254 @@ | |||
1 | + | # Java Control Flow: If Statements and Loops | |
2 | + | ||
3 | + | ## If Statements | |
4 | + | ||
5 | + | ### Basic If Statement | |
6 | + | The simplest form of decision making: | |
7 | + | ||
8 | + | ```java | |
9 | + | public class BasicIf { | |
10 | + | public static void main(String[] args) { | |
11 | + | int temperature = 28; | |
12 | + | ||
13 | + | if (temperature > 25) { | |
14 | + | System.out.println("It's a hot day!"); | |
15 | + | } | |
16 | + | ||
17 | + | // Using multiple conditions | |
18 | + | if (temperature > 25 && temperature < 30) { | |
19 | + | System.out.println("Perfect weather for swimming!"); | |
20 | + | } | |
21 | + | } | |
22 | + | } | |
23 | + | ``` | |
24 | + | ||
25 | + | ### If-Else Statement | |
26 | + | When you need to handle two alternative scenarios: | |
27 | + | ||
28 | + | ```java | |
29 | + | public class IfElse { | |
30 | + | public static void main(String[] args) { | |
31 | + | int score = 75; | |
32 | + | ||
33 | + | if (score >= 60) { | |
34 | + | System.out.println("You passed!"); | |
35 | + | } else { | |
36 | + | System.out.println("You need to retake the test."); | |
37 | + | } | |
38 | + | ||
39 | + | // Using with String comparison | |
40 | + | String status = "Premium"; | |
41 | + | if (status.equals("Premium")) { | |
42 | + | System.out.println("Access granted to premium features"); | |
43 | + | } else { | |
44 | + | System.out.println("Upgrade to premium for additional features"); | |
45 | + | } | |
46 | + | } | |
47 | + | } | |
48 | + | ``` | |
49 | + | ||
50 | + | ### If-Else-If Ladder | |
51 | + | For multiple conditions: | |
52 | + | ||
53 | + | ```java | |
54 | + | public class IfElseIf { | |
55 | + | public static void main(String[] args) { | |
56 | + | int grade = 85; | |
57 | + | ||
58 | + | if (grade >= 90) { | |
59 | + | System.out.println("A - Excellent!"); | |
60 | + | } else if (grade >= 80) { | |
61 | + | System.out.println("B - Good job!"); | |
62 | + | } else if (grade >= 70) { | |
63 | + | System.out.println("C - Satisfactory"); | |
64 | + | } else if (grade >= 60) { | |
65 | + | System.out.println("D - Needs improvement"); | |
66 | + | } else { | |
67 | + | System.out.println("F - Failed"); | |
68 | + | } | |
69 | + | } | |
70 | + | } | |
71 | + | ``` | |
72 | + | ||
73 | + | ### Nested If Statements | |
74 | + | Conditions within conditions: | |
75 | + | ||
76 | + | ```java | |
77 | + | public class NestedIf { | |
78 | + | public static void main(String[] args) { | |
79 | + | boolean isLoggedIn = true; | |
80 | + | String userRole = "admin"; | |
81 | + | ||
82 | + | if (isLoggedIn) { | |
83 | + | System.out.println("Welcome back!"); | |
84 | + | ||
85 | + | if (userRole.equals("admin")) { | |
86 | + | System.out.println("You have admin privileges"); | |
87 | + | } else { | |
88 | + | System.out.println("You have user privileges"); | |
89 | + | } | |
90 | + | } else { | |
91 | + | System.out.println("Please log in first"); | |
92 | + | } | |
93 | + | } | |
94 | + | } | |
95 | + | ``` | |
96 | + | ||
97 | + | ## Loops | |
98 | + | ||
99 | + | ### For Loop | |
100 | + | Used when you know the number of iterations: | |
101 | + | ||
102 | + | ```java | |
103 | + | public class ForLoop { | |
104 | + | public static void main(String[] args) { | |
105 | + | // Basic for loop | |
106 | + | for (int i = 0; i < 5; i++) { | |
107 | + | System.out.println("Count: " + i); | |
108 | + | } | |
109 | + | ||
110 | + | // Loop with multiple variables | |
111 | + | for (int i = 0, j = 10; i < j; i++, j--) { | |
112 | + | System.out.println("i = " + i + ", j = " + j); | |
113 | + | } | |
114 | + | ||
115 | + | // For loop with array | |
116 | + | int[] numbers = {1, 2, 3, 4, 5}; | |
117 | + | for (int i = 0; i < numbers.length; i++) { | |
118 | + | System.out.println("Number: " + numbers[i]); | |
119 | + | } | |
120 | + | } | |
121 | + | } | |
122 | + | ``` | |
123 | + | ||
124 | + | ### Enhanced For Loop (For-Each) | |
125 | + | Simplified loop for collections and arrays: | |
126 | + | ||
127 | + | ```java | |
128 | + | public class EnhancedFor { | |
129 | + | public static void main(String[] args) { | |
130 | + | String[] fruits = {"Apple", "Banana", "Orange", "Mango"}; | |
131 | + | ||
132 | + | for (String fruit : fruits) { | |
133 | + | System.out.println("Fruit: " + fruit); | |
134 | + | } | |
135 | + | ||
136 | + | // With ArrayList | |
137 | + | ArrayList<Integer> numbers = new ArrayList<>(); | |
138 | + | numbers.add(1); | |
139 | + | numbers.add(2); | |
140 | + | numbers.add(3); | |
141 | + | ||
142 | + | for (Integer number : numbers) { | |
143 | + | System.out.println("Number: " + number); | |
144 | + | } | |
145 | + | } | |
146 | + | } | |
147 | + | ``` | |
148 | + | ||
149 | + | ### While Loop | |
150 | + | Used when the number of iterations is not known in advance: | |
151 | + | ||
152 | + | ```java | |
153 | + | public class WhileLoop { | |
154 | + | public static void main(String[] args) { | |
155 | + | int count = 0; | |
156 | + | ||
157 | + | while (count < 5) { | |
158 | + | System.out.println("Count: " + count); | |
159 | + | count++; | |
160 | + | } | |
161 | + | ||
162 | + | // Reading input example | |
163 | + | Scanner scanner = new Scanner(System.in); | |
164 | + | String input = ""; | |
165 | + | ||
166 | + | while (!input.equals("quit")) { | |
167 | + | System.out.println("Enter command (type 'quit' to exit):"); | |
168 | + | input = scanner.nextLine(); | |
169 | + | System.out.println("You entered: " + input); | |
170 | + | } | |
171 | + | } | |
172 | + | } | |
173 | + | ``` | |
174 | + | ||
175 | + | ### Do-While Loop | |
176 | + | Ensures the loop body executes at least once: | |
177 | + | ||
178 | + | ```java | |
179 | + | public class DoWhile { | |
180 | + | public static void main(String[] args) { | |
181 | + | int number = 1; | |
182 | + | ||
183 | + | do { | |
184 | + | System.out.println("Number: " + number); | |
185 | + | number *= 2; | |
186 | + | } while (number < 100); | |
187 | + | ||
188 | + | // Menu example | |
189 | + | Scanner scanner = new Scanner(System.in); | |
190 | + | int choice; | |
191 | + | ||
192 | + | do { | |
193 | + | System.out.println("\n1. View profile"); | |
194 | + | System.out.println("2. Edit settings"); | |
195 | + | System.out.println("3. Exit"); | |
196 | + | System.out.print("Enter your choice: "); | |
197 | + | ||
198 | + | choice = scanner.nextInt(); | |
199 | + | ||
200 | + | switch (choice) { | |
201 | + | case 1: | |
202 | + | System.out.println("Viewing profile..."); | |
203 | + | break; | |
204 | + | case 2: | |
205 | + | System.out.println("Editing settings..."); | |
206 | + | break; | |
207 | + | case 3: | |
208 | + | System.out.println("Goodbye!"); | |
209 | + | break; | |
210 | + | default: | |
211 | + | System.out.println("Invalid choice!"); | |
212 | + | } | |
213 | + | } while (choice != 3); | |
214 | + | } | |
215 | + | } | |
216 | + | ``` | |
217 | + | ||
218 | + | ### Control Statements in Loops | |
219 | + | Break and continue statements: | |
220 | + | ||
221 | + | ```java | |
222 | + | public class LoopControl { | |
223 | + | public static void main(String[] args) { | |
224 | + | // Break example | |
225 | + | for (int i = 0; i < 10; i++) { | |
226 | + | if (i == 5) { | |
227 | + | break; // Exit loop when i equals 5 | |
228 | + | } | |
229 | + | System.out.println("Count: " + i); | |
230 | + | } | |
231 | + | ||
232 | + | // Continue example | |
233 | + | for (int i = 0; i < 5; i++) { | |
234 | + | if (i == 2) { | |
235 | + | continue; // Skip iteration when i equals 2 | |
236 | + | } | |
237 | + | System.out.println("Number: " + i); | |
238 | + | } | |
239 | + | } | |
240 | + | } | |
241 | + | ``` | |
242 | + | ||
243 | + | Key points to remember: | |
244 | + | - If statements can be nested but try to avoid too many levels of nesting | |
245 | + | - Always use curly braces for clarity, even for single-line blocks | |
246 | + | - Choose the appropriate loop based on your needs: | |
247 | + | - For loop when you know the number of iterations | |
248 | + | - While loop when the iteration count is unknown | |
249 | + | - Do-while when you need at least one iteration | |
250 | + | - Use break to exit a loop early | |
251 | + | - Use continue to skip to the next iteration | |
252 | + | - Be careful with infinite loops - ensure your loop condition will eventually be false | |
253 | + | ||
254 | + | These control flow statements are fundamental to Java programming and are used extensively in real-world applications. |
更新
更早