Jump to content
C4 Forums | Control4

Conditional Statements


Recommended Posts

I'm new to C4 programming and have figured out how to create conditional statements. Having done basic language programming and also worked on the Elan Via Tools system, conditional statements were always If...Then...Else...End If. Am I correct in assuming that C4 only allows If...Then? Or, am I missing something?

Link to comment
Share on other sites



It’s my understanding ( haven’t tried it so can’t be 100% sure) that you need to use the STOP statement as an ELSE condition. Example:

IF (condition is true)
   Do something
   Stop
do something else

So once the stop command is executed all code thereafter is discarded. IOW, if the condition is NOT true the do something else statement would be the only line executed.

 

Link to comment
Share on other sites

3 hours ago, martymohr said:


It’s my understanding ( haven’t tried it so can’t be 100% sure) that you need to use the STOP statement as an ELSE condition. Example:

IF (condition is true)
   Do something
   Stop
do something else

So once the stop command is executed all code thereafter is discarded. IOW, if the condition is NOT true the do something else statement would be the only line executed.

 

Which doesn't cover all of it.

OP: Basically view C4's conditionals as a read script being triggered in order as each line is read.

Example of if/ifnot or if/else:

 

WHEN button is pushed
IF light is on
	do this and this
	stop
do such and such

note that alternatively you can do:

IF light is on
	do this and this
IF light is off
	do such and such

Provided you aren't turning that very light on and off that is!

Example of if/andif:

WHEN button is pushed
IF light is on
	IF door is open
		do this and that

Note the multi layer nested IF statements

Important to note is that the system will continue a script to the end unless told otherwise, as hinted in the first code section. This can be important:

WHEN button is pushed
IF light is on
	turn that light off
IF light is off
	turn that light on

This piece of coding will ALWAYS leave the light on as the system 'reads' and executes line by line. So:

WHEN button is pushed
IF light is on {system checks - yup the light is on, so it knows to read the nested line}
	turn that light off {executes this immediately}
IF light is off {well yeah, it will ALWAYS be off - even if it wasn't before, the system just turned it off!}
	turn that light on {end result, light will never turn off with that button - but it sure will turn on!}

To prevent that from happening there is the STOP command which ends the reading of the current script:

WHEN button is pushed
IF light is on
	turn that light off
	STOP {this then prevents the system from reading any of the script below - so take care here that additional programming is not also stopped}
IF light is off
	turn that light on
Select radio station in the room {ie if you want to have it start music any time the button is pushed - it now will not do it if the light was on, you'd move this line ABOVE the first IF statement instead}

Note that as mentioned before, technically the "if not" conditional is redundant so can be skipped

Of course, it's even easier to just program the button to just toggle the light - the above example is just for simple demonstration purposes :)

Lastly, if/elseif is simply a matter of having multiple IF statements with or without stop commands

Should conditionals be mutually exclusive:

WHEN button is pushed
IF current device is cable box
	whatever you want
IF current device is AppleTV
	whatever you want
IF current device is BLu-Ray
	you get the point

In case of possible overlapping IF statements and having a priority:

WHEN button is pushed
IF the cable box is on
	Do this
	stop
IF the bluray is on {in other words, if the cable box is NOT on but the Bluray is)
	Do something else
	stop
IF the TV is on
	Yet another thing to do....

 

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.