Loopus Doopus

Why does this loop run continuously? I expected it to stop when a = 7.

int a = 1;
void setup() {

Serial.begin(9600);
}

void loop() {
for (a = 1; a < 8; a++)
{ int b = 1; }

}
int main()
{ Serial.println(a);
return 0; }

The Scientific Method requires that you publish your observations.

You may spot the solution to your problem yourself, if you do.

The loop() behaviour is explained in that the Arduino has a main() function so, (or in your case 2 main functions) :

#include <Arduino.h>

int main(void)
{
    init();
#if defined(USBCON)
    USBDevice.attach();
#endif
    setup();
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
    return 0;
}

Remove the main() function please, there is no main() function in Arduino. Only setup() and loop(). The setup() runs once and the loop() runs over and over again.

int a = 1;

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

void loop() {
  for (a = 1; a < 8; a++)
  {
    Serial.print(a);
    Serial.print(", ");
    delay(200);
    int b = 1;
  }
  Serial.println();
  delay(500);
}

How do you KNOW that the code is looping continuously vs not stopping when a = 7?
I would argue that while you may observer nothing happening (no serial output), your conclusion is not what is actually happening.

You have overridden the main() function which the Arduino environment uses to initialize itself.
In effect you have disabled the entire Arduino environment.
neither setup() nor loop() will ever be called.

Your main() will attempt to print the value of a followed by and but the serial port has not been initialized. Either the code will hang attempting to use uninitialized hw or when Serial.println() returns, main() will return back to the C runtime startup code which will immediately fall into a hard loop since returning from main() is considered a failure.

Arduino sektches do not and cannot use main() if they want to use the normal Arduino environment.
setup() and loop() is the way to interface into the Arduino environment.

— bill

6v6gt: The loop() behaviour is explained in that the Arduino has a main() function so, (or in your case 2 main functions) :

There won't ever be two main() functions. There will be single main() function in the linked image. If there is a main() function in a sketch, it overrides the main() that the Arduino environment supplied that is need to run the Arduino environment.

bperrybap:
I know the loop never ends because I get continuous never ending 8’s printing out on the monitor.
I did consider that the Setup.pringln(a) instruction may not be returning actual (a), but I don’t know how to determine that.

I made the following changes but now I only see a single 1, as if the setup print command was the only output.

Koepel: You (Koepel) stated: Remove the main() function please, there is no main() function in Arduino. Only setup() and loop(). The setup() runs once and the loop() runs over and over again.

It seems that setup is running before the loop and not after because below print(a) shows 1, not 7. By the way and maybe unrelated, I added int b = 1 in the loop because it won’t compile without something there.

int a = 1;
void setup() {

Serial.begin(9600);
Serial.println(a);
}

void loop() {
for (a = 1; a < 8; a++)
{ int b = 1; }

}

Here’s the Foundations Page.

And here’s from the link explaining what a Sketch is.

setup() and loop()

There are two special functions that are a part of every Arduino sketch: setup() and loop(). The setup() is called once, when the sketch starts. It’s a good place to do setup tasks like setting pin modes or initializing libraries. The loop() function is called over and over and is heart of most sketches. You need to include both functions in your sketch, even if you don’t need them for anything.

There’s several more, don’t miss the AVR LibC (AVR-GCC) Standard Libraries Reference linked to on the lower right of the Arduino Reference Page.

void setup() {

  Serial.begin(9600);
  Serial.println(a);

  for (int a = 1; a < 8; a++)  int b = 1; // this line will get optimized out since b does nothing.

}

void loop() {
}
byte counter = 0, count = 8;

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

void loop() 
{
  Serial.println( ++counter ); // pre-increments counter so 0 becomes 1... 7 becomes 8

  while (counter == count ); // traps execution when counter reaches count
}

Thanks. I'll study the Foundations tutorial. Note that my previous sample was extracted from another tutorial on looping.

edkost:
bperrybap:
I know the loop never ends because I get continuous never ending 8’s printing out on the monitor.

Not with the example code you posted in post #1.
The code that you showed us originally in post #1 cannot produce that behavior.
You must have been running code that is different from what you were showing us.
Providing correct and exact details are very important when asking for help.

— bill

Not only will loop() never be called, if it was called, it would do absolutely nothing, because the entire for loop would be optimized out by the compiler, and loop() will end up empty.

This is what is left of that code once the compiler does optimization:

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

void loop() {
}

int main()
{ 
    Serial.println(a);
    return 0; 
}

I would guess the return 0 likely causes a jump to the reset vector., so main just runs over, and over, and over again.

So many mistakes in so few lines of code....

Regards, Ray L.

RayLivingston: Not only will loop() never be called, if it was called, it would do absolutely nothing, because the entire for loop would be optimized out by the compiler, and loop() will end up empty.

This is what is left of that code once the compiler does optimization:

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

void loop() { }

int main() {    Serial.println(a);    return 0; }




I would guess the return 0 likely causes a jump to the reset vector., so main just runs over, and over, and over again.

So many mistakes in so few lines of code....

Regards,
Ray L.

