# Can you break and start an if loop?

**URL:** https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761
**Category:** Coding Questions
**Created:** [June 29, 2022, 7:06pm UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761 "2022-06-29T19:06:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Kalarila](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Kalarila](https://discourse.processing.org/u/Kalarila)
#### Post date: [June 29, 2022, 7:06pm UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/1 "2022-06-29T19:06:26Z")

</div>

[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.

 ![Bildschirmfoto 2022-06-29 um 20.50.51](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/3/031d2b27d7af3ef9fa5fb1ff4d9c7d567a30723d.png)  
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?  
😊

(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](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [June 29, 2022, 7:12pm UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/2 "2022-06-29T19:12:31Z")

</div>

Hi @Kalarila,

Ok! Quite complicated. 🙂

Please have a look into [lerpColor](https://processing.org/reference/lerpColor_.html)

And have a look [Here](https://discourse.processing.org/t/how-do-i-cycle-lerp-between-multiple-colors/13441/5)

Cheers  
— mnse

---

<div class="post-metadata">

### Author: ![Kalarila](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Kalarila](https://discourse.processing.org/u/Kalarila)
#### Post date: [June 29, 2022, 7:21pm UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/3 "2022-06-29T19:21:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [June 29, 2022, 7:25pm UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/4 "2022-06-29T19:25:51Z")

</div>

Take a look [above](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/2)

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

Cheers  
— mnse

---

<div class="post-metadata">

### Author: ![Kalarila](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Kalarila](https://discourse.processing.org/u/Kalarila)
#### Post date: [June 29, 2022, 7:27pm UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/5 "2022-06-29T19:27:29Z")

</div>

Hi @mnse

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

Cheers

- Kalarila

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [June 29, 2022, 7:36pm UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/6 "2022-06-29T19:36:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 29, 2022, 11:26pm UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/7 "2022-06-29T23:26:04Z")

</div>

> [@Kalarila](#):
>
> 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.

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](https://en.wikipedia.org/wiki/Color_wheel).

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

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/4/5429d9796537566ad6622860fe4dfdaf061f003d.png)

One of the references:

> **[colorMode() / Reference](https://processing.org/reference/colorMode_.html)**
>
> Changes the way Processing interprets color data. By default, the parameters for fill(), stroke(), background(), and color() are defined by values between 0 and 255 using t…

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

`:)`

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 30, 2022, 8:35am UTC](https://discourse.processing.org/t/can-you-break-and-start-an-if-loop/37761/8 "2022-06-30T08:35:53Z")

</div>

> [@Kalarila](#):
>
> If I just put the break after the “if” it’s not working (because it’s trying to break the “if” loop I think…?)

I don’t think this is correct.

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

```auto

int a=0;

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

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

  a++;
}

println (a);

```
