# What is the keyword "do"

**URL:** <https://discourse.processing.org/t/what-is-the-keyword-do/6433>\
**Category:** Beginners\
**Created:** [December 9, 2018, 12:03am UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433 "2018-12-09T00:03:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dragon\_hatcher](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dragon\_hatcher](https://discourse.processing.org/u/dragon_hatcher)\
**Post date:** [December 9, 2018, 12:03am UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433/1 "2018-12-09T00:03:59Z")

</div>

I can see that when I type “do” in processing it is highlighted so it is a keyword however I can’t find any reference to in the manual or online. I was wondering what it does.

---

<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:** [December 9, 2018, 12:58am UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433/2 "2018-12-09T00:58:37Z")

</div>

- [Docs.Oracle.com/javase/tutorial/java/nutsandbolts/while.html](http://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/while.html)
- [Forum.Processing.org/two/discussions/tagged?Tag=do%2Fwhile](http://Forum.Processing.org/two/discussions/tagged?Tag=do%2Fwhile)

---

<div class="post-metadata">

**Author:** ![dragon\_hatcher](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dragon\_hatcher](https://discourse.processing.org/u/dragon_hatcher)\
**Post date:** [December 9, 2018, 1:04am UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433/3 "2018-12-09T01:04:11Z")

</div>

ah, thanks. Any idea why it isn’t in the reference?

---

<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:** [December 9, 2018, 1:07am UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433/4 "2018-12-09T01:07:18Z")

</div>

The devs arbitrarily decide what goes to the reference page. 🤪

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 12, 2018, 10:05pm UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433/5 "2018-12-12T22:05:49Z")

</div>

Also, in practice, the do-while construct is infrequently used in Processing (or in contemporary Java), perhaps in part because you can achieve the same effect with a bare `while`. It is more common to use an [equivalent construct](https://en.wikipedia.org/wiki/Do_while_loop#Equivalent_constructs) . While do-while can fulfill a specific niche need, some people [avoid do-while as a question of style](https://stackoverflow.com/questions/994905/is-there-ever-a-need-for-a-do-while-loop).

For learners, how likely they will need:

- `if`: common
- `for`: common
- `while`: less common
- `switch`: uncommon – they need it, but they still don’t use it
- `do-while`: really, really rare

---

<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:** [December 12, 2018, 10:30pm UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433/6 "2018-12-12T22:30:15Z")

</div>

> [@jeremydouglass](#):
>
> … the `do-while` construct is infrequently used in Processing (or in contemporary Java),

> <https://gist.github.com/GoToLoop/bba0c288aaeeb5ef9bb1#file-keyboard_input_library-pde>

In the sketch from the Gist link above, `do / while ()` was actually needed: ➰

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

```

B/c the loop needs to happen at least once, in order to ask an input from the user.  
Only then check whether it is valid or if the user wants to redo it.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 12, 2018, 10:56pm UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433/7 "2018-12-12T22:56:04Z")

</div>

> [@GoToLoop](#):
>
> B/c the loop needs to happen at least once, in order to ask an input from the user.

Great – that _is_ a classic use of do-while!

By coincidence, a key input validation loop is also the use case example given in the discussion I linked earlier:

> <https://stackoverflow.com/questions/994905/is-there-ever-a-need-for-a-do-while-loop/994937#994937>

…but in a simple Processing sketch with a `draw()` loop there is seldom call for a beginner to write code in that form, as you usually don’t want to block screen refresh while waiting on input. It isn’t _never_ useful – just rare, and even then achievable by other means.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [December 12, 2018, 11:12pm UTC](https://discourse.processing.org/t/what-is-the-keyword-do/6433/8 "2018-12-12T23:12:21Z")

</div>

![DuOVYe0UUAEBg_j](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ac9ecf2cd527c2e89f89b923507bab5f3a61be0c.jpeg)  
(from [https://twitter.com/PierreJoye/status/1072865391993479169](https://twitter.com/PierreJoye/status/1072865391993479169))