You would still have the declaration and initialization of the a variable. But then you forgot to account for the linker. The IDE told the compiler to create a separate link segment for each function. The linker is told to remove segments that have no references. The linker will realize that there were no references (calls) to setup() and loop() so the linker would remove the segment for setup() and the segment for loop() which removes all the code for those functions from the final linked image. Leaving:

int a = 1;

int main()
{ 
    Serial.println(a);
    return 0; 
}

So not only are they not called, but the actual functions and the code inside them will be completely stripped from the final linked image.

In terms of what happens when main returns, The C runtime code will fall into a hard loop.

The C runtime code is what gets called from the reset vector. It sets up the C environment. Part of that is code that handles all the copying of the initialized data (which would be the 2 bytes for the initialization of a), after all this done, it calls/jumps to main() Should main() return, the C runtime code jumps to _exit() and _exit() masks interrupts and then has a hard loop jumping to itself.

Returning from main() will put the processor in a hard loop with interrupts masked. Basically a "halt and catch fire" situation. Only recovery from this is hard reset or WDT (which is also a reset)

--- bill

RayLivingston: So many mistakes in so few lines of code....

But to me, the biggest issue was that the code we saw could not create the claimed behavior so we were not seeing the code that was actually being used to create the claimed behavior.

--- bill

Your are right about that being the wrong code. I grabbed it from my original input to the thread below and it returned nothing on the monitor. Now I don't have my original code I thought I was referencing so I guess I will start all over again. Ugh!

Learning to be meticulous is part of learning coding.

Please use code tags.

Read this before posting a programming question

How to use this forum

OK, I have gotten a little further with loops, but in this code below I do not understand why a loop is necessary. If I remove the void loop() and while (true) instructions it will not compile. Otherwise it works as I intend it to.

/* Converts a 16-bit decimal integer to binary and displays as 16 binary values D0 through D15. */

void setup() { // Open serial communications: Serial.begin(9600); } int a = 65514 + 65536; //Enter the string integer here. (the first integer; the second integer is an offset)

void loop() { // make a string: String stringOne = String(a, BIN); //Converts the integer constant into a binary string

char identifieddigit0 = stringOne.charAt(0); //Loads the value of position 0 into identifieddigit0 char identifieddigit1 = stringOne.charAt(1); char identifieddigit2 = stringOne.charAt(2); char identifieddigit3 = stringOne.charAt(3); char identifieddigit4 = stringOne.charAt(4); char identifieddigit5 = stringOne.charAt(5); char identifieddigit6 = stringOne.charAt(6); char identifieddigit7 = stringOne.charAt(7); char identifieddigit8 = stringOne.charAt(8); char identifieddigit9 = stringOne.charAt(9); char identifieddigit10 = stringOne.charAt(10); char identifieddigit11 = stringOne.charAt(11); char identifieddigit12 = stringOne.charAt(12); char identifieddigit13 = stringOne.charAt(13); char identifieddigit14 = stringOne.charAt(14); char identifieddigit15 = stringOne.charAt(15); char identifieddigit16 = stringOne.charAt(16); char identifieddigit17 = stringOne.charAt(17);

String message0 = "D0: ";

String message1 = "D15: "; Serial.println(message1 + " " + identifieddigit1); String message2 = "D14: "; Serial.println(message2 + " " + identifieddigit2); String message3 = "D13: "; Serial.println(message3 + " " + identifieddigit3); String message4 = "D12: "; Serial.println(message4 + " " + identifieddigit4); String message5 = "D11: "; Serial.println(message5 + " " + identifieddigit5); String message6 = "D10: "; Serial.println(message6 + " " + identifieddigit6); String message7 = "D9: "; Serial.println(message7 + " " + identifieddigit7); String message8 = "D8: "; Serial.println(message8 + " " + identifieddigit8); String message9 = "D7: "; Serial.println(message9 + " " + identifieddigit9); String message10 = "D6: "; Serial.println(message10 + " " + identifieddigit10); String message11 = "D5: "; Serial.println(message11 + " " + identifieddigit11); String message12 = "D4: "; Serial.println(message12 + " " + identifieddigit12); String message13 = "D3: "; Serial.println(message13 + " " + identifieddigit13); String message14 = "D2: "; Serial.println(message14 + " " + identifieddigit14); String message15 = "D1: "; Serial.println(message15 + " " + " " + identifieddigit15); String message16 = "D0: "; Serial.println(message16 + " " + " " + identifieddigit16);

Serial.println(); Serial.println(a - 65536); //Prints the input integer in base 10 Serial.println(a-65536, BIN); //Prints the input integer in base 2

while (true); }

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

int a = 65514 + 65536;

Wuh?

edkost: OK, I have gotten a little further with loops, but in this code below I do not understand why a loop is necessary. If I remove the void loop() and while (true) instructions it will not compile. Otherwise it works as I intend it to.

Have you actually read ANY of the Arduino tutorials?? The need for setup() and loop(), and how they work, are lesson #1. You're not going to learn how to write Arduino code by fumbing around in the dark, making guesses. You have to actually put some effort into learning how it all works....

Regards, Ray L.