Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    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.

  2. #22
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default

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

  3. #23
    pkang is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Thumbs up

    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);
    
    	}

  4. #24
    fatoniv is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default

    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

  5. #25
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by fatoniv View Post
    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
    1. Create a new topic
    2. What is your assignment?
    3. We don't do your homework.
    I die a little on the inside...
    Every time I get shot.

  6. #26
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    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.

  7. #27
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Quote Originally Posted by fatoniv View Post
    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
    lol good job :p
    Programming 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...

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default

    Quote Originally Posted by rdtindsm View Post
    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.
    Obscure code in any language these days is a bad thing, frankly. Obscurity is the bane of maintainability.

    Quote Originally Posted by rdtindsm View Post
    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.
    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.

    Quote Originally Posted by rdtindsm View Post
    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.
    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.

  9. #29
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    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

  10. #30
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    how about if we want to display the number reverse like this :
    987
    654
    321?

  11. #31
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  12. #32
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    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.

  13. #33
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's just that your solution is quite rigid. For instance, if you solved it like so (as demonstrated similarly previously I believe)
    Java Code:
        int i = 1;
        while (i < 10) {
          System.out.print(i);
          if (i % 3 == 0) {
            System.out.println();
          }
          i++;
        }
    You could change this to output this:
    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.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM
  2. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 05:40 PM
  3. [SOLVED] Need help with Loops...please!
    By Zebra in forum New To Java
    Replies: 5
    Last Post: 04-10-2008, 01:44 PM
  4. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 03:59 AM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •