Go up now, look toward the sea. And he went up, and looked, and said, There is nothing. And he said, Go again seven times. And it came to pass at the seventh time, that he said, Behold, there ariseth a little cloud out of the sea, like a man’s hand. — 1 Kings 18:43–44 (KJV)
After three and a half years without rain, Elijah prayed on the top of Mount Carmel, bowed down with his face between his knees. He sent his servant to look toward the sea. Nothing. “Go again.” The servant didn’t know how many trips it would take; he was to keep going until something changed. On the seventh time, a little cloud — and soon, a great rain.
That’s the difference between the two loops:
| You know… | Use | |
|---|---|---|
| Jericho: six days, then seven laps | exactly how many times | a for loop |
| Carmel: until a cloud appears | only when to stop | a while loop |
The while loop
sky = "nothing"
while sky != "a little cloud":
sky = input("Look toward the sea: ")
print("Behold, a little cloud!")
Look toward the sea: nothing
Look toward the sea: nothing
…
Look toward the sea: a little cloud
Behold, a little cloud!
How it runs:
skystarts as"nothing".- Python checks the condition: is
skynot equal (!=) to"a little cloud"? It’sTrue, so it runs the indented line: it asks again, and whatever you type becomes the newsky. - Back to the top: check again. As long as the answer is
True, it keeps going. - When you type
a little cloud, the check isFalse, and Python continues after the loop.
The check always happens before each pass. If the condition is False at the very start, the body never runs at
all.
The infinite loop
sky = "nothing"
while sky != "a little cloud":
print("Look toward the sea") # nothing here changes sky!
Nothing inside this loop ever changes sky, so the condition is True forever and the program never stops. That’s an
infinite loop. (If you run one by accident, press Ctrl + C to stop it.)
The rule: something inside a while loop must move it toward the end — a new answer, a counter going up, a value getting smaller.
The same idea in JavaScript
let sky = "nothing";
while (sky !== "a little cloud") {
sky = prompt("Look toward the sea:");
}
console.log("Behold, a little cloud!");
It’s everywhere
| Loop | What it does |
|---|---|
while game_running: |
draw the next frame of a game |
while not done: |
download the next piece of a file |
while typing: |
keep showing suggestions |
Common mistakes
- Forgetting to change the condition inside the loop → it never ends.
- Changing it in the wrong direction: counting up while waiting for a number to drop to 0.
- Using
whilewhen you know the count. “Do this 10 times” is clearer asfor i in range(10):. - Mixing
=and!=.!=asks “is it different?”;=changes a value.
Try it
-
Count down from 10 to 1 with a while loop, then print
"Liftoff!". -
Start with
x = 1and keep doubling it while it’s less than 1000. How many doublings does it take?Answer
x, steps = 1, 0 while x < 1000: x = x * 2 steps = steps + 1 print(x, steps) # 1024 10 -
Change Elijah’s program to count the looks and print
"Cloud after 7 looks"at the end.
Reflect
The servant was sent back seven times to the same place to see the same empty sky. Elijah didn’t ask him to guess when the answer would come — only to keep looking until it did. Persistent prayer often feels exactly like that loop: the same check, again, until the condition changes.