What is the keyword "do"


#1

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.


#2

#3

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


#4

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


#5

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

#6

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.


#7

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.


#8


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