Adblock breaks this site

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

Discussion in 'Programming General' started by Govind, Sep 27, 2008.

  1. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick.

    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.
     
  2. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick.

    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)).
     
  3. Steal_Everything8

    Steal_Everything8 Guest

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

    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)
     
  4. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick.

    It can also be used to 'imagine' that you still have repeat/until, for coders who are comfortable with that sort of loop.
     
  5. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick.

    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.
     
  6. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick.

    Exactly.
     
  7. SCARSCRIPTER101

    SCARSCRIPTER101 Member

    Joined:
    Jul 30, 2009
    Posts:
    25
    Referrals:
    0
    Sythe Gold:
    0
    Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick.

    it would be cool for someone to make an include so you can make c++ coding like scar eg.
    WriteLn("Hello World");
    instead of
    cout << "Hello World\n";
     
< How can i edit a .class file | need help with c++ program >


 
 
Adblock breaks this site