problem with using for loops with Dev C++

This is a discussion on problem with using for loops with Dev C++ within the C++ Programming forums, part of the General Programming Boards category; when I am trying to put a for loop in Dev C++ I can't have just the initialization as Code: ...

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    problem with using for loops with Dev C++

    when I am trying to put a for loop in Dev C++ I can't have just the initialization as

    Code:
    (i = 1; i < 4; i++)
    I have to use

    Code:
    int i = 1; i < 4; i++
    small, but I'm so used to seeing it in the book that I forget it every time I write it myself.

    and it's me again.
    Last edited by indigo0086; 08-10-2005 at 07:28 PM.

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    was there a question hidden in that text?

    Code:
    for(int i = 0; i != 10; i++)
    {
         cout << i << endl;
    }

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    oh, nm.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Or, of course:
    Code:
    int i;
    for(i = 0; i != 10; i++)
    {
         cout << i << endl;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yeah, that's it.

    the book put it that way so I just forgot it.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    Using Zach's way, the variable 'i' can be used anywhere else in the main(). The other way... the variable is limited to thes scope which is the for loop.

    If u're using that variable just as a counter....then i prefer ILoveVectors' way. If using the variable somewhere else, then declare the variable efore.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quick add-on to Sridar's post:
    In general, I would recommend ILV's way (I was essentially just being obnoxious with my post). However, when I have a std:: container and I will need iterators a few times (independently of each other), I will declare one and then reuse it; I do not do this with integer counters. (This probably has as much to do with keeping my lines short as it does with the fact that you don't know exactly what goes into the construction of an iterator.)

    So, that was probably a useless comment, but anyway...


    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Also some compilers (VC++ 6.0) incorrectly treat ILoveVector's example like Zach L.'s example, so be careful if you are on that compiler, otherwise ILoveVector's example is generally preferred.

  9. #9
    Banned
    Join Date
    Jun 2005
    Posts
    594
    as far as im concerned there is no prefrence between the two,
    there are two diffrent things in my book.

    because i would use a different one depending ont he situation.

    Code:
    for(int i = 0; i != 10; i++)
    {
         cout << i << endl;
    }
    but i would do thsi if i was going to do say the following

    Code:
    int i = 0;
    for(; i != 10; i++)
    {
         cout << i << endl;
    }
    for(; i <= 20; i+=2)
    {
         cout << i << endl;
    }
    for(; i <= 200; i+=10)
    {
         cout << i << endl;
    }

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Or how about:
    Code:
    {
    
      int i = 0;
      for(; i != 10; i++)
      {
           cout << i << endl;
      }
      for(; i <= 20; i+=2)
      {
           cout << i << endl;
      }
      for(; i <= 200; i+=10)
      {
           cout << i << endl;
      }
    
    }
    Then 'i' goes out of scope after all the loops
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    stop it your scaring the new people...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. Problem running prog I wrote in Dev C++
    By KidMan in forum C Programming
    Replies: 8
    Last Post: 09-22-2005, 01:50 AM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21