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. Meaning

Meaning

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

    What would the following conditional (i+n) in a for loop evaluate to? for (i=0; i + n; i--) I saw it once, but I dont understand what was happening. Did I post well? Rate it! Did I post badly? Rate that too!

    D T T 3 Replies Last reply
    0
    • E Esmo2000

      What would the following conditional (i+n) in a for loop evaluate to? for (i=0; i + n; i--) I saw it once, but I dont understand what was happening. Did I post well? Rate it! Did I post badly? Rate that too!

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

      0 if n starts at 0. Otherwise non-zero, thus the for loop would never terminate.


      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

      E C 2 Replies Last reply
      0
      • D David Crow

        0 if n starts at 0. Otherwise non-zero, thus the for loop would never terminate.


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        E Offline
        E Offline
        Esmo2000
        wrote on last edited by
        #3

        I want to believe you, but I cannot. int k = 0; int i, n=100; for (i=0; i + n; i--) k++; When this is done, k will equal 100. Can you explain? Did I post well? Rate it! Did I post badly? Rate that too!

        C D 2 Replies Last reply
        0
        • E Esmo2000

          I want to believe you, but I cannot. int k = 0; int i, n=100; for (i=0; i + n; i--) k++; When this is done, k will equal 100. Can you explain? Did I post well? Rate it! Did I post badly? Rate that too!

          C Offline
          C Offline
          cmk
          wrote on last edited by
          #4

          The for test is: i + n which is the same as: i + n != 0 n = 100 so we can rewrite as: i + 100 != 0 or (subtract 100 from both sides), i != -100 so as i starts at 0 and decrements by 1 on each iteration it will loop 100 times. ...cmk Save the whales - collect the whole set

          1 Reply Last reply
          0
          • E Esmo2000

            I want to believe you, but I cannot. int k = 0; int i, n=100; for (i=0; i + n; i--) k++; When this is done, k will equal 100. Can you explain? Did I post well? Rate it! Did I post badly? Rate that too!

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

            It helps to show all of the code up front. Since n starts out as 100, rather than 0 as I initially presumed, i + n will eventually evaluate to 0 which will terminate the loop.


            "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

            1 Reply Last reply
            0
            • E Esmo2000

              What would the following conditional (i+n) in a for loop evaluate to? for (i=0; i + n; i--) I saw it once, but I dont understand what was happening. Did I post well? Rate it! Did I post badly? Rate that too!

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              in C/C++, booleans can be reprensented the following way :

              0 equals to false
              any other values different from 0 equals to true

              so, as (i + n) would equal to 100, it is true - until it become equals to 0... you follow me ? :-D


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              1 Reply Last reply
              0
              • D David Crow

                0 if n starts at 0. Otherwise non-zero, thus the for loop would never terminate.


                "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                C Offline
                C Offline
                cpm2
                wrote on last edited by
                #7

                Surely if n was -1 it would not evaluate to 1, but to zero, so the for loop would not loop forever?

                Helpful

                D 1 Reply Last reply
                0
                • C cpm2

                  Surely if n was -1 it would not evaluate to 1, but to zero, so the for loop would not loop forever?

                  Helpful

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

                  In the code snippet provided by Esmo2000, when does n ever equal -1?


                  "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                  C 1 Reply Last reply
                  0
                  • E Esmo2000

                    What would the following conditional (i+n) in a for loop evaluate to? for (i=0; i + n; i--) I saw it once, but I dont understand what was happening. Did I post well? Rate it! Did I post badly? Rate that too!

                    T Offline
                    T Offline
                    TheGreatAndPowerfulOz
                    wrote on last edited by
                    #9

                    for n < 0 the loop will execute until i wraps around to abs(n) for n == 0 the loop will never execute (i+0) == 0 == false for n > 0 the loop will execute n times

                    1 Reply Last reply
                    0
                    • D David Crow

                      In the code snippet provided by Esmo2000, when does n ever equal -1?


                      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                      C Offline
                      C Offline
                      cpm2
                      wrote on last edited by
                      #10

                      :-) I hadn't seen that response yet

                      Helpful

                      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