3 Replies - 1897 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 loop

Posted 07 December 2009 - 07:49 AM

Description: Copy and paste this code and save it as repeat.h. Include it in any file in which you want to use this loop. Further instructions are given in the comments. // comments are not supported by some C compilers.Have you ever wanted to just loop a specific number of time without having to type: 'for(int i = 0; i < num; i ++) {}' ? Now you can just type: 'REPEAT(num) {}' .
//repeat.h
#ifndef _REPEAT_H_
	#define _REPEAT_H_
	#define REPEAT(num) for(int rpt = 0; rpt < (num); rpt ++)
#endif
/*
	REPEAT(num) {			//num == number of times you want to loop
		//contents of loop;	//rpt (int) == (first repetition == 0, second repetion == 1, ect)
	}
*/

//custom versions
//you may edit from here
//if you edit, save the file under a different name (not repeat.h)
//#ifndef _REPEAT_CUSTOM_
	//#define _REPEAT_CUSTOM_
	//#define REPEAT1(num) for(custom version 1)
	//#define REPEAT2(num) for(custom version 2)
	//ect...
//#endif
//stop editting here


Is This A Good Question/Topic? 0
  • +

Replies To: REPEAT loop

#2 Hezekiah   User is offline

  • D.I.C Addict
  • member icon

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

Re: REPEAT loop

Posted 07 December 2009 - 07:49 AM

Description: Copy and paste this code and save it as repeat.h. Include it in any file in which you want to use this loop. Further instructions are given in the comments. // comments are not supported by some C compilers. Replace them with the following blocks of /* */ comments: line 1, 11 - 12, 14 - 16, 17 - 18, 19, 20, 21, 22 and 23.Have you ever wanted to just loop a specific number of time without having to type: 'for(int i = 0; i < num; i ++) {}' ? Now you can just type: 'REPEAT(num) {}' .
//repeat.h
#ifndef _REPEAT_H_
	#define _REPEAT_H_
	#define REPEAT(num) for(int rpt = 0; rpt < (num); rpt ++)
#endif
/*
	REPEAT(num) {			//num == number of times you want to loop
		//contents of loop;	//rpt (int) == (first repetition == 0, second repetition == 1, ect)
	}
*/
//num = 0 or a negative value for an infinite loop
//float or double values are seen as the number before the . + 1

//custom versions
//you may edit from here
//if you edit, save the file under a different name (not repeat.h)
//#ifndef _REPEAT_CUSTOM_
	//#define _REPEAT_CUSTOM_
	//#define REPEAT1(num) for(custom version 1)
	//#define REPEAT2(num) for(custom version 2)
	//ect...
//#endif
//stop editing here

Was This Post Helpful? 0
  • +
  • -

#3 Hezekiah   User is offline

  • D.I.C Addict
  • member icon

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

Re: REPEAT loop

Posted 07 December 2009 - 07:49 AM

Description: Copy and paste this code and save it as repeat.h. Include it in any file in which you want to use this loop. Further instructions are given in the comments.Have you ever wanted to just loop a specific number of time without having to type: 'for(int i = 0; i < num; i ++) {}' ? Now you can just type: 'REPEAT(num) {}' .
//repeat.h
#ifndef _REPEAT_H_
	#define _REPEAT_H_
	#define REPEAT(num) for(int rpt = 0; rpt < (num); rpt ++)
#endif
/*
	REPEAT(num) {                  //num == number of times you want to loop
		//contents of loop;    //rpt (int) == (first repetition == 0, second repetition == 1, ect)
	}
*/
//num = 0 or a negative value for an infinite loop
//float or double values are seen as the next full 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 loop

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
  • +
  • -

#5 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

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

Re: REPEAT loop

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
  • +
  • -

#6 Hezekiah   User is offline

  • D.I.C Addict
  • member icon

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

Re: REPEAT loop

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