Skip to content

Declare counter variable in for statements #556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Language/Structure/Control Structure/break.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ In the following code, the control exits the `for` loop when the sensor value ex
[source,arduino]
----
int threshold = 40;
for (x = 0; x < 255; x ++) {
for (int x = 0; x < 255; x ++) {
analogWrite(PWMpin, x);
sens = analogRead(sensorPin);
if (sens > threshold) { // bail out on sensor detect
Expand Down
2 changes: 1 addition & 1 deletion Language/Structure/Control Structure/continue.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The `continue` statement skips the rest of the current iteration of a loop (link
The following code writes the value of 0 to 255 to the `PWMpin`, but skips the values in the range of 41 to 119.
[source,arduino]
----
for (x = 0; x <= 255; x ++) {
for (int x = 0; x <= 255; x ++) {
if (x > 40 && x < 120) { // create jump in values
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions Language/Variables/Data Types/array.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ Arrays are often manipulated inside for loops, where the loop counter is used as

[source,arduino]
----
int i;
for (i = 0; i < 5; i = i + 1) {
for (byte i = 0; i < 5; i = i + 1) {
Serial.println(myPins[i]);
}
----
Expand Down
5 changes: 2 additions & 3 deletions Language/Variables/Utilities/PROGMEM.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const PROGMEM uint16_t charSet[] = { 65000, 32796, 16843, 10, 11234};
const char signMessage[] PROGMEM = {"I AM PREDATOR, UNSEEN COMBATANT. CREATED BY THE UNITED STATES DEPART"};

unsigned int displayInt;
int k; // counter variable
char myChar;


Expand All @@ -81,14 +80,14 @@ void setup() {

// put your setup code here, to run once:
// read back a 2-byte int
for (k = 0; k < 5; k++) {
for (byte k = 0; k < 5; k++) {
displayInt = pgm_read_word_near(charSet + k);
Serial.println(displayInt);
}
Serial.println();

// read back a char
for (k = 0; k < strlen_P(signMessage); k++) {
for (byte k = 0; k < strlen_P(signMessage); k++) {
myChar = pgm_read_byte_near(signMessage + k);
Serial.print(myChar);
}
Expand Down
5 changes: 2 additions & 3 deletions Language/Variables/Utilities/sizeof.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ This program prints out a text string one character at a time. Try changing the
[source,arduino]
----
char myStr[] = "this is a test";
int i;

void setup() {
Serial.begin(9600);
}

void loop() {
for (i = 0; i < sizeof(myStr) - 1; i++) {
for (byte i = 0; i < sizeof(myStr) - 1; i++) {
Serial.print(i, DEC);
Serial.print(" = ");
Serial.write(myStr[i]);
Expand All @@ -73,7 +72,7 @@ Note that `sizeof` returns the total number of bytes. So for arrays of larger va
int myValues[] = {123, 456, 789};

// this for loop works correctly with an array of any type or size
for (i = 0; i < (sizeof(myValues) / sizeof(myValues[0])); i++) {
for (byte i = 0; i < (sizeof(myValues) / sizeof(myValues[0])); i++) {
// do something with myValues[i]
}
----
Expand Down