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. for loop incrementing

for loop incrementing

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 9 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.
  • C Offline
    C Offline
    chaitanya22
    wrote on last edited by
    #1

    can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?

    N D R K C 5 Replies Last reply
    0
    • C chaitanya22

      can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      chaitanya22 wrote:

      such as for(i=1; i<=100;i+2)..

      should be : for(int i=1; i<100; i+=2){} Regards, Nish


      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
      The Ultimate Grid - The #1 MFC grid out there!

      1 Reply Last reply
      0
      • C chaitanya22

        can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?

        D Offline
        D Offline
        Divyang Mithaiwala
        wrote on last edited by
        #3

        chaitanya22 wrote:

        such as for(i=1; i<=100;i+2)..

        Hello chaitanya for(inintializer;condition;incrementer) Here in place of incrementer you can put any statement regards of assigment statement. It is not require that you use only a variable that is use in initializer place. C design a very rich for loop as you never seen before.


        Divyang Mithaiwala System Engineer & Software Developer

        1 Reply Last reply
        0
        • C chaitanya22

          can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?

          R Offline
          R Offline
          Rster021111
          wrote on last edited by
          #4

          You can even have multiple items in your loop, such as: for (int i = 2, j = 3; i < 5 && j < 10; i++, j+=3) da Big_R

          C 1 Reply Last reply
          0
          • C chaitanya22

            can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?

            K Offline
            K Offline
            Koushik Biswas
            wrote on last edited by
            #5

            If you do not want to make your for loop look anything other than the ordinary, but still want to skip processing the evens, use the continue keyword... for( i = 1; i <= 100; i++ ) { if( i % 2 == 0 ) continue; //.... } Koushik Biswas

            Steve EcholsS 1 Reply Last reply
            0
            • R Rster021111

              You can even have multiple items in your loop, such as: for (int i = 2, j = 3; i < 5 && j < 10; i++, j+=3) da Big_R

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              or...

              int b=0,c=0,q=60,_=q;for(float i=-20,o,O=0,l=0,j,p;j=O*O,p=l*l,
              (!_--|(j+p>4)?fputc(b?q+(_/3):10,(i+=!b,p=j=O=l=0,c++,stdout)),
              _=q:l=2*O*l+i/20,O=j-p+o),b=c%q,c<2400;o=-2+b*.05);

              Cleek | Image Toolkits | Thumbnail maker

              T 1 Reply Last reply
              0
              • C Chris Losinger

                or...

                int b=0,c=0,q=60,_=q;for(float i=-20,o,O=0,l=0,j,p;j=O*O,p=l*l,
                (!_--|(j+p>4)?fputc(b?q+(_/3):10,(i+=!b,p=j=O=l=0,c++,stdout)),
                _=q:l=2*O*l+i/20,O=j-p+o),b=c%q,c<2400;o=-2+b*.05);

                Cleek | Image Toolkits | Thumbnail maker

                T Offline
                T Offline
                ThatsAlok
                wrote on last edited by
                #7

                Chris Losinger wrote:

                int b=0,c=0,q=60,_=q;for(float i=-20,o,O=0,l=0,j,p;j=O*O,p=l*l,(!_--|(j+p>4)?fputc(b?q+(_/3):10,(i+=!b,p=j=O=l=0,c++,stdout)),_=q:l=2*O*l+i/20,O=j-p+o),b=c%q,c<2400;o=-2+b*.05);

                Ohh God! What will Happen to parsers:)

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

                cheers, Alok Gupta VC Forum Q&A :- I/ IV

                1 Reply Last reply
                0
                • K Koushik Biswas

                  If you do not want to make your for loop look anything other than the ordinary, but still want to skip processing the evens, use the continue keyword... for( i = 1; i <= 100; i++ ) { if( i % 2 == 0 ) continue; //.... } Koushik Biswas

                  Steve EcholsS Offline
                  Steve EcholsS Offline
                  Steve Echols
                  wrote on last edited by
                  #8

                  While this code works, it might not be as intuitive, and more importantly, efficient as: for (int i = 0; i < 100; i += 2) { // do something } Might not be important for small loops, but an extra if and % op can add up in big loops. - delete that;

                  • S
                    50 cups of coffee and you know it's on!
                    Code, follow, or get out of the way.
                  1 Reply Last reply
                  0
                  • C chaitanya22

                    can a FOr loop be incremented twice at a time? such as for(i=1; i<=100;i+2).. as i need to have only odd numbers. Is it correct?

                    C Offline
                    C Offline
                    chetan210183
                    wrote on last edited by
                    #9

                    Hi chaitanya, I hope the below link helps u. http://www.its.strath.ac.uk/courses/c/ Chetan. Helping others satisfies you...

                    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