# conceptualize loops, I seem to think about them backwards.

• March 4th, 2014, 12:51 AM
ggx7
conceptualize loops, I seem to think about them backwards.
So when I took a test on while loops, I guess I think about them backwards, for instance I put && for conditions, when I should've put ||. I also put in the exiting condition in the while, when I really should be putting a condition that will keep the user in the while loop. Can someone please explain scenarios and the how's and why's and why do you think I'm getting it screwed up. Or perhaps I should just study the book more. Sorry if I sound like a bumbling buffoon. I guess I talk to people because outside of school I don't really have a life being a full time student, lol.

Later I will put up things I got wrong on the test, if you guys don't mind looking them over... I've got to study for a statistics exam now, hope you guys have a great night.
• March 4th, 2014, 03:13 AM
GregBrannon
Re: conceptualize loops, I seem to think about them backwards.
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:

Each of the three loops has a condition, and (as you stated) for as long as (or while) the condition is true, the body of the loop will be executed. You seem to have that concept clear. When to use each:

for: when the number of times the body of the loop is to be run is known or can be calculated
while: when the number of times the body of the loop is to be run is uncertain or decided by the user
do/while: when the body of the loop must be run at least once but after that the number of times it is to be run is uncertain or decided by the user

Even though the above are generally true, they are guidelines rather than absolute rules, because any loop can be used in place of any of the others at any time.

As for boolean logic, greater confidence will come with experience, but even the most experienced and confident can write a boolean equation that is confusing. So rule number 1 is to keep the loop condition as simple as possible. Try to keep the number of conditions to 2 or less OR the number of boolean operators to one no matter how many conditions there are. Using the keep it simple approach, in order for the loop body to be executed when the loop conditions are combined with:

&&: ALL of the conditions must be true, or combined with
||: ANY of the conditions must be true

Yes, reviewing the material in your textbook may help. You might also look for online articles that discuss topics you're having trouble with, because sometimes seeing the topic explained differently may help you see the light.

Hope this helps. Good luck!