Why is for(int i=0; i < 100; ++i) poor?

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
 
D

Daniel T.

utab, the book C++ Primer is over 1,200 pages. Go ahead and provide the
context so we can explain what Mr. Lippman meant.

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[/QUOTE]

I looked around on the 'net a bit but couldn't find any references (and
I don't have the book.) Without more context, one cannot say that the
above is "poor style" in any way. If the magic number '100' appears in
come other location such that if it changes in one place, it must also
change here, then using '100' instead of a const int is poor style.
 
J

Jim Langston

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

Yes. They're saying the following program:

int main()
{
for ( int i = 0; i < 100; ++i )
// process i
}

will not compile without at least a ; or {} at the end of the for block. A
bit pedantic since this is only a code snippet.
 
M

mlimber

utab said:
I do not know what is meant by Mr.Lippman. Since I do not have a paper
copy,

Please tell me you didn't download it from one of those pirated sites
that are spammed here every week or so. If so, I cannot help you.

Cheers! --M
 
J

Jim Langston

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

That excersice is bogus AFAIC because there is not enough information given
to determine if that is in fact poor style. 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.

Other than that, whoever wrote that exercise exepects you to be a mind
reader to figure out what the heck he was talking about, unless the section
before that explained why he didn't like it (a personal choice it would
seem)
 
R

Ron Natalie

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?
No, they make a compound statement. A statement is REQUIRED after the
for loop. You either need { } or a ; or some other legal form of a
statemnt.
 
A

alexisbietti

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 !
 
C

Clark S. Cox III

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.

More effective in what way?
Of course, reverse loops may have side effects, it may break your app !

This seems to mean that it is *less* effective in the general case.
 
P

Pete Becker

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.

"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.
 
S

Salt_Peter

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

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;
 
N

Noah Roberts

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.

enums take space?
 
J

Jim Langston

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.

I agree. Myself, I would most likely code it exactly like the original
sample.
 
J

Jim Langston

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 !

Of course, though, if you're iterating through an array of 100 elements,
with your code you're now iterating backwards, unless you do 100-n or
something, which makes it less effective.

If someone is worried about an extra clock cycle, they should be doing it in
assembly in the first place.
 
A

Andrew Koenig

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

As noted, the exercise appears at the end of section 2.4. If you read
section 2.4, you will find that the exercise is answered in the first few
paragraphs of that section.

So the real point of the exercise is to get you to read the text :)
 
G

Gernot Frisch

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;

Being pedantic again:
process i; is systax error. Write:
process(i);
 
A

alexisbietti

Even better solution :

for (int i = 100; i--; )
{
// process i
}

Saves a comparison operation, as is tests only whether the return value
of i-- (which is i before decrementation) is zero or not.



(e-mail address removed) a écrit :
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top