-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path03_break_continue.py
More file actions
286 lines (223 loc) · 8.11 KB
/
03_break_continue.py
File metadata and controls
286 lines (223 loc) · 8.11 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
"""
================================================================================
File: 03_break_continue.py
Topic: Loop Control Statements - break, continue, pass
================================================================================
This file demonstrates loop control statements that alter the normal flow
of loop execution. These are essential tools for writing efficient and
readable loops.
Key Concepts:
- break: Exit the loop immediately
- continue: Skip to the next iteration
- pass: Do nothing (placeholder)
- Practical use cases for each
================================================================================
"""
# -----------------------------------------------------------------------------
# 1. The break Statement
# -----------------------------------------------------------------------------
# Immediately exits the loop, skipping any remaining iterations
print("--- The break Statement ---")
# Example 1: Exit when target is found
print("Finding first even number:")
numbers = [1, 3, 5, 8, 9, 10]
for num in numbers:
print(f" Checking {num}...", end=" ")
if num % 2 == 0:
print(f"Found! {num} is even.")
break
print("Odd, continuing...")
# Example 2: Exit on specific condition
print("\nSearching for name 'Charlie':")
names = ["Alice", "Bob", "Charlie", "David", "Eve"]
for name in names:
if name == "Charlie":
print(f" ✓ Found '{name}'!")
break
print(f" Checking '{name}'...")
# -----------------------------------------------------------------------------
# 2. The continue Statement
# -----------------------------------------------------------------------------
# Skips the rest of the current iteration and moves to the next
print("\n--- The continue Statement ---")
# Example 1: Skip negative numbers
print("Processing only positive numbers:")
values = [5, -2, 8, -1, 10, -3, 7]
for value in values:
if value < 0:
print(f" Skipping {value} (negative)")
continue
print(f" Processing {value}")
# Example 2: Skip specific items
print("\nPrinting all fruits except banana:")
fruits = ["apple", "banana", "cherry", "banana", "date"]
for fruit in fruits:
if fruit == "banana":
continue
print(f" {fruit}")
# -----------------------------------------------------------------------------
# 3. The pass Statement
# -----------------------------------------------------------------------------
# Does nothing - used as a placeholder
print("\n--- The pass Statement ---")
# Example 1: Placeholder in empty function/class
class FutureFeature:
pass # Will implement later
def not_implemented_yet():
pass # Placeholder for future code
# Example 2: Explicit "do nothing" in conditionals
print("Processing numbers (ignoring zeros for now):")
numbers = [1, 0, 2, 0, 3]
for num in numbers:
if num == 0:
pass # Explicitly do nothing (could be a TODO)
else:
print(f" Number: {num}")
# -----------------------------------------------------------------------------
# 4. break vs continue - Side by Side Comparison
# -----------------------------------------------------------------------------
print("\n--- break vs continue Comparison ---")
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# With break - stops at 5
print("With break (stop at 5):")
for num in data:
if num == 5:
break
print(num, end=" ")
print()
# With continue - skips 5
print("With continue (skip 5):")
for num in data:
if num == 5:
continue
print(num, end=" ")
print()
# -----------------------------------------------------------------------------
# 5. break with while Loops
# -----------------------------------------------------------------------------
print("\n--- break with while Loops ---")
# Breaking out of an infinite loop
print("Processing until 'quit' command:")
commands = ["start", "process", "load", "quit", "save"]
index = 0
while True:
command = commands[index]
print(f" Command: {command}")
if command == "quit":
print(" ↳ Exiting loop...")
break
index += 1
# -----------------------------------------------------------------------------
# 6. continue with while Loops
# -----------------------------------------------------------------------------
print("\n--- continue with while Loops ---")
# Skip multiples of 3
print("Numbers 1-10 (skipping multiples of 3):")
i = 0
while i < 10:
i += 1
if i % 3 == 0:
continue
print(i, end=" ")
print()
# -----------------------------------------------------------------------------
# 7. Nested Loops with break
# -----------------------------------------------------------------------------
# break only exits the innermost loop
print("\n--- Nested Loops with break ---")
print("Breaking inner loop only:")
for i in range(1, 4):
print(f" Outer loop: i = {i}")
for j in range(1, 4):
if j == 2:
print(" ↳ Breaking inner loop at j = 2")
break
print(f" Inner loop: j = {j}")
# Using a flag to break outer loop
print("\nBreaking outer loop with flag:")
stop_outer = False
for i in range(1, 4):
if stop_outer:
break
for j in range(1, 4):
print(f" i={i}, j={j}")
if i == 2 and j == 2:
print(" ↳ Breaking both loops!")
stop_outer = True
break
# -----------------------------------------------------------------------------
# 8. for-else with break
# -----------------------------------------------------------------------------
# else block runs only if loop completes without break
print("\n--- for-else with break ---")
# Example: Searching for a prime number
def is_prime(n):
"""Check if n is prime."""
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False # Found a divisor, not prime
return True # No divisors found, is prime
# Search for first prime in list
numbers = [4, 6, 8, 9, 11, 12]
print(f"Searching for prime in {numbers}:")
for num in numbers:
if is_prime(num):
print(f" → First prime found: {num}")
break
else:
print(" → No primes found in the list")
# -----------------------------------------------------------------------------
# 9. Practical Example: Input Validation
# -----------------------------------------------------------------------------
print("\n--- Practical Example: Input Validation ---")
# Simulating user input validation
test_inputs = ["abc", "-5", "150", "42"]
print("Validating inputs (must be 1-100):")
for input_str in test_inputs:
print(f"\n Input: '{input_str}'")
# Check if it's a number
if not input_str.lstrip('-').isdigit():
print(" ✗ Error: Not a valid number")
continue
value = int(input_str)
# Check range
if value < 1:
print(" ✗ Error: Too low (must be >= 1)")
continue
if value > 100:
print(" ✗ Error: Too high (must be <= 100)")
continue
print(f" ✓ Valid input: {value}")
break
else:
print("\n No valid input found!")
# -----------------------------------------------------------------------------
# 10. Practical Example: Skip Processing on Error
# -----------------------------------------------------------------------------
print("\n--- Practical Example: Error Handling ---")
# Processing a list of files (simulated)
files = [
{"name": "data1.csv", "readable": True},
{"name": "data2.csv", "readable": False}, # Error
{"name": "data3.csv", "readable": True},
{"name": "corrupt.csv", "readable": True, "corrupt": True}, # Error
{"name": "data4.csv", "readable": True},
]
print("Processing files:")
processed_count = 0
for file in files:
name = file["name"]
# Skip unreadable files
if not file.get("readable", False):
print(f" ✗ {name}: Cannot read file, skipping...")
continue
# Skip corrupt files
if file.get("corrupt", False):
print(f" ✗ {name}: File is corrupt, skipping...")
continue
# Process the file
print(f" ✓ {name}: Processing complete")
processed_count += 1
print(f"\nTotal files processed: {processed_count}/{len(files)}")