What do you mean, no for loops?

Sat, 2 Jul 2005

There's a hole in my programming knowledge, and the name of that hole is C. Now, it just so happens that I've never actually needed to use C for anything, but recently I've started recompiling and even modifying packages for my personal use. Since the majority of Linux packages are written in C, that means I need to put C in my toolbox.

Digression alert: I actually learned some C++ in high school, in AP Computer Science. However, we never even touched the subject of objects, which makes the fact of using C++ rather pointless. That year was the last year the AP exam would be given in Pascal, which I think is why Doc McPherson decided to teach us C++. He said that once we knew one language, we could easily learn another, which is technically true but not the situation one wants for a major exam. In any case, I didn't learn object-oriented programming until I took an introductory programming class (in Java) at UW two summers ago.

Sub-digression: Doc is a brilliant man, but I think he's too brilliant for his own good. He was useless as a teacher, because he couldn't get out of his head enough to understand why a certain topic didn't make sense to his students. He'd give us a lecture and then send us into the lab, and if students asked him about the concepts in the lecture, he'd accuse them of not listening (and wouldn't tell them the answer). I managed to escape that stigma, I think because Doc had taught my brother Ralph (brilliant, and now a professional programmer at Apple). Doc would answer my questions, albeit at a much higher conceptual level than I could easily grasp. I'd go back to my friends, and we'd figure out as much as we could before I went back to Doc for another iteration.

So anyway, I'm working my way through Jeff's C book, C How to Program, and I've just realized that C has no for loop. I am quite distressed by this state of affairs. I know for (int i=0; i<10; i++) { } is only an abbreviation for int i=0; while (i<10) { i++; }. That's not the point.

The point is that for loops are my favorite programming construct, and I can't use them.

What's possibly even more distressing is my realization that I am enough of a geek, possibly even a dork, to have a favorite programming construct.

I've just turned over to the next chapter, and it looks like I was wrong. More Googling reveals the real meaning of the error message I got. for loops are allowed; I'm just not allowed to declare a variable inside one unless I tell gcc to use the C99 standard instead of its (older) default.

Comments

Jeff says:

Well, if you're going to say that for (i=0; i < 10; i++) {} is only an abbreviation for i = 0;
while (i < 10) { ; i++; }
, you might as well say that both are an abbreviation for

     move.l    #0.l, index
     move.l    index, d0
loop cmpi.l    #10.l, d0
     bge       end
     ; stuff
     bra       loop
end  nop

This being in MC68k assembly, many others are possible. This is also an abbreviation for something else, but I'm out of practice and not feeling that into it, so I won't translate this into MC68k machine code. You've been spared.

Jeff

Laurabelle says:

See what I have to put up with?!

Post a comment











XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

OpenID: If you use OpenID, your comment will be approved automatically and will not be held for moderation.