Results 1 to 17 of 17

Thread: do...while loop

  1. #1
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default do...while loop

    Another quick question:

    How can the following be done using do ... while loop? I hope it wont affect the efficiency of the program.

    Java Code:
                    int i=0;
    		
    		while(i<21)
    		{
    			System.out.println(i);
    			i++;
    		}

  2. #2
    kureikougaiji is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default im confused also

    i=0;

    do{

    System.out.println(i);
    i++

    }
    while(i<21);

    output:
    0
    1
    2
    .
    .
    .
    21

    it printed 21 before checking it...

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Do loops

    Hello

    You should add an extra check, since the condition for the do-while loop is at the end of the loop statement.
    Java Code:
    [COLOR="Teal"]int[/COLOR] i=0;
    do{
       [COLOR="Teal"]if (i < 21) [/COLOR]System.out.println(i);
       i++[COLOR="Teal"];[/COLOR]
    }
    while(i<21);
    Gives the output
    Java Code:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    felixtfelix is offline Member
    Join Date
    Dec 2007
    Location
    Mumbai, India
    Posts
    37
    Rep Power
    0

    Default

    Eva,
    We can do this in even simple way
    int i=0;
    do{
    System.out.println(i);
    }while(++i<21);

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    [QUOTE=tim;13228]
    Java Code:
    [COLOR="Teal"]int[/COLOR] i=0;
    do{
       [COLOR="Teal"]if (i < 21) [/COLOR]System.out.println(i);
       i++[COLOR="Teal"];[/COLOR]
    }
    while(i<21);
    tim, why you use 'if' statement there. Because both 'if' and 'while' conditions are do the same. ;)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by kureikougaiji View Post
    it printed 21 before checking it...
    Are you sure? It doesn't print 21 actually. Go through the code again. By the way, you don't have define the variable type of 'i'.

  7. #7
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Assumed

    Hello Eranga

    I assumed that what kureikougaiji said, was correct. I've compiled and ran the code and 21 was not printed. The if-statement is unnecessary.

    Thank you. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    shasaf is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default

    Hi Eranga,

    Basically what tim is trying to tell you is that the condition {i<21} in a do-while loop comes up at the end. What happens in this case is for the first time there is no condition to check and the data will get printed in any case. Only after the printing of data will the condition be checked. For instance if you had initiated i to 24 to start off, the piece of code without the if condition will print 24, and then check the condition. Hence only 24 will get printed. Infact the basic purpose of a do while loop is when you are sure that the piece of code has to get executed at least once.

  10. #10
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Welcome

    Welcome to the forums shasaf.

    You've got it spot on. That was exactly what I indented. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  11. #11
    kureikougaiji is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default hehe

    fighting over simple stuffs...:)

  12. #12
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Hello kureikougaiji.

    We do not fight on the forums. It is a place to learn and improve. I admitted my mistake and there is no reason to fight. :D

    I usually use an if statement in these situations to prevent logical errors, since algorithms are not always this simple. In my point of view- better safe than sorry. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  13. #13
    kureikougaiji is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default hmm

    yes, you're all right you know.. in programming, there is a vast ways on how to implement a problem.. sorry if i'm using the wrong words earlier..:) peace..:p

  14. #14
    kureikougaiji is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default haha

    wrong grammarrrr

  15. #15
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default

    Thanks for the discussion on different possibilities. Yes, Its a learning place where we improve our skills.

    - PEACE

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by shasaf View Post
    What happens in this case is for the first time there is no condition to check and the data will get printed in any case.
    First of all, welcome to the forum. :)

    Yes, you are right. But the reason is this. Normally in use of do-while loop is, do the processing first and then check the condition fired or not. If you want to check the condition first, you can use either 'if' or 'for' loops in different ways. I telling this is the normal practice I do, as well as most of the people. ;)

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by tim View Post
    Hello kureikougaiji.

    We do not fight on the forums. It is a place to learn and improve. I admitted my mistake and there is no reason to fight. :D

    I usually use an if statement in these situations to prevent logical errors, since algorithms are not always this simple. In my point of view- better safe than sorry. ;)
    Yes tim. Our community is the best place to learn and improved our knowledge. And also, regarding your coding practice, different people have there own methods to do it in safely. As you said, the way you explain is safe for you ;)

Similar Threads

  1. Loop Help
    By HeavyD in forum New To Java
    Replies: 7
    Last Post: 09-22-2010, 10:55 PM
  2. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 AM
  3. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:44 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 04:11 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
  •