While loop question

  • 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. :robot:
  • 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: :sunglasses:
  • Besides the inner block while ((age = readByte("Enter your age:")) < 0); it also has a variant called do {} while () as its outer block: :dizzy_face:
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. :thinking:
  • But for now I can only think about the 1 I’ve posted above. :upside_down_face:
2 Likes