CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: for loops

  1. #1
    Join Date
    Feb 2005
    Posts
    2

    Angry for loops

    I am very new to this and would like a little help. I understand that the for loop has a declaration, a test and an incrementer. What I am trying to do below is print a entered string in reverse.

    I keep getting an out of bounds exception because I am reaching 0, the next step being -1.

    I just can't seem to work this out. Could someone help please?

    This is my code.

    for (i=length-1;i<=length;i--)

    {

    mychar=wordString.charAt(i);

    System.out.println(""+mychar);

    System.out.println (""+i);

    }

    My apologies if this is a stupid question
    Cheers
    Harlequeen

  2. #2
    Join Date
    Nov 2004
    Posts
    6

    Re: for loops

    Should be:

    for (i=length-1; i>=0; i--)

    {

    mychar=wordString.charAt(i);

    System.out.println(""+mychar);

    System.out.println (""+i);

    }

    You are starting at the back of the string, and decrementing i, so the test is that the loop should continue while i is greater than or equal to zero.

    HTH,
    Matt

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: for loops

    This is surely another example where 30 seconds stepping through the code with pencil and paper would spot the error.

    You don't need a keyboard and screen to write or debug code. Quite often, they are a positive distraction...

    A pencil and paper are the best programming tools out there...
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: for loops

    StringBuffer.reverse()
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: for loops

    Quote Originally Posted by harlequeen
    I am very new to this and would like a little help. I understand that the for loop has a declaration, a test and an incrementer. What I am trying to do below is print a entered string in reverse.

    I keep getting an out of bounds exception because I am reaching 0, the next step being -1.

    I just can't seem to work this out. Could someone help please?

    This is my code.

    for (i=length-1;i<=length;i--)

    {

    mychar=wordString.charAt(i);

    System.out.println(""+mychar);

    System.out.println (""+i);

    }

    My apologies if this is a stupid question
    Cheers
    Harlequeen
    Which is not surprising. When you write "i<=length", you're giving your loop an upper bound (which is completely unnecessary in this particular instance), but not a lower bound. A lower bound is important here since you're decrementing here from a set point to zero.

    You're getting this particular error, because there is no lower bound and as a result you're going into the negatives (and there is no way that an array can have a negative position.) Instead of "i<=length", try "i >= 0".
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  6. #6
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    48

    Re: for loops

    @dlorde:

    A pencil and paper are the best programming tools out there...
    i like the way you approach erroneous code, and i think this way in the most problem cases, but there are few failures, where i wont miss my debugger
    Peter
    _______________________
    Homer: Okay, brain. You don't like me, and I don't like you, but let's get through this thing and then I can continue killing you with beer.
    Homer's Brain: It's a deal!

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: for loops

    i'd also like to note that:

    i <= length


    shouldnt be used for upper-bounding an array, because an array of length 10, actually has elements 0 to 9. if youre testing for lessthan or EQUAL to 10, then at some point, you will attempt to access array[10], which is out of bounds


    standard loops for iterating an array forwards and reverse:

    for(int i=0; i<array.length; i++)
    for(int i=array.length-1; i>=0; i--)
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: for loops

    Quote Originally Posted by universer
    but there are few failures, where i wont miss my debugger
    I must admit, it's a partly case of do as I say, not as I do
    I use the debugger constantly - but I do always have a pencil and paper to hand, because I believe it's hard to use a debugger effectively without them. With experience, many of the simple bugs like off-by-one, it's-null-but-it-shouldn't-be, == not =, and == not equals() errors happen less often, and are easier to spot when they do, so student programmers have even more to gain more from p&p than experienced programmers - it took me some time to realise this when I was a student, so I try to pass it on.

    Necessity is the mother of invention. Laziness is the mother of necessity...
    Anon Perl Hacker?

    So it follows: the grandmother of invention is laziness...
    Donald Noyes
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: for loops

    Quote Originally Posted by dlorde
    I must admit, it's a partly case of do as I say, not as I do
    I use the debugger constantly - but I do always have a pencil and paper to hand, because I believe it's hard to use a debugger effectively without them. With experience, many of the simple bugs like off-by-one, it's-null-but-it-shouldn't-be, == not =, and == not equals() errors happen less often, and are easier to spot when they do, so student programmers have even more to gain more from p&p than experienced programmers - it took me some time to realise this when I was a student, so I try to pass it on.
    That's actually quite true, especially when you're messing around with pointers.
    Quote Originally Posted by dlorde
    Necessity is the mother of invention. Laziness is the mother of necessity...
    Anon Perl Hacker?

    So it follows: the grandmother of invention is laziness...
    Donald Noyes
    rofl Nice .
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  10. #10
    Join Date
    Feb 2005
    Posts
    2

    Re: for loops

    thanks for your help. I did, in fact, find the answer myself when I gave myself a break and then went back. As I said, I'm very new to this, so failing to get something right is not stranger to me at the moment.

    I do try and use pen and paper to follow the progression of a program, but I got bogged down.

    Anyway, thanks for your help and your comments, I am finding out that trial and error is a great learning tool

    Cheers

    Harlequeen

  11. #11
    Join Date
    May 2004
    Posts
    45

    Re: for loops

    Some hints (or cookie recipes):

    To generate the sequence 0, 1, 2, ... n-1:
    for (int i = 0; i < n; ++i) {
    }

    To generate the sequence 1, 2, ... n:
    for (int i = 1; i <= n; ++i) {
    }

    To generate the sequence n-1, n-2, .... 2, 1, 0:
    for (int i = n - 1; i >= 0; --i) {
    }

    To generate the sequence n, n-1, .... 2, 1:
    for (int i = n; i >= 1; --i) {
    }

    To generate the sequence 1, 3, 5, 7, ... 99:
    for (int i = 1; i < 100; i += 2) {
    }

    To generate the sequence 0, 2, 4, 6, 8, .... 100:
    for (int i = 0; i <= 100; i += 2) {
    }

    If you understand and memorize some of those patterns above, you'll never have problems with "for loops" biting you.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured