Can you break and start an if loop?

[details=“Summary”]
This text will be hidden
So I want to create a simple program where some shapes slowly change color from red to green, then to blue and then back to red. To do so, I created integers r, g and b and made a draw method that would always do r = r + 1 etc.


After the first “while”-loop I put a break because otherwise it would just stay green. Now, of course, it stops after the break, starts again and just stops again, but if I put the break later or try to rearrange it, it would either just stay one color or try to change from red to green and from green to blue at the same time. The only way around this would be, if I could start the second loop and break the first loop at the same time (Do you understand what I mean?).
Is it possible to do this?
:blush:

(1. Sorry if I did something wrong or it isn’t clear what I’m trying to say, it’s my first time on this website
2. Sorry for my bad English
3. I’m pretty sure there are better ways to code this, but I’m a beginner and that’s the only way I can do it so far)[/details]

please format code with </> button * homework policy * asking questions

Hi @Kalarila,

Ok! Quite complicated. :slight_smile:

Please have a look into lerpColor

And have a look Here

Cheers
— mnse

Hi @mnse and thanks for answering me so quick,

so, I would like to break the first “for”-loop after as soon as the condition for the first “if”-loop (r = 0 & g = 255) is completed. Then I would like to start the second “for”-loop. If I just put the break after the “if” it’s not working (because it’s trying to break the “if” loop I think…?)

Cheers
-Kalarila

Take a look above

With that info you can get what you want a more easier way …

Cheers
— mnse

Hi @mnse

OOOOH, of course there is a much easier way, thank you :grin:

Cheers

  • Kalarila

Hi @Kalarila,

Yup! Give it a try and if you stuck don’t hesitate to come back.
Also please don’t use images for code, instead please use the </> Button on the edit window an paste your code inside the marks. Makes easier for us to support.

Cheers
— mnse

Hello!

I have written plenty of code that is similar to cycle through the colors and light up discrete RGB LEDs and it also works for Processing.

Take a look at the Processing website for all the related material on color.

In HSB colorMode() you can easily cycle through the colors of the color wheel.

This is the end result of a loop from 0 to 360 changing only the hue of a line:

image

One of the references:

I encourage you to do it both ways! RGB and HSB colorMode…

:)

I don’t think this is correct.

This break-command actually breaks the for-loop (and not only the if-clause)


int a=0;

for (int i=0; i<1000; i++) {

  if (a>3) {
    break; // leaves the for-loop
  }

  a++;
}

println (a);

1 Like