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

01-28-2008, 10:23 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 49
|
|
|
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.
int i=0;
while(i<21)
{
System.out.println(i);
i++;
}
|
|

01-28-2008, 10:52 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
|
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, 12:04 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
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.
int i=0;
do{
if (i < 21) System.out.println(i);
i++;
}
while(i<21);
Gives the output
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.
|
|

01-29-2008, 11:02 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 32
|
|
|
Eva,
We can do this in even simple way
int i=0;
do{
System.out.println(i);
}while(++i<21);
|
|

01-29-2008, 12:30 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
|
|
[quote=tim;13228]
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.
|
|

01-29-2008, 12:33 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
|
|
Originally Posted by kureikougaiji
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.
|
|

01-29-2008, 02:32 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
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.
|
|

01-29-2008, 02:37 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
|
|
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.
|
|

01-29-2008, 02:58 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 1
|
|
|
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, 03:02 PM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
|
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.
|
|

01-30-2008, 11:13 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
|
hehe
fighting over simple stuffs... 
|
|

01-30-2008, 11:21 AM
|
 |
Senior Member
|
|
Join Date: Dec 2007
Location: South Africa
Posts: 334
|
|
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.
|
|

01-30-2008, 11:34 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
|
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.. 
|
|

01-30-2008, 11:34 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
|
haha
wrong grammarrrr
|
|

01-30-2008, 11:50 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 49
|
|
|
Thanks for the discussion on different possibilities. Yes, Its a learning place where we improve our skills.
- PEACE
|
|

01-31-2008, 08:37 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
|
|
Originally Posted by shasaf
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.
|
|

01-31-2008, 08:44 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
|
|
Originally Posted by tim
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|