Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Propeller C – Start Simple

Propeller C – Start Simple

Code That Repeats

Sometimes we need to be able to program our Propeller to repeat certain operations.  This is useful for simple things, such as blinking a light, and more complex tasks, such as robotic sensor navigation.

Blocks of repeating code are called loops.  The C while loop can be set up to repeat a code block while some condition is true.  A while loop can also be set up to repeat endlessly, but there is a trick for escaping endless loops as well.  Let’s try all of these.

  • Click the Open Project button.
  • If you aren’t already there, navigate to …\SimpleIDE\Learn\Examples\C Intro\Basics.
  • Open Repeat While.side.
  • Set the power switch to position 1 if it isn’t already (if applicable for your board).
  • Click the Run with Terminal and verify that the program just keeps on counting (up to 100).
/*
  Repeat While.c
 
  Keep displaying n = n + 5 every fifth of a second while n < 100.
*/

#include "simpletools.h"                      // Include simpletools

int main()                                    // main function
{
  int n = 0;                                  // Declare n, initialize to zero
  while(n < 100)                              // Repeat while n less than 100
  {
    print("n = %d\n", n);                     // Display name & value of n
    n = n + 5;                                // Add 5 to n each time through
    pause(200);                               // 0.2 s between repetitions
  }
  print("All done!");                         // Display all done
}

 

How it Works

Inside main, a variable n is declared and initialized to zero.  Then, while (n < 100) starts a loop that will keep repeating the code block below it as long as (n < 100) is true. 

Each time through the loop, n = n +5 increases the value of n and then a print displays the new value in the SimpleIDE terminal. 

Eventually, n = 100 so (n < 100) is no longer true.  At that point, the code execution skips to the first instruction below the while loop’s code block.  In this case, it’s print(“All done!”);. 

 


Did You Know?

Keep in mind that conditions like a < b and n < 100 evaluate to 1 if they are true or 0 if they are false.  Like if statements, while statements execute their code block when the condition is 1, or really any value other than zero.


 

Try This – Endless Loop

Think about the Did You Know section for a minute. Can you guess how to make a while loop repeat forever? If you change while(n < 100) to while(1), you’ll have an endless loop.

  • Click the Save Project As button and save your project as Repeat Endless.side. 
  • Change while(n < 100) to while(1).
  • Run the modified program and verify that it just keeps going, and going, and going…

 

Your Turn – Break Out of an Endless Loop

Even an endless loop doesn’t have to be endless thanks to the C language’s break keyword.

  • Add if(n == 50) break; inside the while loop’s code block.
  • Test and verify that the that the program breaks out of the loop when n reaches 50. 

What do you think would happen if you move the break statement to just underneath n = n + 5 inside the while loop’s code block? Try it!

 


Printer-friendly version
Make Complicated Decisions
Prev
Counting Loops
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress