#### Archived

This topic is now archived and is closed to further replies.

# Is there a way to make a function similar to while statement?

This topic is 5151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

You know how the while statement you put the: { //code } and it loops between the two {}. Is there a way to manually do that or is that just built into the language? thanks,

##### Share on other sites

I''m not sure I completely understand your question, but you could just do something like this:

while (condition)
{
myFunction();
}

or, like this:

void myFunction()
{
while (condition)
{
// code for the function goes here
}
}

##### Share on other sites
Uh... what exactly are you talking about? Do you mean like:
    int i = 10;label:    {        bla         bblah    }    if(i--) goto label    ...
Forgive me Lord, for I have sinned.

##### Share on other sites
you know how the while statement works.

You say while then you have the {} and it loops whatever inbetween the {}

I want to make my own while type statement, where if i type {} under it , it loops whatever in there, i know people are going to say use the while, there is a reason that i cant be bothred explaining why i want to do this.

##### Share on other sites
Yes...*cough* goto *cough* would be the way to do it. While loops are much much much much much better.

##### Share on other sites
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,
Not really possibly, if I currectly understand what you''re after
quote:
i know people are going to say use the while, there is a reason that i cant be bothred explaining why i want to do this.
If so, I cannot be bothered to explain in more detail.

while(cond)
{
foo();
}

==

for(;cond
{
foo();
}

##### Share on other sites
Or even recursion, I don''t recommend it though, for most cases.

void Loop(){    //Do stuff    if(condition)        Loop();}

##### Share on other sites
quote:
Original post by fredizzimo
Or even recursion, I don''t recommend it though, for most cases.

At least not in a compiler that doesn''t optimize tail recursion.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

##### Share on other sites
If for some strange reason, you want one while loop to do different things at different times, you could use function pointers... like this:

void DoSomething(){}void DoSomethingElse(){}void GoofyWhile(void (*func)()){   while (whatever)   {      func();   }}int main(){    GoofyWhile(DoSomething);    GoofyWhile(DoSomethingElse);    return 0;}

At least I think that''s how the syntax would look... check out funtion-pointer.org for more info

##### Share on other sites
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.

##### Share on other sites
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:
// 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]

##### Share on other sites
#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?

##### Share on other sites
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
}