Is there a way to make a function similar to while statement?
What language? If you''re using C or C++, you could probably do whatever you want with a #define and a lot of support code.
You're coding c or c++, right? In those languages coding structures (if/while/for etc.) are hardcoded, you can't roll your own.
There are other languages where you can, though. In Lisp or Scheme it's as simple as defining a normal function... unfortunately I can't remember how you define a normal function in Lisp, so I can't give you a quick example right now.
In Perl and Python you can use blocks of code as parameters to functions, these are calles closures, and have some tricky properties concerning global variables, but thats another story. Anyway, it looks a bit crufty, but you can get a bit of what I think you want. Example in perl:
[edited by - deformed rabbit on November 10, 2003 9:16:23 PM]
There are other languages where you can, though. In Lisp or Scheme it's as simple as defining a normal function... unfortunately I can't remember how you define a normal function in Lisp, so I can't give you a quick example right now.
In Perl and Python you can use blocks of code as parameters to functions, these are calles closures, and have some tricky properties concerning global variables, but thats another story. Anyway, it looks a bit crufty, but you can get a bit of what I think you want. Example in perl:
// declare structuresub whileish ($$$) { my $cond = shift; my $block1 = shift; my $block2 = shift; while (1) { my $val = &$cond(); if ($val == 1) { &$block1() } elsif ($val == 2) { &$block2() } else last; }}// this will hang, but I hope you get the pointmy $number=1;whileish { $number } { print "number=1"; $number=2; } { print "number=2"; $number=1; }
[edited by - deformed rabbit on November 10, 2003 9:16:23 PM]
#define MyWhile(condition) for (;condition
That''s about as good as you''re going to get, but what is the reason you want to do this? If you can''t be bothered to explain why you need it done, why should we bother to give you an answer?
That''s about as good as you''re going to get, but what is the reason you want to do this? If you can''t be bothered to explain why you need it done, why should we bother to give you an answer?
quote:Original post by johnnyBravo
I want to make my own while type statement, where if i type {} under it , it loops whatever in there (...)
Uhm, that''s exactly how the while() statement works. It loops what''s between the {}s while the contition is true. I''m not sure if I understand you correctly. Do you want an infinite loop? If so, use
while(1)
{
//Do stuff
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement