How to implement "else if" in Decision

Moderator: Benj

Post Reply
ngmflowcode
Posts: 12
Joined: Wed Jan 30, 2013 5:21 pm
Contact:

How to implement "else if" in Decision

Post 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

ngmflowcode
Posts: 12
Joined: Wed Jan 30, 2013 5:21 pm
Contact:

Re: How to implement "else if" in Decision

Post 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

kersing
Valued Contributor
Valued Contributor
Posts: 1940
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Contact:

Re: How to implement "else if" in Decision

Post 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".
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Contact:

Re: How to implement "else if" in Decision

Post 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

ngmflowcode
Posts: 12
Joined: Wed Jan 30, 2013 5:21 pm
Contact:

Re: How to implement "else if" in Decision

Post 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

kersing
Valued Contributor
Valued Contributor
Posts: 1940
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Contact:

Re: How to implement "else if" in Decision

Post 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
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

lightbug
Posts: 5
Joined: Sun Sep 21, 2014 11:56 pm
Contact:

Re: How to implement "else if" in Decision

Post 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)
            }
}

User avatar
SteveM
Posts: 55
Joined: Tue Mar 25, 2014 2:09 pm
Contact:

Re: How to implement "else if" in Decision

Post 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

Post Reply