Can a while command exist without being in a loop?

Hi, before I say anything else, this is my first post so I’m sorry if I messed up anywhere.

I’ve been experimenting with arduino for a few days and am getting the hang of it, but one thing that I don’t understand is the for/while commands. It was my understanding that a while(condition) command could be used without being inside another command, but I can only get it to work when it is inside a loop(), like so:

int ledPin = 13;                              // setting pin 13 as ledPin
int count = 0;                                // setting the count for the loop as zero

void setup() {
  pinMode(ledPin, OUTPUT);           // Setting pin 13 as an output to light up an led

void loop(){
  while (count < 10){                    // if variable 'count' is less than ten, continue
    digitalWrite(ledPin, HIGH);        // turn the pin on
    delay(1000);                           // delay one second
    digitalWrite(ledPin, LOW);         // turn the pin off
    delay(1000);                           // delay one second
    count++;                                // add one to the variable 'count'

Is it possible to have a while command without it being inside a loop command?


“loop” isn’t a command, it’s a function, like “setup”.

You can’t have working code outside of a function.

Your “while” loop is the same as writing the “for” loop:

 for ( ;count < 10; count++) {

but it would be better written as:

 for (count = 0; count < 10; count++) {

[edit]FYI, the “loop” function is called inside an infinite “while” loop.[/edit]

@AWOL Sorry for nitpicking but if he intended the loop only to execute the first time through loop() then what you say is better, would lead to a bug. No pun intended :)

In which case, it would have been better in "setup". ;)

Well, not if the purpose of the while-loop was to signal the first entry of loop() :)

Alright, this cleared a lot up for me. Thanks a ton!

Now that my question is answered, do I delete the posting or leave it up?

Leave it - others may find the question relevant.