Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default how to use do while loops

    Hello guys i have one question here i was trying to generate a table and i used nested do while loops my main reason to use do while is to get an out put of the numbers from 1 to 9 at this way
    123
    456
    789
    when i tried do while loops i got a result like this
    1
    2
    3
    4
    5
    6
    8
    9
    my code is like this
    class test
    {
    public void static main (String args [])
    {
    int i = 1;
    do { System.out.println (i);
    i ++

    }while (i <10);

    }

    }
    thank you
    Last edited by mikeitalydz; 09-20-2009 at 10:46 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    System.out.println(...) always prints a new line after the String while
    System.out.print(...)

    You may want to play with nested for-loops here, not while-loops.

  3. #3
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    i tried using print ("" +i )
    i got the result like this :
    123456789
    but what i want is something like this
    123
    456
    789

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    again, use nested for loops (two of them in fact). So you know that you're going to need a few line feeds in there, and you know that println will give you the line feeds while print doesn't. With this information, please give it another try to figure it out. Again, look into using two for-loops, one inside of the other.

  5. #5
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    my question is i want to generate table using nested do while loops which the result will be like this
    123
    456
    789

    why i should use for-loop if there is a possibilty to do it with do-while loop?
    i tried for loop it didn't work with me i do not know why.
    please explaine to me why i can't use do while loop in this case
    thank you for your time and hlep . im looking forward to hear from you soon.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Ultimately you will need to learn all those loops. They are handy for different scenarios.
    Generally you would use the old for loop if you know how many times you need to loop while making use of the index of current position inside the loop.
    The new for each is when iterating through all the elements of a constant size collection.
    A while is more applicable when you don't know how many times you must loop through while a do-while is when you know that you must perform the loop action at least once.

  7. #7
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    using for loop

    Java Code:
    for(i=1;i<10;i++){
      System.out.print(i);
      if(i%3==0)
             System.out.print("\n"); 
    }
    It is preferable to use for-loop when you know when your loop stops, ie you know the number of iteration loops.
    Last edited by hotinlavacoolinjava; 09-21-2009 at 09:53 AM. Reason: putting in code tags

  8. #8
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    Thank you very much

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by hotinlavacoolinjava View Post
    using for loop

    for(i=1;i<10;i++){
    System.out.print(i);
    if(i%3==0)
    System.out.print("\n");
    }


    It is preferable to use for-loop when you know when your loop stops, ie you know the number of iteration loops.
    Would you please use code tags if you have to post code?
    Also spoon feeding is not very helpful at all. The OP doesn't learn anything from that.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,920
    Rep Power
    19

    Default

    Especially when the proposed solution is not platform independent.

  11. #11
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by mtyoung View Post
    for loop, while loop, do-while loop are the same
    No they are not. Read the response I posted about them above.

  13. #13
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    hey when hotinlavacoolinjava put the code in here i did not say i will take it it’s nice to see the difference and to have a hint but that doesn’t mean I will use it , my whole point is to have an idea and the answer why i can't use do while where there is a possibility of using it in this example and to say yes you can but use for loop!! The question always will come back to you why and why i shouldn’t , i did not come here to get a fresh hot code and leave because there is no way to me or anyone who is like me a new to java ,learn the language if he can't do an effort !! , I believe to know the difference between all types of repetition statements means you have to read and read and read and practice if you want to succeed and that what I’m doing .but we all get back to the same page no one of us was born professor no one is perfect we will always need help even if we are a masters in java…

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,920
    Rep Power
    19

    Default

    Breathe, mate...Breathe!
    :)

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by mikeitalydz View Post
    ... The question always will come back to you why and why i shouldn’t ...
    Read post number 6 in this thread.

  16. #16
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    yes i did read post number 6 thank you.

  17. #17
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    bool loop = true
    for (;; loop)
    {
     // code
     if (something)
       loop = false
    }
    
    while(loop)
    {
      // code
     if (something)
       loop = false;
    }
    so i think while, for are the same

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,920
    Rep Power
    19

    Default

    That's an abuse of code, though.
    I would get mt Big Red Pen out for that one if I saw it in a code review.

    The fact that you can make them work in similar ways does not mean they are the same.

    I can, after all, use a hammer on a screw and still join two bits of wood together, but that doesn't mean it's not wrong tool for the job.

  19. #19
    Join Date
    Sep 2009
    Posts
    32
    Rep Power
    0

    Default

    i think it should work i have not tested it but you can try
    static int i=1;

    do
    {
    int count=1;
    do
    {
    System.out.print(i);
    i++;
    count++
    }while(count<3)
    System.out.println();
    }while(i<10)
    Last edited by kirtichopra2003; 09-23-2009 at 09:45 AM.

  20. #20
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    this is an interesting topic...ssenior members fighting about loops :p think i will hang around in this topic for abit :D

    But they are right, i tried to do it with a while and a for and doing this with a for loop is much much easier and time saving.

    all the best,

    Dieter
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

Page 1 of 2 12 LastLast

Similar Threads

  1. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM
  2. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 05:40 PM
  3. [SOLVED] Need help with Loops...please!
    By Zebra in forum New To Java
    Replies: 5
    Last Post: 04-10-2008, 01:44 PM
  4. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 03:59 AM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •