Thread: just a ponder...

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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++;
       }
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    ... not to mention a for statement doesn't leave a static hanging around.
    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,057
    ... 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
    Ireland
    Posts
    8,446
    > 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()?
    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, 09:31 PM