How to stop a for function if an if statement becomes false

im trying to create a controller like think where a joystick if moved and the cursor moves on the lcd screen. im trying to make it so that once the if statement becomes false the for function within it will stop aswell and will only continue if the statement becomes true. so if the x_pos is not above 520 then the for function will terminate and the other statement will run.

  if (x_pos > 520) {
    for (byte i = 0; i < 16; i++) {

        // Continue loop if x_pos > 520
        lcd.setCursor(i, 1);
        lcd.write(byte(0));

        if (i > 0) {
          lcd.setCursor(i - 1, 1);
          lcd.print(" ");
        }

        delay(500);
      }
  }
}

You can’t have a delay and the for loop is in the wrong place since you don’t test the joystick anymore. Just use the loop and millis and check from time to time if the test is still true

For extra information and examples look at

My brother you dont understand. Im trying to create something like an xbox controller where if you move the joystick to the left the cursor goes to another spot thats what im doing. even if i swap delay for millis() it wont do nothing. my point is how to terminate a for function when the if statement it is in becomes false.

Hello hollandmab

Don´t talk, just do it.

Take a piece a paper and a pencil and design a program structure based on the IPO model before start coding.
The basics for coding the project are to be found in the IDE as examples.

Have a nice day and enjoy coding in C++.

:roll_eyes:

Seems you know way more than I do, Good luck then…

What you want is not very clear to me but a "For" function exits as soon as the second "header element" become false ("i < 16" in your case)
If it becomes "for (byte i = 0;i < 16 && x_pos > 520; i++)" then the loop stops (after the delay(500) expires) if x_pox <= 520.
If you want to cancel the delay as well, use millis() instead and read the three pages suggested by J-M-L Jackson

real sigmas use delay, only betas will ever consider touching millis()

Considered using a while()?
Millis() is non-blocking, Delay() is.
Delay(), runs on top of millis and waits for specified amount of time.

No idea what "sigma" means in this case.
My Opinion: professionals and committed, ambitious amateurs use non-blocking timing

translated into normal words
when variable x_pos has a value smaller than 520 stop printing to the lcd

whenever variable x_pos has a value greater that 520 go on printing to the lcd

is this a correct description of the funcionality you want?

without seeing where x_pos changes its value

You would have to add code inside the for-loop that does change the value of variable x_pos

If there is no code inside the for-loop that updates the value of variable x_pos how should this code ever stop before it has run through completely?
It is simply not possible! Except you are using interrupts. But if you are unexperienced with interrupts this will just mutliplicate your trouble to make it work.

As an experienced programmer I can analyse from what you have posted so far that you are more or less a beginner who has some mis-conceptions about how coding works what is possible in coding and what not.

And this is the reason why you should describe your wanted functionality in normal words by avoiding all programming terms.

best regards Stefan

may be

I'm not wasting my time with @hollandmab, it seems he knows it all.

As alpha male can get boring on its own, I guess.

Two problems in this thread.

  1. The above statement can't ever be true, the function within it can't run unless the if statement becomes true.
  2. You posted only a snippet, the context is lost

I won't be either. :wink:

do you want

    for (byte i = 0; i < 16 && x_pos > 520; i++) {

This is the perfect definition of the sigma male, from that article:
Although hordes of “experts” on Reddit will claim that the sigma male is the pinnacle of masculinity, the truth is that this personality type is little more than a bitter beta.

maybe using a "lambda"-expression will solve the TO's problems?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.