Results 1 to 17 of 17
Thread: do...while loop
- 01-28-2008, 08:23 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 49
- Rep Power
- 0
- 01-28-2008, 08:52 AM #2
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
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...
- 01-28-2008, 10:04 AM #3
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.
Gives the outputJava 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);
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.
- 01-29-2008, 09:02 AM #4
Member
- Join Date
- Dec 2007
- Location
- Mumbai, India
- Posts
- 37
- Rep Power
- 0
Eva,
We can do this in even simple way
int i=0;
do{
System.out.println(i);
}while(++i<21);
- 01-29-2008, 10:30 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
[QUOTE=tim;13228]
tim, why you use 'if' statement there. Because both 'if' and 'while' conditions are do the same. ;)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);
- 01-29-2008, 10:33 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-29-2008, 12:32 PM #7
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.
- 01-29-2008, 12:37 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No tim,
Just I'm worried there is any special reason to use that.:p
- 01-29-2008, 12:58 PM #9
Member
- Join Date
- Jan 2008
- Posts
- 1
- Rep Power
- 0
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.
- 01-29-2008, 01:02 PM #10
Welcome
Welcome to the forums shasaf.
You've got it spot on. That was exactly what I indented. :DEyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-30-2008, 09:13 AM #11
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
hehe
fighting over simple stuffs...:)
- 01-30-2008, 09:21 AM #12
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.
- 01-30-2008, 09:34 AM #13
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
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
- 01-30-2008, 09:34 AM #14
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
haha
wrong grammarrrr
- 01-30-2008, 09:50 AM #15
Member
- Join Date
- Dec 2007
- Posts
- 49
- Rep Power
- 0
Thanks for the discussion on different possibilities. Yes, Its a learning place where we improve our skills.
- PEACE
- 01-31-2008, 06:37 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. ;)
- 01-31-2008, 06:44 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Loop Help
By HeavyD in forum New To JavaReplies: 7Last Post: 09-22-2010, 09:55 PM -
while loop
By michcio in forum New To JavaReplies: 5Last Post: 01-27-2008, 12:56 AM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM -
A loop that doesn't loop
By MichYer in forum New To JavaReplies: 2Last Post: 07-30-2007, 08:44 AM -
While loop
By leebee in forum New To JavaReplies: 1Last Post: 07-18-2007, 03:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks