Page 1 of 1

How to implement "else if" in Decision

Posted: Sun Feb 03, 2013 6:32 am
by ngmflowcode
I noticed a simple ability to create "if" statements with the Decision block, but I can't figure out how to do multiple conditions without using multiple decision blocks.

In C, one can write an if statement with multiple else if's but I don't see that option here. Maybe the switch would be more useful?

Suggestions are appreciated.

-NGM

Re: How to implement "else if" in Decision

Posted: Sun Feb 03, 2013 6:56 am
by ngmflowcode
From the documentation it is looking like I do need to use the switch block, however operators like =, >, <, etc... don't seem to work here. In addition, only constants appear in the drop down, it does not list the Global variables.

This is what I thought would work, but my syntax is wrong:

Image

The help file does not show any example usage.

-NGM

Re: How to implement "else if" in Decision

Posted: Sun Feb 03, 2013 11:29 am
by kersing
Switch can only be used for single values, not ranges, just like the C equivalent. Multiple decision blocks can be nested, that is basically what you do in C as well

Code: Select all

if ( condition ) {
        code1;
} else if ( condition2 ) {
        code2;
}
Is equivalent to adding two decision blocks, with code1 in the "yes" flow and the second decision in the "no".

Re: How to implement "else if" in Decision

Posted: Sun Feb 03, 2013 10:34 pm
by JonnyW
Hello.

If you are familiar with C, and as you are using v5 it may help to have a look at the C code generated for decision blocks. You can drag the tabs to the right of the window or use the options in the 'Window' menu to create a split-view and see the C code along with the Flowchart.

There is an example showing C code viewing here:
[/youtube]

Cheers,

Jonny

Re: How to implement "else if" in Decision

Posted: Wed Feb 13, 2013 8:45 pm
by ngmflowcode
So I basically need to hand code this in C? It's not that difficult, but I thought this would be a basic feature in Flowcode.

-NGM

Re: How to implement "else if" in Decision

Posted: Wed Feb 13, 2013 9:47 pm
by kersing
That is not what I was trying to tell you at all.

Question for you:
How would you code the following in C? (Flowcode equivalent will be provided after you've answered :-) )
if i between 20 and 30 call subroutineA
else if i between 40 and 50, add 150 to i
else if i between 100 and 200, subtract 100 from i

Re: How to implement "else if" in Decision

Posted: Mon Sep 22, 2014 12:46 am
by lightbug
the best way IMO is using blocks
ngmflowcode wrote:From the documentation it is looking like I do need to use the switch block, however operators like =, >, <, etc... don't seem to work here. In addition, only constants appear in the drop down, it does not list the Global variables.

This is what I thought would work, but my syntax is wrong:

Image

The help file does not show any example usage.

-NGM
it's easy in C code
in your example.

Code: Select all

if(>0)
{
    if(>26)
    {
         if(>50)
        {
                ..........
              (and so on...)
                 ..........
        }else{
                 (case 2)
                }
    }else{
           (case 1)
            }
}

Re: How to implement "else if" in Decision

Posted: Mon Sep 22, 2014 12:16 pm
by SteveM
Hi all,
Nesting "if ... else if ... else if ... else ..." is quite simple using just the decision icons. The thing to remember is that the 'No' branch from the decision icons is equivalent to 'else' - anything you put in that branch is executed only when the condition is false. The structure ends up looking something like this...
Nesting ifs.PNG
(28.86 KiB) Downloaded 5976 times