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

Counting Loops

A for loop repeats a block of code, but it’s more configurable than a while loop. A for loop is also the preferred tool for counting tasks.  while loops are better for repeating something “while” a condition is true.  In contrast, for loops are better for repeating a block a certain number of times.  This next program counts to ten with a for loop.

  • Click SimpleIDE’s Open Project button.
  • If you’re not already there, navigate to …\SimpleIDE\Learn\Examples\C Intro\Basics.
  • Open Count to Ten.side.
  • Set the power switch to position 1 if it isn’t already (if applicable for your board).
  • Click the Run with Terminal button, and verify that the program counts to ten.
/*
  Count to Ten.c
 
  Count to ten in SimpleIDE Terminal.
*/

#include "simpletools.h"                      // Include simpletools

int main()                                    // main function
{
  for(int n = 1; n <= 10; n++)                // Count to ten
  {
    print("n = %d\n", n);                     // Display name & value of n
    pause(500);                               // 0.5 s between reps
  }
  print("All done!");                         // Display all done
}

 

How it Works

The for statement has three parameters: for(start index; condition; increment).    

  • start index – typically a variable that is assigned an initial value where the counting starts
  • condition – a condition that has to be true for the loop to repeat
  • increment – an operation that should be performed on the index variable with each loop repetition

In the Count to Ten program, the start index is int n = 1, a variable declaration that initializes the variable n to the value 1.  The condition is n <= 10, which means that the loop will keep repeating while n is less than or equal to ten.  The increment is n++, which is a shorthand form of n = n + 1.  So with each repetition of the for loop’s code block, the value of n increases by 1.

 


Did You Know?

Increment operators — The ++ operator is called an increment operator.  Although increment and decrement operators are especially useful in for loops, they can be used as shortcuts in many parts of your programs. 

++    add 1 to a variable
––    subtract 1 from a variable

Assignment operators — There are also some useful shortcuts for assigning values to variables.  For example, instead of n = n + 5, you can use n += 5. As you might expect there are, shortcuts for subtraction  –=, multiplication *=, division /=, and modulus (remainder) %=. These are called the assignment forms of the operators.


 

Try This

Here is a for loop that will count to 200 in steps of 5, just like the while loop you experimented with earlier.

  • Use Save Project As to save your program as Count to 200 by 5s.
  • Modify the for loop so that it looks like the this:

  • Verify that it counts to 200 in steps of 5.

 

Your Turn

  • Modify your program so that it counts up to 200 in steps of 5, then down to 0 in steps of 10.

Printer-friendly version
Code That Repeats
Prev
Index Array Variables
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress