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. Braces required?

Braces required?

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

    I have the following code:

    1. for(int i = 0; i < 20; ++i)
    2. if(!m_aSoftwareGains[i].Load(pFile))
    3.  return false;
      
    4. m_bSoftwareGainsLoaded = true;

    I didn't put in the seemingly extraneous braces. Rather than running the following lines: 1,2,1,2,1,2,1,2...1,2,4 it runs: 1,2,4 Can anyone explain to me why this is? J

    "I am the Lorax. I speak for the trees."

    V J H 3 Replies Last reply
    0
    • J Jamie Hale

      I have the following code:

      1. for(int i = 0; i < 20; ++i)
      2. if(!m_aSoftwareGains[i].Load(pFile))
      3.  return false;
        
      4. m_bSoftwareGainsLoaded = true;

      I didn't put in the seemingly extraneous braces. Rather than running the following lines: 1,2,1,2,1,2,1,2...1,2,4 it runs: 1,2,4 Can anyone explain to me why this is? J

      "I am the Lorax. I speak for the trees."

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      What? The compile should output an error message. Insert the braces. Kuphryn

      R 1 Reply Last reply
      0
      • V valikac

        What? The compile should output an error message. Insert the braces. Kuphryn

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        kuphryn wrote: What? The compile should output an error message. Insert the braces :confused: why ? syntax is ok. But inserting brackets is still a good idea ! ~RaGE();

        J J 2 Replies Last reply
        0
        • R Rage

          kuphryn wrote: What? The compile should output an error message. Insert the braces :confused: why ? syntax is ok. But inserting brackets is still a good idea ! ~RaGE();

          J Offline
          J Offline
          Jamie Hale
          wrote on last edited by
          #4

          A good idea, yes. But not required. It should execute the way I want, right? J

          "I am the Lorax. I speak for the trees."

          D 1 Reply Last reply
          0
          • J Jamie Hale

            A good idea, yes. But not required. It should execute the way I want, right? J

            "I am the Lorax. I speak for the trees."

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

            Yes. Have you put a breakpoint on statement #4 and noted the value of 'i' at that point? Is it >= 20? Can you reproduce this problem in a smaller program such that it could be posted here?

            J 2 Replies Last reply
            0
            • D David Crow

              Yes. Have you put a breakpoint on statement #4 and noted the value of 'i' at that point? Is it >= 20? Can you reproduce this problem in a smaller program such that it could be posted here?

              J Offline
              J Offline
              Jamie Hale
              wrote on last edited by
              #6

              i = 0 :) :confused: J

              "I am the Lorax. I speak for the trees."

              1 Reply Last reply
              0
              • D David Crow

                Yes. Have you put a breakpoint on statement #4 and noted the value of 'i' at that point? Is it >= 20? Can you reproduce this problem in a smaller program such that it could be posted here?

                J Offline
                J Offline
                Jamie Hale
                wrote on last edited by
                #7

                As soon as my build finishes, I'll try to reproduce it in a postable version. J

                "I am the Lorax. I speak for the trees."

                K 1 Reply Last reply
                0
                • J Jamie Hale

                  As soon as my build finishes, I'll try to reproduce it in a postable version. J

                  "I am the Lorax. I speak for the trees."

                  K Offline
                  K Offline
                  Keith D
                  wrote on last edited by
                  #8

                  hmm.. does it work correctly when you use braces? and what compiler are you using?

                  J 1 Reply Last reply
                  0
                  • R Rage

                    kuphryn wrote: What? The compile should output an error message. Insert the braces :confused: why ? syntax is ok. But inserting brackets is still a good idea ! ~RaGE();

                    J Offline
                    J Offline
                    John M Drescher
                    wrote on last edited by
                    #9

                    Rage wrote: But inserting brackets is still a good idea ! I always insert the braces. It makes the code easier to read and avoids me forgetting to add them when I add another statement to the for loop. As for the error I can not see why... John

                    J A 2 Replies Last reply
                    0
                    • K Keith D

                      hmm.. does it work correctly when you use braces? and what compiler are you using?

                      J Offline
                      J Offline
                      Jamie Hale
                      wrote on last edited by
                      #10

                      Works fine when I use braces. Using VC++ 6.0 SP5. J

                      "I am the Lorax. I speak for the trees."

                      1 Reply Last reply
                      0
                      • J John M Drescher

                        Rage wrote: But inserting brackets is still a good idea ! I always insert the braces. It makes the code easier to read and avoids me forgetting to add them when I add another statement to the for loop. As for the error I can not see why... John

                        J Offline
                        J Offline
                        Jamie Hale
                        wrote on last edited by
                        #11

                        To me, the code is "cleaner" without the braces, but I understand it's just a style thing. J

                        "I am the Lorax. I speak for the trees."

                        J 1 Reply Last reply
                        0
                        • J Jamie Hale

                          To me, the code is "cleaner" without the braces, but I understand it's just a style thing. J

                          "I am the Lorax. I speak for the trees."

                          J Offline
                          J Offline
                          Jim A Johnson
                          wrote on last edited by
                          #12

                          "just a style thing"? Read a good book on defensive programming - "Code Complete" comes to mind. Your "style" is prone to errors.

                          1 Reply Last reply
                          0
                          • J Jamie Hale

                            I have the following code:

                            1. for(int i = 0; i < 20; ++i)
                            2. if(!m_aSoftwareGains[i].Load(pFile))
                            3.  return false;
                              
                            4. m_bSoftwareGainsLoaded = true;

                            I didn't put in the seemingly extraneous braces. Rather than running the following lines: 1,2,1,2,1,2,1,2...1,2,4 it runs: 1,2,4 Can anyone explain to me why this is? J

                            "I am the Lorax. I speak for the trees."

                            J Offline
                            J Offline
                            John R Shaw
                            wrote on last edited by
                            #13

                            I sounds like you are single steping through the code with the debugger.

                            1. for(int i = 0; i < 20; ++i)
                            2. if(!m_aSoftwareGains[i].Load(pFile))
                            3. return false;
                            4. m_bSoftwareGainsLoaded = true;

                            When single stepping it will appear to go 1,2,4,1,2,4,...,1,2,3.

                            1. for(int i = 0; i < 20; ++i)
                            2. {
                            3. if(!m_aSoftwareGains[i].Load(pFile))
                            4. return false;
                            5. }
                            6. m_bSoftwareGainsLoaded = true;

                            Now It should appear to be 1,3,5,1,3,5,...,1,3,4.

                            INTP

                            J 1 Reply Last reply
                            0
                            • J Jamie Hale

                              I have the following code:

                              1. for(int i = 0; i < 20; ++i)
                              2. if(!m_aSoftwareGains[i].Load(pFile))
                              3.  return false;
                                
                              4. m_bSoftwareGainsLoaded = true;

                              I didn't put in the seemingly extraneous braces. Rather than running the following lines: 1,2,1,2,1,2,1,2...1,2,4 it runs: 1,2,4 Can anyone explain to me why this is? J

                              "I am the Lorax. I speak for the trees."

                              H Offline
                              H Offline
                              Haakon S
                              wrote on last edited by
                              #14

                              Are you sure you don't have a ";" at the end of line one? I've tried to reproduce the difference in behaviour, but with no success. When you look at the disassembly, it seems to be identical with or without braces. :confused: Haakon. 'Problems worthy of attack prove their worth by hitting back.' Piet Hein

                              J 1 Reply Last reply
                              0
                              • H Haakon S

                                Are you sure you don't have a ";" at the end of line one? I've tried to reproduce the difference in behaviour, but with no success. When you look at the disassembly, it seems to be identical with or without braces. :confused: Haakon. 'Problems worthy of attack prove their worth by hitting back.' Piet Hein

                                J Offline
                                J Offline
                                Jamie Hale
                                wrote on last edited by
                                #15

                                :-O It was a user problem. I assumed the problem was there, and that biased my testing. I stepped through, and when the code returns from the call (line 2), the little yellow arrowhead points to line 4. THAT DOESN'T MEAN THAT IT IS THE NEXT INSTRUCTION TO RUN! I must have seen that in my testing, wondered what the hell was going on, and stopped the debug. I too traced through the assembly, but made the same mistake - didn't trace through far enough. Oh well. :-O J

                                "I am the Lorax. I speak for the trees."

                                H 1 Reply Last reply
                                0
                                • J John R Shaw

                                  I sounds like you are single steping through the code with the debugger.

                                  1. for(int i = 0; i < 20; ++i)
                                  2. if(!m_aSoftwareGains[i].Load(pFile))
                                  3. return false;
                                  4. m_bSoftwareGainsLoaded = true;

                                  When single stepping it will appear to go 1,2,4,1,2,4,...,1,2,3.

                                  1. for(int i = 0; i < 20; ++i)
                                  2. {
                                  3. if(!m_aSoftwareGains[i].Load(pFile))
                                  4. return false;
                                  5. }
                                  6. m_bSoftwareGainsLoaded = true;

                                  Now It should appear to be 1,3,5,1,3,5,...,1,3,4.

                                  INTP

                                  J Offline
                                  J Offline
                                  Jamie Hale
                                  wrote on last edited by
                                  #16

                                  :-O Yes, I discovered this after digging through the assembly. Oops. J

                                  "I am the Lorax. I speak for the trees."

                                  1 Reply Last reply
                                  0
                                  • J Jamie Hale

                                    :-O It was a user problem. I assumed the problem was there, and that biased my testing. I stepped through, and when the code returns from the call (line 2), the little yellow arrowhead points to line 4. THAT DOESN'T MEAN THAT IT IS THE NEXT INSTRUCTION TO RUN! I must have seen that in my testing, wondered what the hell was going on, and stopped the debug. I too traced through the assembly, but made the same mistake - didn't trace through far enough. Oh well. :-O J

                                    "I am the Lorax. I speak for the trees."

                                    H Offline
                                    H Offline
                                    Haakon S
                                    wrote on last edited by
                                    #17

                                    No problem.:-D If we didn't learn anything, you certainly have. H. 'Problems worthy of attack prove their worth by hitting back.' Piet Hein

                                    1 Reply Last reply
                                    0
                                    • J John M Drescher

                                      Rage wrote: But inserting brackets is still a good idea ! I always insert the braces. It makes the code easier to read and avoids me forgetting to add them when I add another statement to the for loop. As for the error I can not see why... John

                                      A Offline
                                      A Offline
                                      Atlantys
                                      wrote on last edited by
                                      #18

                                      John M. Drescher wrote: I always insert the braces I'll :beer: to that! :-D I prefer to wear gloves when using it, but that's merely a matter of personal hygiene [Roger Wright on VB] Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. [Rich Cook]

                                      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