• 0

C++ "for" loop work-around?


Question

Hi, again! (-:

I suppose most beginning C++ programmers wonder about this kind of idea. And pardon me if the question is naive, but...

Is there some way to make a macro, or function, such that I could simply type something like lll(x) instead of using

int i;

for(i=0;i<x;i++)

every time I need a for loop? C has so many shortcuts for every other process, I thought maybe somebody'd found a work-around for "for".

Yes, I know it's petty. But after getting up to page 276 of "Jamsa's C/C++/C# Programmer's Bible" (yes, I'm reading it cover to cover!), and having typed the "for" format for the zillionth time...remember, we're talking about labor-saving here, so I'm not talking about using a "do-while" with an increment. Just something that could be declared in a file, included with each future program...

Peace and Love be with you,

Hosiah

Link to comment
https://www.neowin.net/forum/topic/170858-c-for-loop-work-around/
Share on other sites

18 answers to this question

Recommended Posts

  • 0

Put this into a header file for reuse (i.e. myfor.h):

#define FOR(x, y) for (int x=0; x &lt; y; x++)

So now you can just go like this:

FOR(i, 10)
{
  cout &lt;&lt; i &lt;&lt; '\n';
}

Of course it limits what you can do with it, but if you're just doing the standard for (int i=0; i < 13; i++) style loops it can save some time.

  • 0

>>>i don't see why you'd want to use a macro there anyways!? Are you writing that much raw code that it hurts? <<<

lol, no, I'm just a wimp!

Anyways, virtorio's excellent idea goes to show, this language is plastic enough that you can mold it into anything you want! Thank you for the idea, virtorio, and now I know that I can TOO use semicolons in a macro definition. I tried something similar that didn't work, thought the semicolons were doing it...

And relax, folks, just because I know the shortcut doesn't mean that I'll never type out a full "for" statement again as long as I live. Trust me, if there's a chance that another human eyeball will see my code, I'll stick to the standard form. I'm just in the "sandbox" stage, where I have gotten to know just enough of the language to start having fun playing with it.

*Wheeeeeeeee!*

Hosiah

  • 0

Ok, so educate the noob! Why are macros so bad? Is this one of those controversies that rage on over programming conventions, similar to the hub-bub about "goto"? (which I have yet needed to use in a C++ program)

Incidentally, I sometimes create macros or functions that mimic functions that didn't happen to come with any of my compilers, but which are discussed in the literature. So they have been a "Godsend" for me as well. But I could also see where, if you're part of a team and somebody else has to debug your code, only to find the whole thing is done with undocumented macros, that could be a bad thng...

Peace and Love,

Hosiah

  • 0

I personally don't like the idea of macros because they can do things to the code that might have some strange side effect that I don't want and didn't think of when I wrote the macro. It might not show up in other projects, but if the macro is complex enough, sometime it's going to cause a problem. I guess I just like knowing exactly what the code is doing :) I code everything by hand, with the exception of what the Windows Forms designer in Visual Studio .NET does itself :yes: However, with something as simple as a macro to create a for loop, I don't really see a problem.

  Hosiah said:
Incidentally, I sometimes create macros or functions that mimic functions that didn't happen to come with any of my compilers, but which are discussed in the literature. So they have been a "Godsend" for me as well.

But are you really learning the functions then? :huh: If you're just creating the macro once (probably copying it out of a book anyway) and don't actually write it out yourself very often, I would think you're not truly learning the lesson of that function. You know what it does, but not necessarilly how it works and why it works that way instead of some other way.

  • 0
  Quote
Ok, so educate the noob! Why are macros so bad? Is this one of those controversies that rage on over programming conventions, similar to the hub-bub about "goto"? (which I have yet needed to use in a C++ program)

Yeah, macros when used sparingly can be helpful.

The downside is that the compiler can't check them for syntax correctness and they aren't type safe. The compiler also can't decide if it is appropriate to insert the code inline, all of which the inline keyword provides. So liberal use of macros can also lead to larger code because they are always expanded inline.

In larger projects, ones that you may not be coding everything, they probably aren't a good idea unless everyone on the team is on the same page.

  • 0
  weenur said:
Yeah, macros when used sparingly can be helpful.

The downside is that the compiler can't check them for syntax correctness and they aren't type safe. The compiler also can't decide if it is appropriate to insert the code inline, all of which the inline keyword provides. So liberal use of macros can also lead to larger code because they are always expanded inline.

In larger projects, ones that you may not be coding everything, they probably aren't a good idea unless everyone on the team is on the same page.

exactly

  • 0

An interesting thing is the fact that Quake 2's source code (i think it was Quake 3's mod source code actually) has the following defines:

#define qtrue true

#define qfalse false

I'm not sure it was exactly that way, but I know qtrue and qfalse exists.

Ohhh it's so l33t :p

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.