What is the keyword "do"

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.

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

The devs arbitrarily decide what goes to the reference page. :crazy_face:

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 . While do-while can fulfill a specific niche need, some people avoid do-while as a question of style.

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

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

  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.

1 Like

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:

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

1 Like

DuOVYe0UUAEBg_j
(from https://twitter.com/PierreJoye/status/1072865391993479169)

3 Likes