Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: conceptualize loops, I seem to think about them backwards.

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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!

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    ggx7 (March 4th, 2014)

Similar Threads

  1. Print a given sentence forwards, backwards and every other word.
    By newbiegirl in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2013, 05:45 AM
  2. For Loops
    By Shyamz1 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 11:54 AM
  3. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  4. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  5. Program to take name as input and write it backwards
    By snake101 in forum Java Theory & Questions
    Replies: 7
    Last Post: June 10th, 2009, 04:35 AM