Help with Loops and IFs??

First time programmer here, please be gentle :P

I'm used to programming in pBasic from taking a course in it in school but I'm trying to teach myself C/C++ or whatever the Arduino programming language is called and having writer's block so to say.

I need to write a bit of code that uses (if need be) an IF statement where if the condition is true it needs to loop to itself endlessly until the condition is not met.

For example, in pBasic I would write:

StarterCheck: If StarterIn = HIGH THEN StarterCheck

How would I do this in C?

That would be GoTo, I guess.

It wouldn't cause an issue using GoTo to send it to itself?

See this example: http://arduino.cc/en/Reference/Continue

A sub calling gosub itself causes infinite recursion and eventually a crash due to memory exhaustion.

edit: that is, if there's no end condition or it's triggered too late.

LockDots: I need to write a bit of code that uses (if need be) an IF statement where if the condition is true it needs to loop to itself endlessly until the condition is not met.

For example, in pBasic I would write:

StarterCheck: If StarterIn = HIGH THEN GoSub StarterCheck

In C/C++ you'd use a while loop:

while(StarterIn == HIGH)
{
    // update StartIn here by some means
}

If StartIn is a variable (as implied in the code above) then you'd need to either do something inside the loop that caused it to become HIGH when you wanted to exit the loop, or have some other means to update it (for example you might have an interrupt handler updating it). Usually, you'd execute some code inside the loop to work out the current value.

LarryD:
See this example:
http://arduino.cc/en/Reference/Continue

I’m a little confused by this one. Care to explain how it works? The explanation isn’t so clear, at least not to me.

tuxduino:
A sub calling gosub itself causes infinite recursion and eventually a crash due to memory exhaustion.

edit: that is, if there’s no end condition or it’s triggered too late.

I meant to write just the name of the tag (StarterCheck) without GoSub before it.

PeterH:
In C/C++ you’d use a while loop:

while(StarterIn == HIGH)

{
// update StartIn here by some means
}




If StartIn is a variable (as implied in the code above) then you'd need to either do something inside the loop that caused it to become HIGH when you wanted to exit the loop, or have some other means to update it (for example you might have an interrupt handler updating it). Usually, you'd execute some code inside the loop to work out the current value.

Hmm, this one confuses me too (I hope I’m not hopeless :()

This is the part of my flowchart that I would like to code:

What’s the easiest or simplest way to do this?

I don’t mean to make it sound like I’m asking you to just write it for me, but I’m stuck pretty bad right now :~

Will this suffice the flow chart?

loop(){

  //return on LOW, loop will automatically restart.
  if( Variable != HIGH ) return;

  while( Variable == HIGH  ){
   //Do stuff while true.
  }
}

pYro_65: Will this suffice the flow chart?

loop(){

  //return on LOW, loop will automatically restart.   if( Variable != HIGH ) return;

  while( Variable == HIGH  ){   //Do stuff while true.   } }

pYro, would you mind explaining how that works? I'm trying to step through it, but I'm confusing myself.

Sure,

The arduino has a hidden function in the core called main ( a standard c++ entry point ), it is the first* function to run, it has these few lines:

int main(){

  init();

  setup();

  while( true ){
    loop();
  }
}

init(); sets up the arduino timers/pwm/other stuff

setup(); calls the setup function you define in your sketch.

loop(); this also calls your sketch loop function, notice it is inside a [u]never ending[/u] loop.


Now the code I provided:

loop(){

  //return on LOW, loop will automatically restart.
  if( Variable != HIGH ) return;

  while( Variable == HIGH  ){
   //Do stuff while true.
  }
}

As this function is called in an infinite loop, it will repeat as soon as it finishes.

if( Variable != HIGH ) return; This will cause the function to exit when the variable is low.

When HIGH, the code is allowed to flow into the 'while( Variable == HIGH ){' loop, which will not exit until the variable goes LOW.

As the loop function continually loops, it will return to the if statement constantly exiting the function until another HIGH

pYro you are awesome! That makes a lot more sense now. Thank you so much!

Why is this being made complicated ? Reply #5 had the answer.

I meant to write just the name of the tag (StarterCheck) without GoSub before it.

A label is not a sub(routine). GoSub is basic dialect for "call a subroutine". Goto is not the same as gosub.

Did you read http://arduino.cc/en/Tutorial/HomePage Section 1. Basics ? You seem to miss some fundamental bits.

pYro_65:   if( Variable != HIGH ) return;

I don't think this statement contributes anything to the behaviour.

No, not to this one single example, but usually sketches are more complex than two conditional statements.

If you consider an idea that is fractionally more involved, say, If things need to happen once on a high value, the loop by itself will not suffice either.

If StarterIn = HIGH THEN StarterCheck

The C equivalent would be

If (StarterIn == HIGH) {StarterCheck;}

A loop version of it would be

while(StarterIn == HIGH) continue;

tuxduino:

I meant to write just the name of the tag (StarterCheck) without GoSub before it.

A label is not a sub(routine). GoSub is basic dialect for "call a subroutine". Goto is not the same as gosub.

Did you read http://arduino.cc/en/Tutorial/HomePage Section 1. Basics ? You seem to miss some fundamental bits.

I understand the difference between a GoSub and a Goto. A Goto does just what it implies, it goes to a certain part of the program and continues on to the rest of the code from there whereas a GoSub goes to a subroutine and returns to the following line from where it was called.

In pBasic to accomplish what I have in my flowchart I would write the following:

StarterCheck:
If StarterIn = LOW THEN StarterCheck

StarterCheck: is a placemarker, tag, or label (whatever you want to call it). The "IF" statement would check to see if variable StarterIn is LOW then it will force the program to return to the tag/label "StarterCheck:". This keeps the program looping from "StarterCheck:" to the IF statement as long as variable StarterIn is LOW. Once it goes HIGH, the IF statement is no longer true and so the program falls through to whatever line of code is after the IF statement.

note: In my original post my IF statement was written incorrectly (used HIGH, instead of LOW), the correction has been made above.

So I guess my real question is, how do I create a tag, label, or placemarker in C for the program to go to when an IF condition has been met?

So I guess my real question is, how do I create a tag, label, or placemarker in C for the program to go to when an IF condition has been met?

You don't. It executes the next instruction (or block of instructions.

if(this == that)
   doThis();

or

if(them == those)
{
   doThis();
   andThis();
   andDontForgetThis();
   meToo();
}

Haha. Well that kind of stinks.... There's got to be a way to do it, no? :(

I need the program to get to a certain point where it gets stuck in a loop waiting for an input to go HIGH. Once it goes HIGH I would like it to fall through to the rest of the code.