Monday, February 13, 2017

Increment Operator and Commenting

The increment operator is an Arduino arithmetic operator that is used to increment an integer variable by a value of one. We look at how to use the increment operator in this part of the Arduino programming course.
Comments in programming are notes that are written in a program (or sketch) by the programmer. These notes are used to explain what the code is doing, or to add other useful human readable information to the code. How, when and why to use comments are explained at the end of this part of the course.

Arduino Increment Operator

The Arduino increment operator is used to increase the value that a variable holds by one. This is useful in certain types of loops as will be shown later in this course.

Increment Operator Example 1

The example below shows the increment operator being used to increment a value several times.
void setup() {
  int count = 0;
  
  Serial.begin(9600);
  
  Serial.println(count++);
  Serial.println(count++);
  Serial.println(count++);
  
  Serial.println(count);
}

void loop() {
}

Sketch Output

The output of the sketch in the Arduino serial monitor window is shown here.

Initializing the Variable to Increment

In the above example, the variable count is defined and initialized to a value of 0 (zero).
It is important to initialize this variable before using the increment operator because if it is not initialized, it can contain any random value. Here we initialize it to 0, but could have used any other integer value.

Printing and Incrementing

Serial.println() is used to print the value of count, but in the same statement, the increment operator (++) is used to increase the value that the count variable holds.
The increment operation causes the value in count to increase from 0 to 1.

Post Incrementing

Placing the ++ after the variable name (count++) is a post increment operation. This means that the variable is used in the statement and only incremented after this.
Because of the post increment operation, the initial value of count (which is 0) is printed by the first println() function, and only incremented after this.
The next println() function prints the new incremented value (now 1) and then the increment operator in the statement increments the value of count after it has been printed – it is now incremented to 2.
The third println() statement prints out 2 and increments the value in count to 3.
The final println() statement prints out the value of count but does not use the increment operator. It prints out the value of 3 which the variable was previously incremented to.

Increment Operator Example 2

The following sketch does exactly the same as the previous sketch, but the increment operations and print operations have been separated into their own statements.
This example may help to clarify what is happening in the previous example. We can see that the first println() statements prints the value of count and only after this is the value in count incremented.
void setup() {
  int count = 0;
  
  Serial.begin(9600);
  
  Serial.println(count);
  count++;
  Serial.println(count);
  count++;
  Serial.println(count);
  count++;
  
  Serial.println(count);
}

void loop() {
}
From these two examples, we can see that this single line of code:
Serial.println(count++);
Is the same as these two lines of code:
Serial.println(count);
count++;
The important thing to note is that the incrementing of the variable takes place after printing it, so the value that the variable holds is printed before it is updated to the new incremented value.
As always, load the code to your Arduino and experiment with it.

Commenting Sketches

Commenting sketches allows you to write your own text notes along with the code of the sketch. The next sketch shows two ways of writing comments.
/*
    Sketch Name:  comments
    
    Purpose:      Demonstrates how to use comments.
                  Increments and displays a number in the main loop.
    
    Date:         28 September 2014
    
    Author:       W.A. Smith
*/
void setup() {
  Serial.begin(9600);       // use the serial port for printing the number
}

int count = 0;              // the number to print

void loop() {
  Serial.println(count++);  // print and increment the number
  delay(1000);              // 1 second delay between printing
}
This sketch uses the increment operator and can be seen running in the video below.

Multi-line Comments

Comments can be written on multiple lines when using the opening forward slash asterisk (/*) and closing asterisk forward slash (*/) as shown at the top of the sketch.
Any text comments can be written between the opening and closing of the comment and can span multiple lines. All text in the comment will be ignored by the compiler.
This style of commenting can also be used on a single line, but must always be closed with the asterisk and forward slash as shown below.
delay(1000);  /* 1 second delay between printing */
There is an easier way to comment a single line of code, by using the single line comment, explained next.

Single Line Comments

The double forward slash (//) is used to create a single line comment. All text to the right of the double forward slash on the same line will be a comment and will be ignored by the compiler.
The single line comment can be used on its own line or to the right of a program statement.
// the delay slows down the printing of the numbers
delay(1000);  // 1 second delay between printing



What Comments are Used For

Comments are used to explain how parts of a sketch work, rather than explain the actual programming language that is being written.
When you come back to a program or sketch that you wrote a month or a year ago, you may not remember why you wrote some part of the sketch a particular way. By writing comments in the sketch, you are making notes for yourself to help explain what you were doing when you first wrote the sketch.
Comments can also be arranged as a header section or comment block, like the multi-line comment block at the top of the example sketch. The block contains the name of the sketch, description or purpose of the sketch, date that it was written and the authors name. Comments can also contain references to any books, websites or other resources that were used when writing the sketch.
In short, comments and comment blocks communicate helpful information about the sketch for yourself and for other programmers who may also use the sketch.

No comments: