Skip to content

Commit 2bf0501

Browse files
committed
Declare counter variable in for statements
Declaring the counter variable outside the for statement is done very rarely in actual programming, and only when there is a use for that variable outside the scope of the for loop (which is not the case here). I don't see any value in declaring the variable outside the statement in the example code, as it only teaches bad programming. In some cases, the example code snippets didn't even contain a declaration and so would not compile if copied into a sketch.
1 parent 674a92e commit 2bf0501

File tree

5 files changed

+7
-10
lines changed

5 files changed

+7
-10
lines changed

Language/Structure/Control Structure/break.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ In the following code, the control exits the `for` loop when the sensor value ex
3636
[source,arduino]
3737
----
3838
int threshold = 40;
39-
for (x = 0; x < 255; x ++) {
39+
for (int x = 0; x < 255; x ++) {
4040
analogWrite(PWMpin, x);
4141
sens = analogRead(sensorPin);
4242
if (sens > threshold) { // bail out on sensor detect

Language/Structure/Control Structure/continue.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The `continue` statement skips the rest of the current iteration of a loop (link
3636
The following code writes the value of 0 to 255 to the `PWMpin`, but skips the values in the range of 41 to 119.
3737
[source,arduino]
3838
----
39-
for (x = 0; x <= 255; x ++) {
39+
for (int x = 0; x <= 255; x ++) {
4040
if (x > 40 && x < 120) { // create jump in values
4141
continue;
4242
}

Language/Variables/Data Types/array.adoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ Arrays are often manipulated inside for loops, where the loop counter is used as
6767

6868
[source,arduino]
6969
----
70-
int i;
71-
for (i = 0; i < 5; i = i + 1) {
70+
for (byte i = 0; i < 5; i = i + 1) {
7271
Serial.println(myPins[i]);
7372
}
7473
----

Language/Variables/Utilities/PROGMEM.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ const PROGMEM uint16_t charSet[] = { 65000, 32796, 16843, 10, 11234};
7171
const char signMessage[] PROGMEM = {"I AM PREDATOR, UNSEEN COMBATANT. CREATED BY THE UNITED STATES DEPART"};
7272
7373
unsigned int displayInt;
74-
int k; // counter variable
7574
char myChar;
7675
7776
@@ -81,14 +80,14 @@ void setup() {
8180
8281
// put your setup code here, to run once:
8382
// read back a 2-byte int
84-
for (k = 0; k < 5; k++) {
83+
for (byte k = 0; k < 5; k++) {
8584
displayInt = pgm_read_word_near(charSet + k);
8685
Serial.println(displayInt);
8786
}
8887
Serial.println();
8988
9089
// read back a char
91-
for (k = 0; k < strlen_P(signMessage); k++) {
90+
for (byte k = 0; k < strlen_P(signMessage); k++) {
9291
myChar = pgm_read_byte_near(signMessage + k);
9392
Serial.print(myChar);
9493
}

Language/Variables/Utilities/sizeof.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,13 @@ This program prints out a text string one character at a time. Try changing the
4646
[source,arduino]
4747
----
4848
char myStr[] = "this is a test";
49-
int i;
5049
5150
void setup() {
5251
Serial.begin(9600);
5352
}
5453
5554
void loop() {
56-
for (i = 0; i < sizeof(myStr) - 1; i++) {
55+
for (byte i = 0; i < sizeof(myStr) - 1; i++) {
5756
Serial.print(i, DEC);
5857
Serial.print(" = ");
5958
Serial.write(myStr[i]);
@@ -73,7 +72,7 @@ Note that `sizeof` returns the total number of bytes. So for arrays of larger va
7372
int myValues[] = {123, 456, 789};
7473
7574
// this for loop works correctly with an array of any type or size
76-
for (i = 0; i < (sizeof(myValues) / sizeof(myValues[0])); i++) {
75+
for (byte i = 0; i < (sizeof(myValues) / sizeof(myValues[0])); i++) {
7776
// do something with myValues[i]
7877
}
7978
----

0 commit comments

Comments
 (0)