3 Replies - 1682 Views - Last Post: 22 July 2011 - 08:43 AM Rate Topic: -----

#1 Hezekiah   User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

REPEAT loops

Posted 07 December 2009 - 07:49 AM

Description: Instructions are given in the comments.The REPEAT_* macros are shorter versions of common for loop constructs.
/* repeat.h
 */

#ifndef REPEAT_H
#define REPEAT_H

/* iterate Range with Name
 * usage:
 * REPEAT_RN(begin, end, name) {
 *     [name = begin - (end-1)]
 * }
 */
#define REPEAT_RN(begin, end, name) for(int name = (begin); name < (end); ++name)

/* iterate Range
 * usage:
 * REPEAT_R(begin, end) {
 *     [i = begin - (end-1)]
 * }
 */
#define REPEAT_R(begin, end) REPEAT_RN(begin, end, i)

/* iterate Num times with Name
 * usage:
 * REPEAT_NN(num, name) {
 *     [name = 0 - (num-1)]
 * }
 */
#define REPEAT_NN(num, name) REPEAT_RN(0, num, name)

/* iterate Num times
 * usage:
 * REPEAT_N(num) {
 *     [i = 0 - (num-1)]
 * }
 */
#define REPEAT_N(num) REPEAT_NN(num, i)

/* iterate Array with Name
 * usage:
 * REPEAT_AN(array, name) {
 *     [name = 0 - (sizeof(array)/sizeof(*array)-1)]
 * }
 */
#define REPEAT_AN(array, name) REPEAT_NN((int) (sizeof(array) / sizeof(*array)), name)

/* iterate Array
 * usage:
 * REPEAT_A(array) {
 *     [i = 0 - (sizeof(array)/sizeof(*array)-1)]
 * }
 */
#define REPEAT_A(array) REPEAT_AN(array, i)

#endif



Is This A Good Question/Topic? 0
  • +

Replies To: REPEAT loops

#2 Hezekiah   User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: REPEAT loops

Posted 09 December 2009 - 06:47 AM

If you want to use an input value for num, but you don't want an infinite loop, use something like:
int input;
std::cin >> input;
if(input > 0)
	REPEAT(input) {
		//whatever you want to do;
	}
else
	std::cout << "You may not enter 0 or a negative value";

Was This Post Helpful? 0
  • +
  • -

#3 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: REPEAT loops

Posted 21 July 2011 - 06:20 PM

I can see it's usage, but perhaps more variety would help. For example, a macro to iterator over a range of number.
Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah   User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: REPEAT loops

Posted 22 July 2011 - 08:43 AM

Better? I was thinking about modifying it for a while now. Also, ignore my first comment. I don't know what was going on in my head back when I posted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1