J
James
Ian said:James wrote:
Required if you want it to compile!
I don't get it. Brackets aren't required to compile, they just extend
the scope of the loop. Did I miss something?
James
Ian said:James wrote:
Required if you want it to compile!
utab, the book C++ Primer is over 1,200 pages. Go ahead and provide the
context so we can explain what Mr. Lippman meant.
James said:I don't get it. Brackets aren't required to compile, they just extend
the scope of the loop. Did I miss something?
James
utab said:I do not know what is meant by Mr.Lippman. Since I do not have a paper
copy,
utab said:I do not know what is meant by Mr.Lippman. Since I do not have a paper
copy, I can not give an exact page number but the exact exercise number
is
Exercises Section 2.4
Exercise 2.22: The following program fragment, while legal, is an
example of poor style. What problem(s) does it contain? How would you
improve it?
for (int i = 0; i < 100; ++i)
// process i
No, they make a compound statement. A statement is REQUIRED after theJames said:I don't get it. Brackets aren't required to compile, they just extend
the scope of the loop. Did I miss something?
for (int i = 100; --i >= 0
{
// process i
}
is superior than the other solution : comparing an integer to 0 is more
effective than comparing to any other value.
Of course, reverse loops may have side effects, it may break your app !
Jim said:The simplest way to make it
better style is:
enum { ArrayElements = 100 };
for ( int i = 0; i < ArrayElements; ++i )
{
// process i, etc..
}
just to get rid of the magic number.
Clark said:More effective in what way?
James said:I don't get it. Brackets aren't required to compile, they just extend
the scope of the loop. Did I miss something?
James
Pete said:"Better" style depends on context. If I'm writing a throwaway utility to
do something 100 times, I'm not going to waste time and space creating
an enum.
Noah said:enums take space?
Pete Becker said:"Better" style depends on context. If I'm writing a throwaway utility to
do something 100 times, I'm not going to waste time and space creating an
enum. There's nothing wrong with writing a hard-coded upper limit in that
context.
Noah said:Pete Becker wrote:
enums take space?
for (int i = 100; --i >= 0
{
// process i
}
is superior than the other solution : comparing an integer to 0 is more
effective than comparing to any other value.
Of course, reverse loops may have side effects, it may break your app !
I do not know what is meant by Mr.Lippman. Since I do not have a paper
copy, I can not give an exact page number but the exact exercise number
is
Exercises Section 2.4
Exercise 2.22: The following program fragment, while legal, is an
example of poor style. What problem(s) does it contain? How would you
improve it?
for (int i = 0; i < 100; ++i)
// process i
Nope, you didn't miss anything unless you have compound statements.
What is required is the semicolon:
for(int i = 0; i < 100; ++i)
process i;
Gernot said:Being pedantic again:
process i; is systax error. Write:
process(i);
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.