The computer screen dimmed for a moment.
Then a new message appeared.
NEXT MODULE: COUNTING SYSTEM
INITIALIZING ROBOT UNIT
Pran leaned closer.
"A robot?"
The screen flickered again.
Then a small diagram appeared.
[ ROBOT CONTROL SYSTEM ]
STATUS: OFFLINE
ACTIVATION REQUIREMENT: COUNT SEQUENCE
Pran laughed.
"So the robot won't move unless we count?"
That sounded simple.
But computers are strange creatures.
They will not repeat tasks unless you tell them exactly how.
If Pran wanted the computer to count like this:
1
2
3
4
5
He could write:
printf("1\n");
printf("2\n");
printf("3\n");
printf("4\n");
printf("5\n");
But that would be ridiculous.
Imagine needing to count to 1000.
You would have to write 1000 print statements.
Programmers would lose their minds.
Fortunately, programming languages have something magical.
Something called a loop.
A loop means:
Repeat an action multiple times.
And one of the most common loops in C is the for loop.
Pran opened his program again.
He began typing.
#include
int main() {
for(int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
He stared at the screen.
"That looks… confusing."
But it's actually very simple.
Let's break it down.
The loop starts with this line:
for(int i = 1; i <= 5; i++)
Inside the parentheses are three parts.
Part 1 — Start value
int i = 1
This creates a variable called i and sets it to 1.
You can think of i as a counter.
Part 2 — Condition
i <= 5
This means:
Continue the loop as long as i is less than or equal to 5.
Part 3 — Update
i++
This means:
Increase i by 1 every time the loop runs.
So the loop works like this:
Step 1
i = 1
Print 1.
Step 2
i = 2
Print 2.
Step 3
i = 3
Print 3.
Step 4
i = 4
Print 4.
Step 5
i = 5
Print 5.
Then the loop stops.
Pran pressed Run.
The output appeared instantly.
1
2
3
4
5
Pran nodded slowly.
"That's actually pretty cool."
Instead of writing five print statements, the loop handled everything automatically.
He imagined the loop like a small robot counting out loud.
Robot voice:
1
2
3
4
5
Then the robot stopped.
Satisfied.
Pran looked back at the computer message.
ACTIVATION REQUIREMENT: COUNT SEQUENCE
He smiled.
"Alright robot," he said.
"Let's activate you."
He modified the program slightly.
#include
int main() {
printf("Activating robot...\n");
for(int i = 1; i <= 5; i++) {
printf("Counting: %d\n", i);
}
printf("Robot activated!\n");
return 0;
}
He ran the program.
The screen printed:
Activating robot...
Counting: 1
Counting: 2
Counting: 3
Counting: 4
Counting: 5
Robot activated!
Then something strange happened.
The computer screen flickered again.
Another message appeared.
ROBOT UNIT ONLINE
Then a little ASCII robot appeared.
[^_^]
/| |\
/ \
Pran laughed.
"You've got to be kidding me."
Another message appeared.
ROBOT READY FOR COMMANDS
The cursor blinked again.
_
Pran leaned forward.
"What if I make the robot count to 10?"
He changed one number.
i <= 10
The program became:
#include
int main() {
printf("Robot counting...\n");
for(int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
printf("Counting complete\n");
return 0;
}
He ran it again.
The output appeared.
Robot counting...
1
2
3
4
5
6
7
8
9
10
Counting complete
The robot diagram moved slightly on the screen.
[^_^]
/| |\
/ \
Another message appeared.
ROBOT MOVEMENT ENABLED
Pran raised an eyebrow.
"Movement?"
Then another message appeared.
WARNING: LOOP SYSTEM UNSTABLE
Pran frowned.
"Unstable?"
Then suddenly the screen printed something unexpected.
REPEAT COMMAND REQUIRED
The cursor blinked again.
_
Pran realized something.
The for loop repeats a fixed number of times.
But sometimes you don't know how many times something should repeat.
Sometimes you want something to repeat until a condition changes.
For that…
You need another type of loop.
Pran cracked his knuckles.
"If this computer wants to repeat commands…"
"We're going to need a while loop."
Chapter 6 — The Endless Machine
This chapter introduces another powerful tool.
You will learn:
while loops
repeating until a condition changes
building a number guessing game
