Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. THE USE OF "cout statement inside the for loop"

THE USE OF "cout statement inside the for loop"

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformance
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phijophlip
    wrote on last edited by
    #1

    Hello Friends, Please observe the code which is given below #include int main() { for(int i=0 ; i<=10 ;i++ , cout << i << endl ); cout << endl; return 0; } Please observe the the second which is given below #include int main() { for(int j=0 ; j<=10 ;j++ ) cout << j << endl; return 0; } My question is how is it possible to use cout in the inside the for loop.Is their any difference in the between the two program? Can anyone point the difference in terms of cpu speed,memory management etc.What is advantage of writing the code as you saw in the first section of the code ie the first program Hoping for a reply. Best Regards, Phijo :)

    S N N D 4 Replies Last reply
    0
    • P phijophlip

      Hello Friends, Please observe the code which is given below #include int main() { for(int i=0 ; i<=10 ;i++ , cout << i << endl ); cout << endl; return 0; } Please observe the the second which is given below #include int main() { for(int j=0 ; j<=10 ;j++ ) cout << j << endl; return 0; } My question is how is it possible to use cout in the inside the for loop.Is their any difference in the between the two program? Can anyone point the difference in terms of cpu speed,memory management etc.What is advantage of writing the code as you saw in the first section of the code ie the first program Hoping for a reply. Best Regards, Phijo :)

      S Offline
      S Offline
      Serge Krynine
      wrote on last edited by
      #2

      1. Yes it is possible to use cout inside the for loop. 2. The fragments are not the same: the first one prints 1 through 11 with a new line at the end; the second prints 0 through 10. 3. The pseudo code for the ‘for’ loop: for (expression1; expression2; expression3) { Body; } is somewhat like this: EXECUTE expression1; LABEL1: IF expression2 is FALSE GOTO LABEL2 EXECUTE Body; EXECUTE Eexpression3 GOTO LABEL1 LABEL2: This pseudo code should answer why the fragments do not provide the same results. Note that the 1st fragment has an empty body. 4. From CPU usage point of view, the 1st one executes 11 'cout << i' operations and 12 times flushes the buffer (‘cout << endl’), whereas the 2nd one - 11 and 11 respectively, so the second one is faster. 5. From the code size point of view, the 2nd one is smaller as it contains 2 cout calls, whereas the 1st one – 3. 6. It is hard to say about advantages as the two are not equivalent from the point of view of producing the result. Serge

      1 Reply Last reply
      0
      • P phijophlip

        Hello Friends, Please observe the code which is given below #include int main() { for(int i=0 ; i<=10 ;i++ , cout << i << endl ); cout << endl; return 0; } Please observe the the second which is given below #include int main() { for(int j=0 ; j<=10 ;j++ ) cout << j << endl; return 0; } My question is how is it possible to use cout in the inside the for loop.Is their any difference in the between the two program? Can anyone point the difference in terms of cpu speed,memory management etc.What is advantage of writing the code as you saw in the first section of the code ie the first program Hoping for a reply. Best Regards, Phijo :)

        N Offline
        N Offline
        namaskaaram
        wrote on last edited by
        #3

        well first thingz first!!!!!... both of them should work..... and the first example program that u have shown is a BAD style of writing the code..... both ur programz WILL GIVE DIFFERENT OUTPUTZ!!!!! ur first program is like thiz.....

        #include

        int main()
        {
        for(int i=0 ; i<=10 ;i++ , cout << i << endl );

        cout << endl;

        return 0;
        }

        the OUTPUT TO THE FIRST ONE SHOULD BE..... 1 2 3 4 5 6 7 8 9 10 11 OBSERVE..... the third argument that u give in the for loop is basically executed only AFTER the whole loop is done.....since i++is incremented first the output changez and is different from the second program the second program that u have shown ......

        #include

        int main()
        {

        for(int j=0 ; j<=10 ;j++ )
        cout << j << endl;

        return 0;
        }

        the OUTPUT TO THE FIRST ONE SHOULD BE..... 0 1 2 3 4 5 6 7 8 9 10 i dont see any advantages in writing the first way.....what i think is that itz a bad style ...... as far as memory and processor speedz concern.....i think u wouldnt find much of a any difference..... "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

        1 Reply Last reply
        0
        • P phijophlip

          Hello Friends, Please observe the code which is given below #include int main() { for(int i=0 ; i<=10 ;i++ , cout << i << endl ); cout << endl; return 0; } Please observe the the second which is given below #include int main() { for(int j=0 ; j<=10 ;j++ ) cout << j << endl; return 0; } My question is how is it possible to use cout in the inside the for loop.Is their any difference in the between the two program? Can anyone point the difference in terms of cpu speed,memory management etc.What is advantage of writing the code as you saw in the first section of the code ie the first program Hoping for a reply. Best Regards, Phijo :)

          N Offline
          N Offline
          normanS
          wrote on last edited by
          #4

          An interesting question. I would guess (but it is only a guess) that there will be no difference, i.e. the compiler will reduce them to about the same code, so the CPU speed, memory management, etc will all be about the same. ((I stand corrected, having seen the other replies. Unfortunately I could not do the obvious check - running them - since my office PC does not have C++ installed. I think the rest of the comments below are still valid, however!)) To find differences in timing, you could use both approaches, but check performance using the profiler. This may force you to use debug version, and turn off optimisation, so it may give you misleading results. To check memory management, etc, you could compile with the "generate assembler listing" option ON, and compare the resulting listings.

          1 Reply Last reply
          0
          • P phijophlip

            Hello Friends, Please observe the code which is given below #include int main() { for(int i=0 ; i<=10 ;i++ , cout << i << endl ); cout << endl; return 0; } Please observe the the second which is given below #include int main() { for(int j=0 ; j<=10 ;j++ ) cout << j << endl; return 0; } My question is how is it possible to use cout in the inside the for loop.Is their any difference in the between the two program? Can anyone point the difference in terms of cpu speed,memory management etc.What is advantage of writing the code as you saw in the first section of the code ie the first program Hoping for a reply. Best Regards, Phijo :)

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            A for statement is comprised of three expressions: init, condition, and loop. All three are optional. The init expression is evaluated once. The condition expression is evaluated once before each iteration of the loop. The loop expression is evaluated once after each iteration of the loop.


            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups