Click to See Complete Forum and Search --> : Is There anything wrong with this??
Concrete Geist
01-29-2002, 08:55 PM
Look at this line, I got marked WRONG on this line in my class.
for( index=0, numcount=0; index < numItems; index++ )
This is C++. The part that was marked wrong is the initialization. The numcount=0 part is skwiggled out and a little -1 is written next to it. HOW IS THAT WRONG!!! :mad: :mad:
Danger Fan
01-29-2002, 09:28 PM
doesn't a for loop take 3 arguements?
yes, only 3
for(Initialization_Action; Boolean_Expression; Update_action)
you have 4 there
vee-eye
01-29-2002, 10:36 PM
You may have forgotten to declare numcount, thus not mentioning what type it is.
Originally posted by Danger Fan:
<STRONG>doesn't a for loop take 3 arguements?
yes, only 3
for(Initialization_Action; Boolean_Expression; Update_action)
you have 4 there</STRONG>
No, his first argument is a comma separated list of assignment statements. I believe for loop control structures are separated only by semicolons (at least in Java).
Strogian
01-30-2002, 07:51 AM
Originally posted by Concrete Geist:
<STRONG>Look at this line, I got marked WRONG on this line in my class.
for( index=0, numcount=0; index < numItems; index++ )
This is C++. The part that was marked wrong is the initialization. The numcount=0 part is skwiggled out and a little -1 is written next to it. HOW IS THAT WRONG!!! :mad: :mad:</STRONG>
Or perhaps your teacher just doesn't like having you initialize variables in the for structure, unless they are the lcv. I know I don't like it. :) Did this program compile and run normally? Or did you just write this out by hand?
I think your teacher probably took points off for style. I'm pretty sure the initialization phase and the "increment" stage take multiple comma seperated statements.
Still, since your numcount variable had nothing to do with actual loop control, it's probably not considered good style to initialize it in there. Should do it right before the for loop.
bugfix
01-30-2002, 09:54 AM
I agree, its prolly best to initialise outside of the loop (maybe when the variable is declared.) Syntactically its correct tho'. Or maybe it was because you gave the loop counter a rather vague name of 'index'.
[ 30 January 2002: Message edited by: bugfix ]
Originally posted by bugfix:
<STRONG>I agree, its prolly best to initialise outside of the loop (maybe when the variable is declared.) Syntactically its correct tho'. Or maybe it was because you gave the loop counter a rather vague name of 'index'.
</STRONG>
Would you rather he used 'for_loop_counter_variable'? :) imho, 'index' is overly verbose for a for loop counter. I'd have taken off points and told him to use 'i'. :D (this is a for loop counter, not a normal variable, don't think I apply this thought process to normal variables.)
Stuka
01-30-2002, 01:05 PM
kmj-
Wasn't that one of the important points in the essay "How to write unmaintainable code"? Something about NEVER using anything other than i, j, and k for loop indexes? :D
But yeah, y'all are right - the ONLY thing you SHOULD initialize in your loop initialization is the loop stuff itself. Syntactically correct != stylistically correct.
Originally posted by Stuka:
<STRONG>kmj-
Wasn't that one of the important points in the essay "How to write unmaintainable code"? Something about NEVER using anything other than i, j, and k for loop indexes? </STRONG>
I have a theory that for every statement in the "How to write unmaintainable code", you'll find that statement's exact inverse, converse, and contrapositive in there, too. I believe the Kernighan and Pike book "the practice of programming" says that using even 'index' is too verbose, but I could be wrong. can't find it right now.
I still use single letter variables as for loop indexes all the time and always will; just makes more sense to me, since any other word will not really be more descriptive, just longer.
[edit] -
(note I'm talking about for loops here, only)
There are cases where I don't use single letter variables; namely, in python where the variable is not the index, but the actual object in the list.
[ 30 January 2002: Message edited by: kmj ]
Stuka
01-30-2002, 03:13 PM
kmj-
Guess I shoulda put <sarcasm></sarcasm> around that - i, j, k, etc. are the only choice for me when using a strict index variable (plus they're declared in the for loop if I'm using C++ or Java).
ah... :) you didn't seem too serious about it. :D
there is nothing wrong with comma seperating things in the initialization stanza of a for loop in c or c++, you got marked poorly :/
Originally posted by jkm:
<STRONG>there is nothing wrong with comma seperating things in the initialization stanza of a for loop in c or c++, you got marked poorly :/</STRONG>
some schools have listed style standards, and if you don't follow them, you loose points. dunno if that's the case here, but it may be. I don't think it's fair to take off unless it's actually listes somewhere that it's against the standards.
lazy_cod3R
01-30-2002, 06:14 PM
there is nothing wrong with comma seperating things in the initialization stanza of a for loop in c or c++, you got marked poorly :/
I sorta agree also , if its gonna be used in the for loop why not just declare it in the scope of the for loop just as you have done ?
I think the teacher is being a little pedantic about it :(
I hated hand marking some teachers are really tough and some couldn't care less if you had the whole assignment in the main function :mad:
Concrete Geist
01-30-2002, 07:02 PM
My teacher is a very poor teacher. While she is experienced, she has never taught C++ before, and is only learning it as she goes along. I know more than she does about C++ (her field of expertiese is Pascal). It is a loosely taught class, and there are no stated style rules. However, I have done some things that have made her say "No, I don't like that way, don't do it". And after that I avoid them, these things include :
for( /* no initialization */; num < max; num++ )
for( i=0; i<10; i++ )
/* Not allowed to use one-lettered variables */
for( int num=0; num < 10 ; num++ )
/* no "loop scope" variables */
#define MAX 100
/* We have to use const */
-=, +=, /=, *= etc...
/* She calls these "shortcuts". Not allowed thse either */
And once she told me not to use break; in a loop. (but she didn't deduct marks on that one).
And don't judge the line of code for the variables, cause I have not posted the rest of the program. Just the initialization part is what lost me marks. My comp science class is rediculous.
[ 30 January 2002: Message edited by: Concrete Geist ]
bwkaz
01-30-2002, 07:57 PM
:eek: :eek: :eek:
The coding style police! They're out to get you!
Jeez. I'd have gone nuts if any of my teachers were like that, especially with C++, since it's legal, valid code.
-=, +=, /=, *= etc...
/* She calls these "shortcuts". Not allowed thse either */
:eek: :eek:
Your'e going to get COBOL fingers from all that typing! Especially since she won't let you use one-letter variables!
You've got more ability to put up with this stuff than I would. My hat goes off to you!
I would tell her that if she's not going to lay out a set of styles for you to follow, then she has no right deducting points for things she "doesn't like". That ain't right.
sans-hubris
01-30-2002, 08:37 PM
Here's some insight from a college CS student.
Originally posted by Concrete Geist:
for( /* no initialization */; num < max; num++ )
Might be a good idea to state something like this. Something like this would be helpful to the next person looking at your code, but most of the time, the next coder can figure out that initialization isn't needed. This may help to save a negligeable (or more) amount of time.
for( i=0; i<10; i++ )
/* Not allowed to use one-lettered variables */
One letter variables for loops are pretty standard. Few people would be confused by them. There's nothing wrong with them. Using whole letters just means more typing.
for( int num=0; num < 10 ; num++ )
/* no "loop scope" variables */
That's not necessarily a bad or a good idea. C++ allows for this, but C does not. Not all compilers are fully C++ ANSI compliant and will not necessarily allow for this (this is especially true for MSVC++.)
#define MAX 100
/* We have to use const */
For C++, using const is a Good Thing(TM). It allows C++ compilers to take advantage of runtime type checking, which is specified in standard C++. It's very helpful to have the compiler tell you that you are using a variable incorrectly. Using #define's, the compiler won't always catch incorrect usage of a variable. Using const for C is a bad thing as const is not a part of C, only C++.
-=, +=, /=, *= etc...
/* She calls these "shortcuts". Not allowed thse either */
No, these should be used. Your teacher is blatantly wrong in this area.
And once she told me not to use break; in a loop. (but she didn't deduct marks on that one).
I don't know how loops are broken in Pascal, but if they use something like a goto, that's a Bad Thing(TM). break is a very Good Thing(TM).
Dru Lee Parsec
01-30-2002, 08:51 PM
while(x<10) {
// do stuff
}
They do this :
for( ;x<10 ; ){
// do stuff
}
I hate that :mad:
;)
Ha! I had to edit the code block because the semicolon parenthesis in the second code block was interpreted as a smiley.
[ 30 January 2002: Message edited by: Dru Lee Parsec ]
Great! now that I edited my message it deleted everything in front of the first code block. OK, All I said essentially was that the double initialization was legal and that my pet peeve was when people use for loops in place of while loops.
[ 30 January 2002: Message edited by: Dru Lee Parsec ]
vee-eye
01-31-2002, 11:40 PM
// Pet peaves
if(a < b)
return true;
else
return false;
boolean c;
if(a < b)
c = true;
else
c = false
:mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad:
// This one is actually kind of funny
boolean state = !!!!!!!!!!!!!!!!!!!!!!!!!!true;
sans-hubris
02-01-2002, 03:27 AM
My pet peeve is inconsistent indentation styles (badly managed OSS projects like NiL have this all over the place and it's ugly.)
tnordloh
02-02-2002, 05:50 AM
She's showing her inexperience. It's good to do things that have become accepted in general, because it makes your code more readable. The +=, -=, etc are accepted and used all the time. It is good to use const instead of define statements though, but if the code calls for a #define, yous should use it. It's one of the available tools, and you shouldn't be limited from using it, if it's the right way.
Also, i,j,k are universal counter tokens, and anyone with a little coding experience knows this. I have no idea why this confuses her, if she has a history coding. Is it possible she hasn't ever read anyone else's code, or any examples in a book?
And if you are constantly getting hit up about style, she definitely needs to publish a standard, expecially since her ideas don't follow the 'normal' model.