just a ponder...

This is a discussion on just a ponder... within the C++ Programming forums, part of the General Programming Boards category; i always wanted to know - does Code: for(int i = 0;i < 3;i++){ //code } does the same as: ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    29

    just a ponder...

    i always wanted to know - does
    Code:
    for(int i = 0;i < 3;i++){
        //code
    }
    does the same as:

    Code:
    while(i<3){
        static int i = 0;
        //code
        i++;
    }
    thanks for the replys.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    no it does not - you check the condition i<3 outside the skope where i is defined
    the right version will be
    Code:
    {
       int i=0;
       while(i<3)
       {
         //code
          i++;
       }
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    ... not to mention a for statement doesn't leave a static hanging around.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    ... not to mention a for statement doesn't leave a static hanging around.
    Unless you're using MSVC 6.0 . . . This doesn' t either.
    Code:
    {
        int i = 0;
        while(i < 3) {
            // code
            i ++;
        }
    }
    But there's no reason you shouldn't use a for loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    > Unless you're using MSVC 6.0 . . . This doesn' t either.

    Hmm... now you lost me. Don't static variables only get destroyed after main()?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intelligent vs Smart
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-18-2004, 01:42 PM
  2. something to ponder
    By incognito in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 08:31 PM

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