Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 10:23 AM
eva eva is offline
Member
 
Join Date: Dec 2007
Posts: 49
eva is on a distinguished road
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.

Code:
int i=0; while(i<21) { System.out.println(i); i++; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-28-2008, 10:52 AM
Member
 
Join Date: Nov 2007
Posts: 15
kureikougaiji is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 12:04 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
Code:
int i=0; do{ if (i < 21) System.out.println(i); i++; } while(i<21);
Gives the output
Code:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-29-2008, 11:02 AM
Member
 
Join Date: Dec 2007
Posts: 32
felixtfelix is on a distinguished road
Eva,
We can do this in even simple way
int i=0;
do{
System.out.println(i);
}while(++i<21);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-29-2008, 12:30 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
[quote=tim;13228]
Code:
int i=0; do{ if (i < 21) System.out.println(i); i++; } while(i<21);
tim, why you use 'if' statement there. Because both 'if' and 'while' conditions are do the same.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-29-2008, 12:33 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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'.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-29-2008, 02:32 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-29-2008, 02:37 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
No tim,

Just I'm worried there is any special reason to use that.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-29-2008, 02:58 PM
Member
 
Join Date: Jan 2008
Posts: 1
shasaf is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-29-2008, 03:02 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Welcome
Welcome to the forums shasaf.

You've got it spot on. That was exactly what I indented.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-30-2008, 11:13 AM
Member
 
Join Date: Nov 2007
Posts: 15
kureikougaiji is on a distinguished road
hehe
fighting over simple stuffs...
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-30-2008, 11:21 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.

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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 01-30-2008, 11:34 AM
Member
 
Join Date: Nov 2007
Posts: 15
kureikougaiji is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 01-30-2008, 11:34 AM
Member
 
Join Date: Nov 2007
Posts: 15
kureikougaiji is on a distinguished road
haha
wrong grammarrrr
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 01-30-2008, 11:50 AM
eva eva is offline
Member
 
Join Date: Dec 2007
Posts: 49
eva is on a distinguished road
Thanks for the discussion on different possibilities. Yes, Its a learning place where we improve our skills.

- PEACE
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 01-31-2008, 08:37 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 01-31-2008, 08:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

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
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
while loop michcio New To Java 5 01-27-2008 02:56 AM
can you help me with this for loop? java_fun2007 New To Java 6 12-22-2007 12:20 PM
A loop that doesn't loop MichYer New To Java 2 07-30-2007 10:44 AM
While loop leebee New To Java 1 07-18-2007 05:11 PM
Loop Help HeavyD New To Java 5 07-10-2007 03:26 PM


All times are GMT +3. The time now is 01:58 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org