Results 1 to 20 of 33
Thread: how to use do while loops
- 09-20-2009, 11:44 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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 youLast edited by mikeitalydz; 09-20-2009 at 11:46 PM.
-
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.
- 09-21-2009, 12:25 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
i tried using print ("" +i )
i got the result like this :
123456789
but what i want is something like this
123
456
789
-
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.
- 09-21-2009, 08:57 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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.
- 09-21-2009, 09:56 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
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.
- 09-21-2009, 09:56 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
using for loop
Java Code:for(i=1;i<10;i++){ System.out.print(i); if(i%3==0) System.out.print("\n"); }
Last edited by hotinlavacoolinjava; 09-21-2009 at 10:53 AM. Reason: putting in code tags
- 09-21-2009, 10:00 AM #8
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
Thank you very much
- 09-21-2009, 10:15 AM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
- 09-21-2009, 11:20 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Especially when the proposed solution is not platform independent.
- 09-21-2009, 11:39 AM #11
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
for loop, while loop, do-while loop are the same
- 09-21-2009, 12:22 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
- 09-21-2009, 02:51 PM #13
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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…
- 09-21-2009, 03:06 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Breathe, mate...Breathe!
:)
- 09-21-2009, 04:23 PM #15
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
- 09-22-2009, 04:50 AM #16
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
yes i did read post number 6 thank you.
- 09-23-2009, 07:47 AM #17
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
Java Code:bool loop = true for (;; loop) { // code if (something) loop = false } while(loop) { // code if (something) loop = false; }
- 09-23-2009, 10:32 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.
- 09-23-2009, 10:43 AM #19
Member
- Join Date
- Sep 2009
- Posts
- 32
- Rep Power
- 0
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 10:45 AM.
- 09-24-2009, 12:53 AM #20
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,
DieterProgramming 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...
Similar Threads
-
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02-18-2009, 03:48 AM -
Question about loops
By BHCluster in forum New To JavaReplies: 4Last Post: 04-16-2008, 06:40 PM -
[SOLVED] Need help with Loops...please!
By Zebra in forum New To JavaReplies: 5Last Post: 04-10-2008, 02:44 PM -
Loops (while do etc)
By manupr in forum New To JavaReplies: 1Last Post: 01-15-2008, 04:59 AM -
Help me: loops in java
By silvia in forum New To JavaReplies: 3Last Post: 07-19-2007, 07:47 PM
Bookmarks