You've asked about two different things, both important to the topic, boolean logic and loop concepts, and from what you've posted you seem to understand the looping part. Let's review that first:
...