473,837 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How not to abuse a "for loop": examples?

At the page:
http://www.strath.ac.uk/IT/Docs/Ccou...00000000000000
or
http://tinyurl.com/4ptzs

the author warns:
"The for loop is frequently used, usually where the loop will be
traversed a fixed number of times. It is very flexible, and novice
programmers should take care not to abuse the power it offers."

Could someone give an example of this abuse? I'm googling for
c misuse "for loop"
c abuse "for loop"
with little luck.

I'd like to see what *not* to do :)

Toby
Nov 14 '05 #1
32 4666
On 9 Aug 2004, Toby Newman wrote:
the author warns:
"The for loop is frequently used, usually where the loop will be
traversed a fixed number of times. It is very flexible, and novice
programmers should take care not to abuse the power it offers."

Could someone give an example of this abuse? I'm googling for
c misuse "for loop"
c abuse "for loop"
with little luck.

I'd like to see what *not* to do :)


for (a(), b(), c(), d(), e(), f(), g();
h(), i(), j(), k(), l(), m(), n(), o(), p();
q(), r(), s(), t(), u(), v(), w(), x(), y(), z())
;

Tak-Shing

Nov 14 '05 #2
"Toby Newman" <go****@asktoby .com> wrote in message
news:25******** *************** ***@posting.goo gle.com...

"The for loop is frequently used, usually where the loop will be
traversed a fixed number of times. It is very flexible, and novice
programmers should take care not to abuse the power it offers."

Could someone give an example of this abuse? I'm googling for
c misuse "for loop"
c abuse "for loop"
with little luck.

I'd like to see what *not* to do :)


for (i = 0; i < 100; i += (i == 98) ? -97 : 2)

for (; *++argv
&& (fp = fopen(*argv, "rb"))
&& fread(buffer, 10, 10, fp) == 10
&& fclose(fp) != EOF; )

Although, I'm guilty of both. ;)

--
Peter
Nov 14 '05 #3
Toby Newman wrote:
At the page:
http://www.strath.ac.uk/IT/Docs/Ccou...00000000000000
or
http://tinyurl.com/4ptzs

the author warns:
"The for loop is frequently used, usually where the loop will be
traversed a fixed number of times. It is very flexible, and novice
programmers should take care not to abuse the power it offers."

Could someone give an example of this abuse? I'm googling for
c misuse "for loop"
c abuse "for loop"
with little luck.

I'd like to see what *not* to do :)

Toby


for (i = 0; i < 100; ++i)
{
--i;
}

float j;
for (j = 0.0; j != 10.0; j += 0.1)
{
}

