# For statement to while statement

**URL:** <https://discourse.processing.org/t/for-statement-to-while-statement/24648>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [October 16, 2020, 3:46pm UTC](https://discourse.processing.org/t/for-statement-to-while-statement/24648 "2020-10-16T15:46:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellie98](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ellie98](https://discourse.processing.org/u/Ellie98)\
**Post date:** [October 16, 2020, 3:46pm UTC](https://discourse.processing.org/t/for-statement-to-while-statement/24648/1 "2020-10-16T15:46:36Z")

</div>

I have to change the for statement into a while statement. But i’m getting a different output. Where is my mistake?

```auto
for (int k = 3; k > 0; k--) {
  if (k%2 == 0)
    continue;
  println(k);
}

int k = 3;
while (k > 0) {
    if (k%2 == 0)
      continue;
    k--;
    println(k);
  }

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 16, 2020, 5:17pm UTC](https://discourse.processing.org/t/for-statement-to-while-statement/24648/2 "2020-10-16T17:17:43Z")

</div>

In the doc’s is written that continue; should work in a while loop, so I’m also curious. Following code is cheating isn’t it?

```auto
int k = 3;

while (k > 0) {
  if (k%2 != 0)
    println(k);
  k--;
}

```

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [October 16, 2020, 5:40pm UTC](https://discourse.processing.org/t/for-statement-to-while-statement/24648/3 "2020-10-16T17:40:23Z")

</div>

Bit of a puzzler there.

The thing here is that in the while loop, `continue` also skips the index variable counting / setting `k--`, since it’s not built into the while statement.

```auto
int k = 3;
while (k > 0) {
    if (k%2 == 0) {
      k--;
      continue;
    }
    println(k);
    k--;
  }

```
