Braces required?
-
I have the following code:
- for(int i = 0; i < 20; ++i)
- if(!m_aSoftwareGains[i].Load(pFile))
-
return false;
- 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."
-
I have the following code:
- for(int i = 0; i < 20; ++i)
- if(!m_aSoftwareGains[i].Load(pFile))
-
return false;
- 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."
-
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();
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."
-
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."
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?
-
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?
i = 0 :) :confused: J
"I am the Lorax. I speak for the trees."
-
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?
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."
-
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."
-
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();
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
-
Works fine when I use braces. Using VC++ 6.0 SP5. J
"I am the Lorax. I speak for the trees."
-
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
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."
-
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."
"just a style thing"? Read a good book on defensive programming - "Code Complete" comes to mind. Your "style" is prone to errors.
-
I have the following code:
- for(int i = 0; i < 20; ++i)
- if(!m_aSoftwareGains[i].Load(pFile))
-
return false;
- 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."
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
-
I have the following code:
- for(int i = 0; i < 20; ++i)
- if(!m_aSoftwareGains[i].Load(pFile))
-
return false;
- 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."
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
-
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
:-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."
-
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
:-O Yes, I discovered this after digging through the assembly. Oops. J
"I am the Lorax. I speak for the trees."
-
:-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."
-
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
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]