• Log in or sign up for an account here: https://www.sythe.org/login

Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick.

Govind

The One Musketeer

Highly Respected
Mudkips
Retired Administrator
Joined
Apr 22, 2005
Sythe Gold
23
Messages
7,825
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
If you're used to using Pascal-based languages such as SCAR or Delphi, you're probably familiar with the syntax of a repeat-until loop. While C/C++ has no innate support for an equivalent to a repeat-until loop, it is possible to use preprocessor macros to create one.

See here:
Code:
#include <iostream>
using namespace std;

#define REPEAT do{
#define UNTIL(expr) }while(!(expr))

int main(int argc, char *argv[])
{
	int i = 0;
	REPEAT
		cout << "i: " << i << endl;
		i++;
	UNTIL(i==10);
	return 0;
}

Macros created with the define statement are replaced with actual code when they are compiled. For example, if you were to save and view the intermediate (processed, .ii) C++ source file, the main() function would be:
Code:
int main(int argc, char *argv[])
{
	int i = 0;
	do{
		cout << "i: " << i << endl;
		i++;
	}while(!(i==10));
	return 0;
}
It works just as you would expect it to. This is just a source, not a tutorial, so I won't go any more in depth on it. If you already know general programming concepts this shouldn't be hard.
 
If you're used to using Pascal-based languages such as SCAR or Delphi, you're probably familiar with the syntax of a repeat-until loop. While C/C++ has no innate support for an equivalent to a repeat-until loop, it is possible to use preprocessor macros to create one.
That's really neat, I wouldn't of thought of that. It's also good that you showed what it actually compiles to so that anyone referring to this realizes that a REPEAT UNTIL is basically just a do while(not(blabla)).
 
Or just:
Code:
for (i=0,  i < 10, i++);
{
cout << "i: " << i << endl;
}

or

Code:
int i = 0

while(i<10);
{
cout << "i: " << i << endl;
i++
}

What you did looks like it'd take longer to code...

#define is used if you don't want to type out a long piece of code if you're going to be using it a lot, such as #define CAST(expr) static_cast<unsigned int long>(expr)
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
It can also be used to 'imagine' that you still have repeat/until, for coders who are comfortable with that sort of loop.
 
It can also be used to 'imagine' that you still have repeat/until, for coders who are comfortable with that sort of loop.
Yeah, I got that part but obviously the guy above you didn't because he tried to tell you two more effective ways of coding what you posted. Your point was not the time efficiency or coding ease needed to do it.
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Exactly.
 
Runescape Gamblingrunescape gambling
Back
Top