Posts Tagged ‘Debug’

Incrementing Variables Using ++

September 13, 2009

I received an email from Brendan, one of our 16 year old members attending the Make:KC meetings. He is looking into using the Processing language. One of his questions was about how variables are incremented using the ++ syntax.

  • The ++ syntax is for incrementing values – counting up by one each time.
  • The — syntax is for decrementing values – counting down by one each time.

References:

A thought about debugging

One thing I miss in the Processing and Arduino development environment is a way to step through a program line by line to debug a program as it’s running. Since we don’t have that feature the next best thing is by using print statements. The following programs are written to hopefully show you what is happening in the program as it is running.

Here is a short Processing program to illustrate incrementing:

Incrementing Variables in Processing


void setup()
{
 int a;
 int b;

 a=0;
 print("Initialize value: a = ");
 println(a);

 print("example 1: ++a is equal ");
 println(++a);
 println("  this example increments 'a' by 1 before evaluating (using) the variable");
 println("  in the print statement\n");

 a=0;
 print("Reinitialize: a = ");
 println(a);

 print("example 2: a++ is equal ");
 println(a++);
 println("  this example increments 'a' after printing its value");
 println("  The ++ incrementor only increases the value by 1 each time\n");

 a=0;
 print("Reinitialize: a = ");
 println(a);
 println("example 3: a += 1");
 print("  a equals ");
 println(a += 1);
 println("  This is another example of incrementing the variable by 1");
 println("  a += 1 is a simplified way of writing  a = a + 1\n");

 a=0;
 b=5;
 print("Reinitialize: a = ");
 print(a);
 print(" and b = ");
 println(b);
 println("example 4: a += b");
 print("  a equals ");
 println(a += b);
 println("  The += operator lets you change the incrementing value");
 println("  a += b is a simplified way of writing  a = a + b\n");

}

void draw()
{
}

When you look at Processing’s output view as you run the program you’ll see:

Initialize value: a = 0
example 1: ++a is equal 1
this example increments ‘a’ by 1 before evaluating (using) the variable in the print statement

Reinitialize: a = 0
example 2: a++ is equal 0
this example increments ‘a’ after printing its value
The ++ incrementor only increases the value by 1 each time

Reinitialize: a = 0
example 3: a += 1
a equals 1
This is another example of incrementing the variable by 1
a += 1 is a simplified way of writing  a = a + 1

Reinitialize: a = 0 and b = 5
example 4: a += b
a equals 5
The += operator lets you change the incrementing value
a += b is a simplified way of writing  a = a + b

To illustrate the similarities of programming in Processing and programming for an Arduino, consider this program.

Incrementing Variables in Arduino

void setup()
{
int a;
int b;

Serial.begin(9600);

a=0;
Serial.print("Initialize value: a = ");
Serial.println(a);

Serial.print("example 1: ++a == ");
Serial.println(++a);
Serial.println("  this example increments 'a' by 1 before evaluating (using) the variable");
Serial.println("  in the print statement\n");

a=0;
Serial.print("Reinitialize: a = ");
Serial.println(a);

Serial.print("example 2: a++ == ");
Serial.println(a++);
Serial.println("  this example increments 'a' after printing its value");
Serial.println("  The ++ incrementor only increases the value by 1 each time\n");

a=0;
Serial.print("Reinitialize: a = ");
Serial.println(a);
Serial.println("example 3: a += 1");
Serial.print("  a equals ");
Serial.println(a += 1);
Serial.println("  This is another example of incrementing the variable by 1\n");

a=0;
b=5;
Serial.print("Reinitialize: a = ");
Serial.print(a);
Serial.print(" and b = ");
Serial.println(b);
Serial.println("example 4: a += b");
Serial.print("  a equals ");
Serial.println(a += b);
Serial.println("  The += operator lets you change the incrementing value\n");
}

void loop()
{}

The only difference with an Arduino program is in order to display messages on your computer screen you need to send them through a serial connection using the “Serial.” prefix on the “print” statement. The variables get incremented the same way whether using Processing or Arduino.

(c) 2009 – Vince Thompson