– Looping [ while ]

If you want to make your computer or robot do something over and over, you use looping functions.

The “while” loop will make your computer repeat a task as long as a certain condition remains true.

For instance, we could think of the game “Red-Light/Green-Light” –

The runners have to stay still until the person at the end of the line says, “Green-Light,” so while the light is green, the runners can run.

Assuming two tasks: ‘runners_run’ and ‘runners_stop’, we could write the “while” part kind of like this:

while (Green_Light)





This could be called “pseudo code”, where each task, or function would be further clarified with code statements, such as a call to output a signal to ‘go’ or ‘stop’.

Everything within the {} will loop – or repeat over and over while the light is green. And when the light is no longer green, the program executes the next step.

NEXT: Finite Loops


1 thought on “– Looping [ while ]

  1. Terry Grant

    Another common use example is to wait – for a user input or an external event, like “wait for the starting light”:
    while (red_light) {runners_stay();}

    or “wait for start button”:
    printf(“Press Start!”);
    while(start_button()==0) {} //do nothing but wait


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s