when and foreach loop

So one night i was thinking of something I can't remember now and suddenly it came to me! A when loop!!! its like a while loop except it would be like
1
2
3
4
when(timer_s; 0; 20)
{
    //execute code
} 

so how its read is the first shows that its being used as a timer in seconds and then the start time and then the end. and when it reaches the end (in this case 20) it executes code. How hard would this be to make? and how hard would it be to add an associative array and foreach loop from php?
closed account (zb0S216C)
So what's supposed to happen when the condition is not yet met? For instance:

1
2
3
4
5
6
7
8
when( /* the beans are hot */ )
{
    /*
     * Eat the beans and wash the dishes.
     * But! the beans aren't hot yet, so what
     * does one do while their waiting?
     */
} 

In effect, the program stalls at the sight of the when loop. Of course, in performance intensive programs, such as games, the construct is not ideal.

I think either the for or while construct is preferred since they allow something productive (I hope) to be done while a condition is not met.

Wazzak
i didn't think about that and can't come up with a good reason to use it. Pretty much all I thought about was design. shoot
@Framework,
I think when would be asynchronous, so the program would jump to the end of the block. Then it would just jump back to the start once the condition was met.

It would be similar to how anonymous event handlers look in C#:
ExitButton.Pressed += delegate { Environment.Exit(0); };
would be equivalent to:
when (ExitButton.Pressed) { Environment.Exit(0); };

[edit] In fact, I think I could seamlessly integrate when into C/C++ using the preprocessor and setjmp/longjmp.
Last edited on
closed account (zb0S216C)
@chrisname: What's wrong with a simple if and a function call? Is that not the same thing? Wouldn't you be creating a function minus the parameters and return statement?

Edit:
1
2
3
4
5
6
7
void EatBeans [[noreturn]] ( )
{
    /* Eat the beans. */
}

if( /* the beans are hot */ )
    EatBeans( );

Wazzak
Last edited on
Yeah, but that's how you're supposed to do it. This isn't, which makes it cooler.

I've got it mostly working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

#define when(expression)\
start_when:\
	static int __when_checked__, __when_done__;\
	if (!(expression)) {\
		goto end_when;\
	} else
#define meanwhile \
end_when:\
	if (!(__when_checked__) && (__when_checked__ = 1))
#define endwhen \
check_when:\
	if (!(__when_done__)) {\
		__when_done__ = 1;\
		goto start_when;\
	}

int main()
{
	int output = 0;
	std::string name;
	when (output) {
		std::cout << "Hello, " << name << "!" << std::endl;
	} meanwhile {
		std::cout << "What's your name? " << std::flush;
		std::getline(std::cin, name);
		output = 1;
	} endwhen;
	return 0;
}


It will ask you your name and then say hi in the correct order, but then it loops back and I can't figure out how to fix that (fixed). Also, you can only use one when block per function, and you have to have a meanwhile block, and the whole thing has to end with endwhen. Still, not bad for an hour's work (less if you cut out the time I spent playing with setjmp/longjmp).
Last edited on
so most of this is beyond me so I would just like to make it clear that the when loop will only execute the code when the condition is met. like a while with an if statement.
That's sort of what that does, it's just not very good. What you're looking for is called asynchronous (or event-driven) execution. Here's a good explanation of asynchronous and synchronous execution: http://www.bisque.com/help/CCDSoft/asynchronous_vs._synchronous_execution.htm

The code I posted above is actually executed synchronously* but I used the goto statement to obscure that and make it look like it's executed asynchronously. Just as a note; goto should not be used like that under normal circumstances. It's best to avoid it as much as possible, because a lot of the time it can be confusing (like in my code above). It's okay to use it as a joke like that, but in real code it should normally be avoided. There are situations where using it is good, if not better than the alternatives, but that doesn't happen often.

*Technically all programs on modern CPUs are executed asynchronously; the CPU breaks down instructions into smaller steps, and then it executes all of the instruction step-by-step, almost in parallel. For example, if you have ten instructions that can each be broken down into 5 steps, you could do step 1 of each instruction first, then step 2 for each instruction, and then step 3 and so on. The CPU makes sure to do it in such a way that it gives the impression that the instructions were executed one-by-one like you might expect. It's called instruction pipelining and it's usually faster to do it that way. The Wikipedia article at http://en.wikipedia.org/wiki/Instruction_pipeline is better at explaining than I am.
This reminds me of comefrom. Go ahead, look it and get your mind blown.
thats amazing
so I'm actually very far on a working prototype, but I'm stuck on one thing. i can't figure out a data type for time that i can use in the expression. for right now i have to use cycles but i would like to use it in for seconds until it hits the nth second. how do i do that?
Topic archived. No new replies allowed.