How to sequence a loop
-
for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:
-
for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:
Unfortunately I've seen this sort of thing far too often. It's up there with
if((x == 1) && (x != 2))
and similar stuff. :)Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:
-
for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:
The fact that the final else block will never be executed suggests either (a) this code used to do something different and has been hacked around blindly, or (b) the person who wrote it didn't really understand what he was doing.
-
The fact that the final else block will never be executed suggests either (a) this code used to do something different and has been hacked around blindly, or (b) the person who wrote it didn't really understand what he was doing.
or (c) he really didn't trust integers. To be sure, to be sure, to be sure...
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh:
I already saw a teacher showing "how" to use switches. The combo-box had 2 values: Gif. Jpg. Then, in the code, he did something like:
for(int i=0; i<2; i++)
{
if (i == combo.SelectedIndex)
{
switch(i)
{
case 1:
// code for gif
break;case 2: // code for jpg. break; }
}
}Note that not only the "for" is useless, the "switch" using the values of a for is ridiculous... but the valid values are 0 and 1, and the code checked for 1 and 2. And finally, but not presented in the code, the code was copied and the filename was something + ".gif" or something + ".jpg"... he could simple do a better working code with:
string name = editName.Text + '.' + combo.SelectedValue;
-
I already saw a teacher showing "how" to use switches. The combo-box had 2 values: Gif. Jpg. Then, in the code, he did something like:
for(int i=0; i<2; i++)
{
if (i == combo.SelectedIndex)
{
switch(i)
{
case 1:
// code for gif
break;case 2: // code for jpg. break; }
}
}Note that not only the "for" is useless, the "switch" using the values of a for is ridiculous... but the valid values are 0 and 1, and the code checked for 1 and 2. And finally, but not presented in the code, the code was copied and the filename was something + ".gif" or something + ".jpg"... he could simple do a better working code with:
string name = editName.Text + '.' + combo.SelectedValue;
or maybe he is just impressing his students how genius he is :)
-
or (c) he really didn't trust integers. To be sure, to be sure, to be sure...
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
...but in real-life I've seen "obviously unnecessary" else-blocks quite often. Most times there was an error-handling routine, which put something like "unhandled case; please look at routine foo()" to your log-file. And in some cases it make sense - who guarantees you that a guy who extends this routine, thought on all possible occurances of "i"?
-
...or shouldnt
-
for( int i = 1 ; i <= 3 ; i++) { if(i==1){…} else if(i==2){…} else if(i==3){…} else{…} } source: http://www.coding-horror.de/?p=258 :laugh: