Results 21 to 33 of 33
Thread: how to use do while loops
- 09-24-2009, 05:30 AM #21
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
This kind of depends on your language: Having cut my teeth in C, I know that
for(initialize, modify loop variable, evaluate loop variable) is the same as
initialize
while(evaluate loop variable){
code
modify loop variable
}
I asked an instructor for permission to take an assembly language course when my formal background was limited to self instruction and some microprocessor courses. I failed the question "What is a for loop?" when I told him it was a while loop with extra bookkeeping. They were using Pascal in entry level course, where a for was essentially a counting loop. Complained to the Dean, and got into the course. Of course, there is the for(each element in : set{}) type of loop, yet another animal.
Had another question in a class - What is the most important structured programming construct? My answer - the if and goto, because you can make any of the other structures with these two statements (the way it is done at the assembly level). Just takes some discipline.
- 09-24-2009, 10:04 AM #22
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
As I said on the other thread on loops, if you used a while loop as a stand in for a for loop I'd raise that as a problem in a code review.
For loops mean something.
While loops mean something else.
Do while loops something else again.
Misusing them results in reduced code clarity, and that's a Bad Thing.
Knowing assembler is useful. It helps when figuring out why certain things are happening, but to use that as the basis for writing higher level languages is not good. You need to stop thinking about what's going on under the hood to some extent, especially if that results in you writing anything involving a goto...:)
- 09-24-2009, 05:05 PM #23
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
If you want to use do-while, here is an example
Java Code:public static void main(String[] args) { int count = 1; do{ System.out.print(count); if((count%3)==0){ System.out.println(); } ++count; }while(count <= 9); }
- 09-24-2009, 09:08 PM #24
Member
- Join Date
- Sep 2009
- Posts
- 1
- Rep Power
- 0
Hei Guys,
Who can help me to finish one project in Bluej i need it for school deadline is 1st october, anyone who can help
- 09-24-2009, 11:06 PM #25
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
- 09-25-2009, 02:06 AM #26
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
I am not suggesting writing code that way, merely trying to raise some points for the sake of discussion. C is notorious for obscure code. You can use a C for code to write a while loop be using comparisons and updates of loop variables that don't involve counters. That's no better.
That said, I have seen authors state that using a goto to exit a complex decision structture can be appropriate, as long as you go only to the end of the enclosing structure without entering another control structure. It can simplify the code (make it more readable, simpler to understand), and will confess that I've done it.
I've seen too much code with multiple returns in the same function. IMHO, this is a greater violation of the stuctured code principle of one entrance, one exit. I've been bitten when I didn't do all the clean up code on all exits of some else's code, and rewrote the code to avoid the situation.
- 09-25-2009, 08:27 AM #27
- 09-25-2009, 09:30 AM #28
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Obscure code in any language these days is a bad thing, frankly. Obscurity is the bane of maintainability.
Doesn't make them right. Decision structures in an OO environment do not require the use of explicit gotos to make them readable. If you find that a goto makes something more intelligible then the odds are that you've written it badly in the first place.
Multiple returns are not a problem in many instances. They are, in fact, a lot easier on readability and maintainability (again in many instances).
In long methods with a bunch of nesting they can cause confusion, however I would argue that it is far more likely that you've written a method that is doing too much and ought to look at reworking that code rather than falling back on deep nesting and flags.
- 09-26-2009, 05:06 PM #29
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
this is me again well i tried to use do- while loop
and the code i wrote is like this :
public static void main(String[] args)
{
int i =1;
do {System.out.print(" " +i);
i++;
}while (i<4);
System.out.print("\n");
int j =4;
do {System.out.print(" " +j);
j++;
}while (j<7);
System.out.print("\n");
int k =7;
do {System.out.print(" "+k);
k++;
}while (k<10);
System.out.print("\n");
and the result is :
123
456
789
- 09-26-2009, 05:23 PM #30
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
how about if we want to display the number reverse like this :
987
654
321?
-
Your solution above does not use nested loops, and I am wondering why you are avoiding this. I know that at least one solution using nested loops has already been posted in this thread.
- 09-26-2009, 08:13 PM #32
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
i just want to share that with every one i know nested loops easier and much cleaner but my point there is a possibility of getting a result in many diffrent ways.
-
It's just that your solution is quite rigid. For instance, if you solved it like so (as demonstrated similarly previously I believe)
You could change this to output this:Java Code:int i = 1; while (i < 10) { System.out.print(i); if (i % 3 == 0) { System.out.println(); } i++; }
987
765
321
by just adding 3 characters to the above code. That's it. In fact I'll tell you that you would add 10- but I'll leave it to you to find where.
Similar Threads
-
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02-18-2009, 02:48 AM -
Question about loops
By BHCluster in forum New To JavaReplies: 4Last Post: 04-16-2008, 05:40 PM -
[SOLVED] Need help with Loops...please!
By Zebra in forum New To JavaReplies: 5Last Post: 04-10-2008, 01:44 PM -
Loops (while do etc)
By manupr in forum New To JavaReplies: 1Last Post: 01-15-2008, 03:59 AM -
Help me: loops in java
By silvia in forum New To JavaReplies: 3Last Post: 07-19-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks