for( int i = 0; i < 5; i++ )

This is a discussion on for( int i = 0; i < 5; i++ ) within the C++ Programming forums, part of the General Programming Boards category; Code: for( int i = 0; i < 5; i++ ) { cout << "You suck!!!" << endl; } for( ...

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    for( int i = 0; i < 5; i++ )

    Code:
    for( int i = 0; i < 5; i++ )
    {
          cout << "You suck!!!" << endl;
    }
    
    for( int i = 0; i < 5; i++ )
    {
          cout << "You suck!!!" << endl;
    }
    That is possible in Java but in C++ you have to do this way:
    Code:
    for( int i = 0; i < 5; i++ )
    {
          cout << "You suck!!!" << endl;
    }
    
    for( int j = 0; j < 5; j++ )
    {
          cout << "You suck!!!" << endl;
    }
    or
    Code:
    int i;
    for( i = 0; i < 5; i++ )
    {
          cout << "You suck!!!" << endl;
    }
    
    for( i = 0; i < 5; i++ )
    {
          cout << "You suck!!!" << endl;
    }
    C++ approach for this problem sucks. So is there any reason for this annoying problem? Is this a C++ bug???

    Please, guyz!!! I am not Java troll........
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    7
    There must be something wrong with your compiler. Your first example is perfectly fine C++ code; the variable i loses scope as soon as the for loop terminates. Try running it again.

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    oh, yeah, you right. I never check it before. I thought it cann't because variable i cannot be use after for statement.

    The dumbest post I have ever made.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Ok, since I made the dumbest post, I have to fix it. Consider this code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       for( int i = 0; i < 4; i++ )
       {
          cout << "You suck" << endl;
       }
    
       f = 9;
    }
    You got this error:
    test.cpp: In function `int main()':
    test.cpp:11: error: `f' undeclared (first use this function)
    test.cpp:11: error: (Each undeclared identifier is reported only once for each
    function it appears in.)
    This is logic just as in Java with this code:
    Code:
    public class Tess
    {
       public static void main( String[] args )
       {
          for( int i = 0; i < 4; i++ )
          {
             System.out.println( "Damned" );
          }
          f = 9;
       }
    }
    You got this error. Just same with C++. Only different language.
    Tess.java:9: cannot resolve symbol
    symbol : variable f
    location: class Tess
    f = 9;
    ^
    1 error
    But if you use this code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       for( int i = 0; i < 4; i++ )
       {
          cout << "You suck" << endl;
       }
    
       i = 9;
    }
    You got unusual error. You didn't get this error:
    test.cpp: In function `int main()':
    test.cpp:11: error: `i' undeclared (first use this function)
    test.cpp:11: error: (Each undeclared identifier is reported only once for each
    function it appears in.)
    But you got this error:
    test.cpp: In function `int main()':
    test.cpp:11: error: name lookup of `i' changed for new ISO `for' scoping
    test.cpp:6: error: using obsolete binding at `i'

    But in Java, the error is still the same with this code:
    Code:
    public class Tess
    {
       public static void main( String[] args )
       {
          for( int i = 0; i < 4; i++ )
          {
             System.out.println( "Damned" );
          }
          i = 9;
       }
    }
    Tess.java:9: cannot resolve symbol
    symbol : variable i
    location: class Tess
    i = 9;
    ^
    1 error

    Is there any good reason C++ handle this i different than Java?
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gandalf_bar
    oh, yeah, you right. I never check it before. I thought it cann't because variable i cannot be use after for statement.

    The dumbest post I have ever made.
    I'll have to agree. Dumbest. Post Ever. You start a thread to complain about a problem that you haven't even compiled to see if it exists? What, you just made up some s... to complain about, even though you had never even TRIED to compile your supposed "C++ bug".

    Not a java troll? Maybe. But definately a dumbass.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    all your c++ compiler is saying to you is that it sees that you are trying to use an out of scope variable and the java one isn't
    Woop?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gandalf_bar
    Is there any good reason C++ handle this i different than Java?
    Yeah, one is C++ and one is Java. Two different languages.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Hey, quzah...... take it easy.....
    please look at the post before your post.......
    calm down. There is no need to waste energy with my dumbness.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Actually, it WAS the case that the original code would have a problem - the new C++ standard changed the scope rules in this particular instance.

    That's why GCC has this handy compiler option
    -ffor-scope
    -fno-for-scope
    If -ffor-scope is specified, the scope of variables declared in a for-init-statement is limited to the for loop itself, as specified by the C++ standard. If -fno-for-scope is specified, the scope of variables declared in a for-init-statement extends to the end of the enclosing scope, as was the case in old versions of G++, and other (traditional) implementations of C++.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    01000011 00100000 0010000
    Join Date
    Jul 2004
    Posts
    38
    Quote Originally Posted by gandalf_bar
    Ok, since I made the dumbest post, I have to fix it. Consider this code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       for( int i = 0; i < 4; i++ )
       {
          cout << "You suck" << endl;
       }
    
       f = 9;
    }
    You got this error:
    test.cpp: In function `int main()':
    test.cpp:11: error: `f' undeclared (first use this function)
    test.cpp:11: error: (Each undeclared identifier is reported only once for each
    function it appears in.)
    This is logic just as in Java with this code:
    Code:
    public class Tess
    {
       public static void main( String[] args )
       {
          for( int i = 0; i < 4; i++ )
          {
             System.out.println( "Damned" );
          }
          f = 9;
       }
    }
    You got this error. Just same with C++. Only different language.
    Tess.java:9: cannot resolve symbol
    symbol : variable f
    location: class Tess
    f = 9;
    ^
    1 error
    But if you use this code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       for( int i = 0; i < 4; i++ )
       {
          cout << "You suck" << endl;
       }
    
       i = 9;
    }
    You got unusual error. You didn't get this error:
    test.cpp: In function `int main()':
    test.cpp:11: error: `i' undeclared (first use this function)
    test.cpp:11: error: (Each undeclared identifier is reported only once for each
    function it appears in.)
    But you got this error:
    test.cpp: In function `int main()':
    test.cpp:11: error: name lookup of `i' changed for new ISO `for' scoping
    test.cpp:6: error: using obsolete binding at `i'

    But in Java, the error is still the same with this code:
    Code:
    public class Tess
    {
       public static void main( String[] args )
       {
          for( int i = 0; i < 4; i++ )
          {
             System.out.println( "Damned" );
          }
          i = 9;
       }
    }
    Tess.java:9: cannot resolve symbol
    symbol : variable i
    location: class Tess
    i = 9;
    ^
    1 error

    Is there any good reason C++ handle this i different than Java?
    Okay, I think the reason you are getting the error is because the variable i in the for loop only has scope in the for loop. This is because local variables only have scope inside their functions, and that means they are deleted (more specifically their memory is released or unallocated) after they are finished in the function. The for loop is using i. It declares it in its condition statement. Therefore when the for loop is finished, the i variable does not exist any more. The only way that code would work is if you declared i outside of the for loop. Example:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int i;
    
       for( int i = 0; i < 4; i++ )
       {
          cout << "You suck" << endl;
       }
    
       i = 9;
    }
    I might be wrong though. Not sure :P.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    7
    To answer your question about why you get a different error when running the code in c++ vs. running it in Java, it's because your c++ debugger does more work for you; it is certainly not intrinsic to the language. To explain, each compiler is encountering the same problem, namely that you are using a variable without declaring it, and indeed the first few lines of the c++ error message are identical when the variable i and the variable f are used without declaration. In the former case, the debugger takes the extra step of deducing why you made the mistake, by noting that the variable was declared inside the for loop and guessing that you got confused. So the phenomenon that you're inquiring about occurs because your c++ debugger is better than your java debugger.

  12. #12
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Thank you, guyz! Now I understand it very much......
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <( ' '<) Simple Programming Question?
    By strigen in forum C Programming
    Replies: 1
    Last Post: 03-05-2009, 02:17 PM
  2. x = x < y < 2; WHY IS THIS ALWAYS TRUE!?
    By sh4k3 in forum C Programming
    Replies: 5
    Last Post: 06-08-2007, 01:00 AM
  3. i < x or i != x?
    By OnionKnight in forum C Programming
    Replies: 5
    Last Post: 03-04-2006, 09:59 PM
  4. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 01:06 PM

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