unsigned int m;
for (m = 9; m > 0; m -= 2)
{
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #4
On Mon, 9 Aug 2004, Toby Newman wrote:
At the page:
http://www.strath.ac.uk/IT/Docs/Ccou...00000000000000
or
http://tinyurl.com/4ptzs

the author warns:
"The for loop is frequently used, usually where the loop will be
traversed a fixed number of times. It is very flexible, and novice
programmers should take care not to abuse the power it offers."

Could someone give an example of this abuse? I'm googling for
c misuse "for loop"
c abuse "for loop"
with little luck.

I'd like to see what *not* to do :)


There is nothing you can do with the for loop that you cannot do with the
while loop. For some bizzare reason, people tend to abuse the comma
operator in a for loop but not in a while loop. The for loop has three
sections:

for(init; check; incr)

The init section is where you initialize variables used in the for loop. A
little abuse happens here. People initialize a number of variables here
when they could have been initialized before the loop. I like to do this
if the variables are relative to the loop. If I initialize them at the top
of scope the initialization might not be visible while I step through the
for loop.

The big abuse is in the check section. Here you should be checking to see
if the for loop should exit of not. Some times programmers will stick a
number of statements that could just as easily fit in the body of the loop
into this section. This often makes for a hard to read bit of code.

Another place for abuse is the incr section. This is the section that you
should be incrementing something. If you use a for loop for stepping
through a known number of iterations then this section should just be
incrementing the counter used to keep track of the iterations.

There are no hard and fast rules on this. I often see:

while((c = getchar()) != EOF) {
/* code */
}

getting converted to:

for(; (c = getchar()) != EOF; /* code */);

It is all a matter of coding conventions or style.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 14 '05 #5
In article <25************ **************@ posting.google. com>,
Toby Newman <go****@asktoby .com> wrote:
At the page:
http://www.strath.ac.uk/IT/Docs/Ccou...00000000000000
or
http://tinyurl.com/4ptzs

the author warns:
"The for loop is frequently used, usually where the loop will be
traversed a fixed number of times. It is very flexible, and novice
programmers should take care not to abuse the power it offers."

Could someone give an example of this abuse? I'm googling for
c misuse "for loop"
c abuse "for loop"
with little luck.

I'd like to see what *not* to do :)


--------
for(in=fopen(fi lename,"r");in& &fgets(buf,size of buf,in);)
{
/*do stuff with this line of the file*/
}
--------
/*i is unsigned*/
for(i=1;i;i*=2)
{
/*do stuff with this power of 2*/
}
/*we fall out when we hit max(i)+1 and the unsigned arithmetic wraps
back around to 0
*/
--------
A favorite of mine, that falls under "uncommon" but not "abuse", is:
--------
for(curr=head;c urr;curr=curr->next)
{
/*Do stuff with this list node*/
}
--------
to walk through a linked list.
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
[This] is otherwise known as 'learning by experience'. I don't often
come across people who publicly state they're not interested in that.
--Arthur van der Harg in the scary devil monastery
Nov 14 '05 #6
Dave Vandervies wrote:
A favorite of mine, that falls under "uncommon" but not "abuse", is:
--------
for(curr=head;c urr;curr=curr->next)
{
/*Do stuff with this list node*/
}
--------
to walk through a linked list.


Is that really so uncommon? It seems like the natural way to traverse a
linked list. (Except that I would explicitly test for curr != NULL, since
curr is not a boolean value. But that's also a matter of taste).
Christian
Nov 14 '05 #7
"Christian Kandeler" <ch************ ****@hob.de> wrote in message
news:2n******** ****@uni-berlin.de...
Dave Vandervies wrote:
A favorite of mine, that falls under "uncommon" but not "abuse", is:
--------
for(curr=head;c urr;curr=curr->next)
{
/*Do stuff with this list node*/
}
--------
to walk through a linked list.


Is that really so uncommon? It seems like the natural way to traverse a
linked list. (Except that I would explicitly test for curr != NULL, since
curr is not a boolean value. But that's also a matter of taste).


curr != NULL or curr != 0?
I'm confused now, after the Null Pointer thread... "Keith!!! Where are
you?!!"

--
Mabden
Nov 14 '05 #8
Mabden wrote:

"Christian Kandeler" <ch************ ****@hob.de> wrote in message
news:2n******** ****@uni-berlin.de...
Dave Vandervies wrote:
for(curr=head;c urr;curr=curr->next)
(Except that I would explicitly test for curr != NULL, since
curr is not a boolean value. But that's also a matter of taste).


curr != NULL or curr != 0?


Since the point of writing curr != NULL or curr != 0
instead of just (;curr;) is to enhance legibility,
I think it makes sense to write curr != NULL
When I read (curr != NULL), that tells me that the author
thought that curr was a pointer, when he wrote the code.
Nov 14 '05 #9
go****@asktoby. com (Toby Newman) wrote in message news:<25******* *************** ****@posting.go ogle.com>...
"The for loop is ... very flexible, and novice
programmers should take care not to abuse the power it offers."

Could someone give an example of this abuse? ...
I'd like to see what *not* to do :)


http://tinyurl.com/2452h/wnim.htm
(scroll about half way down.)

Hope this helps,
James
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
3591
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else: print "Empty list" which was supposed to be:
9
5815
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is held in memory and therefore fast, without risk of damaging real files, and with none of the messiness of leftover files after testing. Googling the archives and the web suggests that only I and Remy Blank have done much along these lines. I...
6
71986
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
15
2004
by: GS | last post by:
Hello, I am looking for any opensource package for "appointment system" for doctors, please let me know. This is just o take Name/Phone/time, incase if there is not any opensource, then is it hard to implement using web/php/mysql?. Please let me know. GS
34
2695
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include <limits.h> #include <stdlib.h> int main(void) {
0
9847
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9693
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10899
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7012
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4481
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.