# While loop question

**URL:** <https://discourse.processing.org/t/while-loop-question/1230>\
**Category:** Beginners\
**Created:** [June 25, 2018, 2:33am UTC](https://discourse.processing.org/t/while-loop-question/1230 "2018-06-25T02:33:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parastoo](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@Parastoo](https://discourse.processing.org/u/Parastoo)\
**Post date:** [June 25, 2018, 2:33am UTC](https://discourse.processing.org/t/while-loop-question/1230/1 "2018-06-25T02:33:05Z")

</div>

I was watching The Coding Train’s While Loop tutorial (lesson 6.1-see link below).  
I had a really hard time digesting it and still don’t quite understand one thing. Why does it matter to reset x=0? I mean if you put “x=0;” before the While loop, shouldn’t it be useless?

[![](https://img.youtube.com/vi/RtAPBvz6k0Y/hqdefault.jpg "6.1: While Loop - Processing Tutorial") ](https://www.youtube.com/watch?v=RtAPBvz6k0Y)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 25, 2018, 2:39am UTC](https://discourse.processing.org/t/while-loop-question/1230/2 "2018-06-25T02:39:59Z")

</div>

- Java demands that local variables be initialized w/ some value before its 1st usage. ☕
- Keyword `while` is seldom needed, if at all! Use `for` instead: 🤠  
[https://Processing.org/reference/for.html](https://Processing.org/reference/for.html)

---

<div class="post-metadata">

**Author:** ![Parastoo](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@Parastoo](https://discourse.processing.org/u/Parastoo)\
**Post date:** [June 25, 2018, 3:35am UTC](https://discourse.processing.org/t/while-loop-question/1230/3 "2018-06-25T03:35:32Z")

</div>

Thanks. _for_ is much simpler and easier to understand. Any idea when “while” is used in lieu of “for”?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 25, 2018, 3:53am UTC](https://discourse.processing.org/t/while-loop-question/1230/4 "2018-06-25T03:53:42Z")

</div>

- The rare occasions in which I see that a `while () {}` would be a better fit than a `for () {}` block is when the conditional test to quit it isn’t based on some defined counter. 🤖
- Something like when we request an input from a user, keep asking again while the input is still invalid.
- I’ve got a sketch that does just that here: 🕶

> <https://gist.github.com/GoToLoop/bba0c288aaeeb5ef9bb1>

- Besides the inner block `while ((age = readByte("Enter your age:")) < 0);` it also has a variant called `do {} while ()` as its outer block: 😵

```auto
byte age;
do {
  while ((age = readByte("Enter your age:")) < 0);
  println("Your age is:", age);
} while (readBoolean("Again?"));

```

- There are other fitting situations that a `while () {}` block is better. 🤔
- But for now I can only think about the 1 I’ve posted above. 🙃

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [June 25, 2018, 10:25am UTC](https://discourse.processing.org/t/while-loop-question/1230/5 "2018-06-25T10:25:47Z")

</div>

> [@Parastoo](#):
>
> Thanks. _for_ is much simpler and easier to understand. Any idea when “while” is used in lieu of “for”?

Well, there’s an argument that `while` is a simpler form of `for` 😉

eg. these are equivalent

```auto
while (<condition>) { }
for ( ; <condition> ; ) { }

```

---

<div class="post-metadata">

**Author:** ![Bootzilla](https://avatars.discourse-cdn.com/v4/letter/b/c89c15/32.png) [@Bootzilla](https://discourse.processing.org/u/Bootzilla)\
**Post date:** [February 23, 2019, 9:05am UTC](https://discourse.processing.org/t/while-loop-question/1230/6 "2019-02-23T09:05:04Z")

</div>

I have a related question to this same bit of code in this video:

> float x = 0;  
> void setup(){  
> size(400,300);  
> }
> 
> void draw(){  
> background(0);
> 
> x=0;  
> while (x \< width){  
> x=x+20;
> 
> ```
> fill(101);
> 
> ```
> 
> stroke (255);  
> ellipse(x,150,16,16);  
> }
> 
> }

When it runs, it does this:

 ![18](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0feea1a57b89804b5b775106f248f0b68d755fcc.png)

My question is, if x=0 at the start of the draw loop, why doesn’t the circle just draw at (20,150) and stop there?

Actually, through typing this out, I think I might have worked it out. Is it because x=0 isn’t inside the while loop? So it starts at 0, then the while loop fires until x \> width?

OK, follow up question, why does it matter that x=0 is inside the draw loop when its already declared in the very first line at the top of the code? WIthout it inside the draw loop, it doesn’t draw a circle at all.

I think someone has maybe already answered this above but I’m really new to this (day 3) so I didn’t quite understand what they meant.

Thanks for your patience!

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [February 23, 2019, 9:18am UTC](https://discourse.processing.org/t/while-loop-question/1230/7 "2019-02-23T09:18:48Z")

</div>

> [@Bootzilla](#):
>
> OK, follow up question, why does it matter that x=0 is inside the draw loop when its already declared in the very first line at the top of the code?

Any variable declared outside `draw()` will retain the value it has between calls to draw (frames). So it’ll be 0 the first frame, and some value over width for every frame afterwards, because the loop keeps adding 20 to it until it is.

The `x = 0` resets the value back to zero for the _next_ frame, and so on.

---

<div class="post-metadata">

**Author:** ![Bootzilla](https://avatars.discourse-cdn.com/v4/letter/b/c89c15/32.png) [@Bootzilla](https://discourse.processing.org/u/Bootzilla)\
**Post date:** [February 23, 2019, 9:20am UTC](https://discourse.processing.org/t/while-loop-question/1230/8 "2019-02-23T09:20:13Z")

</div>

Ahhh, ok cool, thank you!
