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 7 of 7

Thread: Situations for using while loops, do while loops, and for while loops.

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

    Default Situations for using while loops, do while loops, and for while loops.

    while loops are great for when you don't know how many times the program will run, right?

    For example, a program that asks the user to do some calculation, then asks him/her to press 0 to quit. (am i right here?)

    do while loops are executed once, and are useful for data validation right? Can you possibly give a scenario/example of this?

    and for while loops are good for when you know the actual quantity or number of times you need to check something, right? Can someone give a scenario of this please?


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Situations for using while loops, do while loops, and for while loops.

    DO-WHILE

    do
    {
    statements;
    }while (condition);

    As the while loop, it can be counter-controlled or sentinel- controlled.
    The body of the loop continues to execute as long as the condition is true or until the condition becomes false.
    Any variables declared inside the do-while belongs to the do-while only.
    The condition has to become false sooner or later; otherwise, it’s an infinite loop.

    *This is a good loop to use with log-ins, with menus (list of options), or when you need to force the user into a loop*


    WHILE

    while is a pre-test loop that requires testing of the condition in the while header before the body of the loop is entered.

    while(condition)
    {
    statements;
    }

    The condition (also referred to as the test expression) tests the value in the variable that controls whether the loop continues or stops, aka, loop-control variable.

    *This is a good loop to use for testing the validity of input data.*

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

    ggx7 (March 5th, 2014)

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

    Default Re: Situations for using while loops, do while loops, and for while loops.

    Thanks, what about the FOR loop, isn't that useful as well? when you know the actual amount of times you need to check something? because of that simple part in the loop where you can easily incremement something?

    for example: for(i = 0; i <= 10; i++)//this will execute 10 times
    {
    System.out.println(i);
    }

  5. #4
    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: Situations for using while loops, do while loops, and for while loops.

    The best way to understand looping and the utility of each loop is to use them. Write a simple study program that does something mutiplie times, and complete the task using each kind of loop. That darned ascii art, pyramids and other things out of asterisks, is an excellent loop exercise and time waster.

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

    ggx7 (March 6th, 2014)

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

    Default Re: Situations for using while loops, do while loops, and for while loops.

    Thank you GregBrannon, not quite sure I know what you're talking about (ascii art, pyramid) Sorry. I'm still kind of new to forums so I don't really know how to navigate them that well. But thankfully I found out about this forum because everyone here has been really great

  8. #6
    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: Situations for using while loops, do while loops, and for while loops.

    Here's an example of an ASCII Art problem from "C: How to Program, 6th Edition," Deitel.

    4.16 (Triangle Printing Program) Write a program that prints the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks should be printed by a single printf statement of the form printf( "*" ); (this causes the asterisks to print side by side). [Hint: The last two patterns require that each line begin with an appropriate number of blanks.]
    (A)
     *
     **
     ***
     ****
     *****
     ******
     *******
     ********
     *********
     **********
     
     (B)
      **********
      *********
      ********
      *******
      ******
      *****
      ****
      ***
      **
      *
     
     (C)
      **********
       *********
        ********
         *******
          ******
           *****
            ****
             ***
              **
               *
     
     (D)
              *
             **
            ***
           ****
          *****
         ******
        *******
       ********
      *********
     **********
     
    Extra credit:
     
        *
       ***
      *****
     *******
    *********
     *******
      *****
       ***
        *

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

    ggx7 (March 7th, 2014)

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

    Default Re: Situations for using while loops, do while loops, and for while loops.

    Quote Originally Posted by GregBrannon View Post
    Here's an example of an ASCII Art problem from "C: How to Program, 6th Edition," Deitel.

    4.16 (Triangle Printing Program) Write a program that prints the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks should be printed by a single printf statement of the form printf( "*" ); (this causes the asterisks to print side by side). [Hint: The last two patterns require that each line begin with an appropriate number of blanks.]
    (A)
     *
     **
     ***
     ****
     *****
     ******
     *******
     ********
     *********
     **********
     
     (B)
      **********
      *********
      ********
      *******
      ******
      *****
      ****
      ***
      **
      *
     
     (C)
      **********
       *********
        ********
         *******
          ******
           *****
            ****
             ***
              **
               *
     
     (D)
              *
             **
            ***
           ****
          *****
         ******
        *******
       ********
      *********
     **********
     
    Extra credit:
     
        *
       ***
      *****
     *******
    *********
     *******
      *****
       ***
        *
    Hi Greg, could you please see my code located here: http://www.javaprogrammingforums.com...umns-form.html

    I have to do something similar to this, and my code is listed there as well.

Similar Threads

  1. Loops
    By user2345 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2014, 12:15 PM
  2. Need Help! For loops and if/else
    By kram in forum Loops & Control Statements
    Replies: 2
    Last Post: February 11th, 2012, 06:22 PM
  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 kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2011, 11:42 PM
  5. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM