🎯 Conditionals
Boolean Values and Operators
Boolean values in Python are True and False. Boolean operators include and, or, and not.
Conditional (if)
Executes a block of code if a condition is true.
Alternative (if-else)
Executes one block if true, another if false.
Chained Conditional (if-elif-else)
Allows multiple conditions to be checked in sequence.
🔄 Iteration
While Loop
Executes as long as a condition is true.
For Loop
Iterates over a sequence (list, tuple, string).
Break Statement
Exits a loop immediately.
Continue Statement
Skips the rest of the code and jumps to the next iteration.
🎮 Interactive Demo: Control Flow Practice
Try different control flow patterns:
Conditional Demo
Loop Demo
🧩 Source Code: Control Flow Practice
HTML for the demo UI:
<div class="interactive-demo">
<h3>Try different control flow patterns:</h3>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
<div>
<h4>Conditional Demo</h4>
<input type="text" class="demo-input" id="switchName" placeholder="Enter switch name (SW101, SW102, SW110)">
<button class="demo-button" onclick="checkSwitch()">Check Switch</button>
<div class="output" id="conditionalOutput">Enter a switch name to check its status!</div>
</div>
<div>
<h4>Loop Demo</h4>
<input type="number" class="demo-input" id="loopCount" placeholder="Enter number of iterations (1-10)" min="1" max="10">
<button class="demo-button secondary" onclick="runLoop()">Run Loop</button>
<div class="output" id="loopOutput">Enter a number to see the loop in action!</div>
</div>
</div>
</div>JavaScript for the demo logic:
function checkSwitch() {
const switchName = document.getElementById('switchName').value;
const output = document.getElementById('conditionalOutput');
if (switchName === '') {
output.textContent = 'Please enter a switch name!';
return;
}
const activeSwitches = ['SW101', 'SW102', 'SW110'];
if (activeSwitches.includes(switchName)) {
output.textContent = `Switch: ${switchName}\nStatus: Active\nPorts: 48\nUptime: 99.9%`;
} else {
output.textContent = `Switch: ${switchName}\nStatus: Not Found\nError: Switch does not exist in topology`;
}
}
function runLoop() {
const count = parseInt(document.getElementById('loopCount').value);
const output = document.getElementById('loopOutput');
if (isNaN(count) || count < 1 || count > 10) {
output.textContent = 'Please enter a valid number between 1 and 10!';
return;
}
let result = `Running loop for ${count} iterations:\n\n`;
for (let i = 0; i < count; i++) {
result += `Iteration ${i + 1}: Checking switches...\n`;
result += ` - SW101: Active\n`;
result += ` - SW102: Active\n`;
result += ` - SW110: Active\n\n`;
}
result += 'Loop completed!';
output.textContent = result;
}🌐 Network Topology Examples
Let's consider a network topology where we need to perform various tasks using control flow statements and loops.
Example Topology
Complete Network Management Script
📊 Control Flow Summary
Conditional Statements:
- if: Execute code when condition is true
- if-else: Choose between two code paths
- if-elif-else: Choose between multiple code paths
Loop Statements:
- while: Repeat while condition is true
- for: Iterate over a sequence
- break: Exit loop immediately
- continue: Skip to next iteration
Best Practices:
- Use meaningful variable names
- Keep conditions simple and readable
- Avoid infinite loops
- Use break and continue judiciously