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

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

Migrating to C++ from Pascal/SCAR/Delphi? Look here for a cool trick.
  1. Unread #1 - Sep 27, 2008 at 11:29 PM
  2. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    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.
     
  3. Unread #2 - Oct 1, 2008 at 8:49 AM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    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)).
     
  5. Unread #3 - Oct 9, 2008 at 1:55 AM
  6. Steal_Everything8
    Referrals:
    0

    Steal_Everything8 Guest

    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)
     
  7. Unread #4 - Oct 9, 2008 at 5:17 PM
  8. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    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.
     
  9. Unread #5 - Oct 9, 2008 at 11:31 PM
  10. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    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.
     
  11. Unread #6 - Oct 10, 2008 at 12:23 AM
  12. Govind
    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

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

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

    Exactly.
     
  13. Unread #7 - Sep 25, 2009 at 1:39 AM
  14. SCARSCRIPTER101
    Joined:
    Jul 30, 2009
    Posts:
    25
    Referrals:
    0
    Sythe Gold:
    0

    SCARSCRIPTER101 Member

    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 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site