WHich loop to use and when? do/while, for, else, if else, while, goto

Trying to figure out which loop to use. I've read the Arduino reference, so in theory i get it. But what I'm after are simple examples.

Can someone point me to where I can find some simple examples comparing the different loops and why and when to use one loop command instead of another.

Thanks

First the simple one, just never use goto :wink:

Next up, most loops can be transformed into each other but it might be handy to use a particular form.

For example, if you want to check if the outcome of some test is within limits it would be logical to use a do-while loop. Because he, you need to at least do the stuff one to know the outcome.

But if you want to loop over a set of unknown length, it's pretty stupid to first do something and only check afterwards. So while() or for() is more logical. Especially for() if you need to initiate something before you start (like a loop variable).

If, else and if else has nothing to do with looping :wink: It just decides if you want to do something or not :wink:

septillion:
First the simple one, just never use goto :wink:

Next up, most loops can be transformed into each other but it might be handy to use a particular form.

For example, if you want to check if the outcome of some test is within limits it would be logical to use a do-while loop. Because he, you need to at least do the stuff one to know the outcome.

But if you want to loop over a set of unknown length, it's pretty stupid to first do something and only check afterwards. So while() or for() is more logical. Especially for() if you need to initiate something before you start (like a loop variable).

If, else and if else has nothing to do with looping :wink: It just decides if you want to do something or not :wink:

Thanks

I understand GoTo is not ideal, so how do I exit a loop that would take two different conditions?

Something like
Loop

Check to see if push button has been pressed.
If PB = High and Pin output = low exit go to routine A
If PB = High and Pin output = HIGH exit go to routine B
If no button press contine loop looking for button press.

GoTo would work, but what's better?

The ‘for’ loop exists because the following is a very common construct:

    int i = 0;  // Initialize the loop variable
    while (i < loopLimit) {  // Test for end of loop
        // Do Stuff
        i++;  // Increment loop varaible
    }  // End of loop body

It is much easier to write (and read!) it it is all on one line:

    for (int i = 0; i < loopLimit; i++) {
        // Do Stuff
    }  // End of loop body

If your loop looks much like the ‘while’ above you probably should have used the ‘for’.

void loop()
{
    boolean PB = digitalRead(PushButtonPin);
    if (PB)
    {
        if (PinOutput == LOW) 
            routineA();
        else
            routineB();
    }
}

You don't need to exit it, you just temperately jump out of it

So:
Check to see if push button has been pressed.
If (PB = High and Pin output = low) exit go to routine run function A
else If PB = High and Pin output = HIGH exit go to routine run function B
If no button press contine loop looking for button press.

And the continues looping is already done by loop() :slight_smile:

Another thing to keep in mind is NOT to use FOR or WHILE loops if they take any appreciable time to complete - anything more than 100 microsecs or thereabouts - as they block the Arduino from doing other things until they complete.

If you need to repeat something that takes longer then just allow loop() to do the repetition and test the condition with IF

...R

Doug101:
Can someone point me to where I can find some simple examples comparing the different loops and why and when to use one loop command instead of another.

this guy explains loops and then in the next video, he explains control flow statements.

the series is great...

Let us see some codes on the applications of loop command with reference to the following diagram.

Program codes to check that the button K1 of the above circuit has been closed and then turn-non (ignite) L (built-in LED of UNO).

1. Using Flow Chart

2. Using goto statement

void setup() 
{
  pinMode(13, OUTPUT);      //IO line of DPin-13 will work as output line
  pinMode(2, INPUT_PULLUP); //IO line of DPin-2 will work as input with internal pull-up
  digitalWrite(13, LOW);  //initially L is OFF
  
  L1: bool statusK1 = digitalRead(2); //reading ope/close status of K1
  if (statusK1 == LOW)            //K1 is closed
  {
    digitalWrite(13, HIGH);       //ignite L
    while(1);                     //wait here for ever; there is no halt instruction for AVR?
  }
  else
  {
    goto L1;                      //K1 is not closed
  }
}

void loop() 
{
  
}

3. Using do-while Structure

void setup()
{
  pinMode(13, OUTPUT);      //IO line of DPin-13 will work as output line
  pinMode(2, INPUT_PULLUP); //IO line of DPin-2 will work as input with internal pull-up
  digitalWrite(13, LOW);  //initially L is OFF

  do
  {
    ;
  }
  while(digitalRead(2)!=LOW);
  digitalWrite(13, HIGH);       //ignite L
}

void loop()
{
  
}

4. Using while-do Structure

void setup()
{
  pinMode(13, OUTPUT);      //IO line of DPin-13 will work as output line
  pinMode(2, INPUT_PULLUP); //IO line of DPin-2 will work as input with internal pull-up
  digitalWrite(13, LOW);  //initially L is OFF

  while(digitalRead(2)!=LOW)
    ;
  digitalWrite(13, HIGH);       //ignite L
}

void loop()
{
  
}

5. Using for() Loop

void setup()
{
  pinMode(13, OUTPUT);      //IO line of DPin-13 will work as output line
  pinMode(2, INPUT_PULLUP); //IO line of DPin-2 will work as input with internal pull-up
  digitalWrite(13, LOW);  //initially L is OFF

  for (; digitalRead(2)==HIGH; )
  {
    ;
  }
  digitalWrite(13, HIGH);       //ignite L
}

void loop()
{
  
}