0

This for loop macro is used frequently in competitive programming:

#define REP(i, a, b) \ for (int i = int(a); i <= int(b); i++) 

Now we use it as follows:

REP(i, a, b) statement; 

But what if i want to use multiple statement like this?

REP(i, a, b) statement1; statement2; 

where both the statements are inside the loop. How do I accomplish this?

1
  • 2
    Why the macro? Obfuscation, or... Commented Sep 21, 2013 at 11:51

2 Answers 2

5
REP(i, a, b) { statement1; statement2; } 

If that's really what you want. But keep in mind that it will make your code harder to read for anyone but you. Especially considering the fact that (i=0; i < n; i++) is more widespread than (i=0; i <= n; i++). If your main goal is to avoid writing this long construction yourself every time, I would take a look a snippets in your favorite code editor.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It's just that I found this standard in majority of competitive programming solutions and just wanted to know how to do it. I use vi.
0

Write

for(int i = a; i <= b; i++) { statement1; statement2; } 

There is ABSOLUTELY no reason for using a macro here - it just hides what the code does, and potentially breaks things (e.g. the values of a and b are not really fitting in an int, but the case makes them "fit".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.