IF ELSE complex conditions

hello everyone,

I need to get a complex if conditions, is there a way to do the following? (I am using C, as I am more used to it)

// Online C compiler to run C program online

#include <stdio.h>
#include <stdbool.h>

int fn_selector(bool input1, bool input2, bool input3)
{
if(input1 == input2 && input2 == input3 && input1 == input3)
{return 4;}

else if(input1 == input2)
	{return 1;}
else if(input2 == input3)
	{return 2;}
else if(input1 == input3)
	{return 3;}

else
	{return 0;}

}

int main() {
// Write C code here
printf(“started\n”);
bool input1 = true;
bool input2 = false;
bool input3 = true;
int res;
res = fn_selector(input1, input2, input3);
printf(“%d\n”, res);
return 0;
}

How can I do that?

1 Like

If I understand it correctly, you basically just go backwards. The first if statement becomes the last one you check:

if-else.vl (13.5 KB)

@seltzdesign ahhhhhh! interesting! almost there! thank you for your suggestion!

So yes, the patch you sent me is working almost.

The thing is : the result of the conditions is the same when the 3 bool are all On AND all Off!! Which is not the expected out. And it also means that when bool #1 and bool #2 will output a result either it is both on AND off
BTW, Is it possible to write a code in c# and turn it in a node somehow? I vaguely remember we could do that in beta…

@seltzdesign Found it! it was actually easy!!! thank you so much!!!

1 Like

yes, you can also simply add it as c# function ;)

2 Likes

Great, glad I could lead you on the right track to solve it :)

I have to say that if-then-else stuff is always a bit meh in vvvv, because they are also not very intuitively readable, but you don’t have those cases very often I find.

There was also some talk about a Case node/region, but I don’t think it ever made it in.

For the most part you usually find much better routes to take without having to use so many ifs after another.

1 Like

This is a good example of where to use expressions

3 Likes

@sebescudie I’m getting an error on my side with the if statement. I’m probably doing something wrong, but would you mind taking a look?

Function [if] unknown in expression : [if (a == b){ return 1;}else { return 2;}]

Expressiontry.vl (7.8 KB)

Looking at the underlying lib’s doc, the if only seems to be supported by the ScriptEvaluator. The Expr node only implements an ExpressionEvaluator, will see if that can be changed.

edit @Hadasi yep, looks like it’s working!

@bjoern Expect a PR soon :)

4 Likes

Thanks, merged. Not sure when there’ll be a release though. I want to add some other stuff.

4 Likes