Concept Flow - Infinite loops
Start
Check condition
Condition true?
No→Exit loop
Yes
Execute loop body
Repeat check condition
An infinite loop keeps running because its condition never becomes false, so it never exits.
while true; do echo "Looping forever" done
| Step | Condition | Result | Action | Output |
|---|---|---|---|---|
| 1 | true | true | Print message | Looping forever |
| 2 | true | true | Print message | Looping forever |
| 3 | true | true | Print message | Looping forever |
| ... | ... | ... | ... | ... |
| n | true | true | Print message | Looping forever |
| Variable | Start | After 1 | After 2 | After 3 | ...final |
|---|---|---|---|---|---|
| condition | true | true | true | true | true |
Infinite loops run endlessly because their condition never becomes false. In bash, 'while true; do ... done' creates an infinite loop. No variable changes to stop the loop. Use Ctrl+C to stop infinite loops manually. Be careful to avoid infinite loops in scripts.
while true runs the loop forever because the condition is always true.while true; do echo Hello; done uses while true correctly. for i in 1..10; do echo $i; done loops 10 times, not infinite. if true; then echo Loop; fi is a conditional, not a loop. while false; do echo No; done never runs because condition is false.for (( ; ; )) creates an infinite loop because it has no start, condition, or increment limits.for (( ; ; )); do echo Loop; done matches infinite loop syntax. for i in {1..5}; do echo $i; done loops 5 times. for i in ; do echo $i; done is invalid syntax. for (( i=0; i<5; i++ )); do echo $i; done loops 5 times with increment.count=1
while :; do
echo $count
((count++))
if [ $count -gt 3 ]; then
break
fi
donewhile : which is an infinite loop, but it has a break condition inside.while true echo "Running" done
do after the while condition to start the loop body.while true but no do before echo. This causes a syntax error.stop.txt exists. Which script correctly implements this?while true; do
if [ -f stop.txt ]; then
break
fi
echo "Waiting..."
sleep 2
done uses while true with a break if stop.txt exists, which stops the loop correctly.stop.txt first and breaks if the file exists, otherwise prints "Waiting..." and sleeps 2 seconds. This matches the requirement.Pass the test and this pattern moves unknown