WebNovels

Chapter 6 - Chapter 6 — The Endless Machine

The robot on the screen stood still.

The tiny ASCII robot blinked its square digital eyes.

[^_^]

/| |\

/ \

Below it, the computer displayed another message.

ROBOT MOVEMENT ENABLED

WARNING: LOOP SYSTEM UNSTABLE

REPEAT COMMAND REQUIRED

Pran leaned closer.

"Loop system unstable?"

The cursor blinked again.

_

The robot suddenly printed another message.

SYSTEM ERROR

REPEAT SEQUENCE UNTIL CORRECT VALUE ENTERED

Pran tilted his head.

"Repeat until correct value?"

That sounded familiar.

The for loop repeats a fixed number of times.

But this machine didn't know how many times it should repeat.

It needed to repeat until something happened.

That is exactly what a while loop does.

A while loop means:

Keep running this code while the condition is true.

The moment the condition becomes false, the loop stops.

Pran opened the code again.

He started typing slowly.

#include

int main() {

int number = 0;

while(number != 7) {

printf("Enter the secret number:\n");

scanf("%d", &number);

}

printf("Correct number!\n");

return 0;

}

He leaned back.

"That should work."

Let's break down the important line.

while(number != 7)

This means:

Continue the loop while the number is NOT equal to 7.

The symbol:

!=

means not equal to.

So the program keeps asking for a number until the user enters 7.

Pran ran the program.

The screen displayed:

Enter the secret number:

Pran typed:

3

The program asked again.

Enter the secret number:

He typed:

10

Again.

Enter the secret number:

Pran grinned.

"Alright…"

He typed:

7

The program printed:

Correct number!

Then something strange happened.

The computer screen flickered.

A new message appeared.

SECRET NUMBER VERIFIED

Pran blinked.

"Wait… that actually triggered something?"

Another message appeared.

ENDLESS MACHINE CONTROL ACTIVATED

Then the robot on the screen began to move slightly.

[^o^]

/| |\

/ \

Pran laughed.

"Okay… this computer is getting interesting."

The screen printed another message.

TEST MODE ACTIVATED

GUESS THE SECRET NUMBER

Pran leaned forward.

"A guessing game?"

He quickly modified the program.

#include

int main() {

int guess = 0;

printf("Guess the secret number!\n");

while(guess != 5) {

printf("Enter your guess:\n");

scanf("%d", &guess);

}

printf("You guessed correctly!\n");

return 0;

}

He ran it.

The screen displayed:

Guess the secret number!

Enter your guess:

Pran typed:

2

The program asked again.

Enter your guess:

He typed:

4

Still wrong.

Again.

Enter your guess:

Finally he typed:

5

The screen printed:

You guessed correctly!

Pran nodded.

"That's fun."

while loops are extremely powerful because they allow programs to keep running until something changes.

They are used for:

• games• menus• password systems• waiting for user input• repeating tasks

Basically, many programs would not work without them.

The computer screen flashed again.

Another message appeared.

ENDLESS MACHINE STABILIZED

The robot moved again.

[^_^]

/| |\

/ \

Then another system message appeared.

SYSTEM PROGRESS: 40%

Pran's eyes widened.

"Forty percent?"

The computer system was slowly unlocking.

Another message appeared.

NEXT MODULE: TOOL CREATION

Pran raised an eyebrow.

"Tool creation?"

The screen printed one final line.

REQUIREMENT: FUNCTION SYSTEM

Pran cracked his knuckles again.

"If we're going to build tools…"

"We're going to need functions."

Chapter 7 — The Tool Builder

This chapter introduces something that makes programs organized and powerful.

You will learn:

functions

reusable code

parameters

returning values

More Chapters