XtGem Forum catalog
/ /

XtScript: Conditional Operators

Learn how to make different output of your XtScript code based on conditional arguments given

Conditional operators is a way to make a dynamic output of our XtScript codes based on the certain conditions included.

If you are familiar with conditional operator in Javascript or PHP, you will not find hard time in learning conditional operators in XtScript.

Conditional operation in XtScript is begin with if and closed with endif.

XtScript Conditional Operators
CommandSyntax
ifif condition
# Code to be executed if condition true
endif
if .. else ..if condition
# Code to be executed if condition true
else
# Code to be executed if condition false
endif
if .. elseif .. elseif condition
# Code to be executed if condition true
elseif condition 2
# Code to be executed if first condition false and condition 2 true
else
# Code to be executed if all condition false
endif

Learn XtScript Conditional Operators from examples

XtScript if

- Code:
<!--parser:xtscript-->
	var $fruit = Banana

	if $fruit == Apple
		print I have an Apple
	endif

	print <p>The print command inside <b>if</b> is not been executed because \$fruit value is not Apple, but Banana</p>
<!--/parser:xtscript-->
- Result:

The print command inside if is not been executed because $fruit value is not Apple, but Banana

== in the if condition above is a logical operator. if $fruit == Apple is means "if variable $fruit is equal to Apple". We can learn more about XtScript logical operators later in the next chapter.

XtScript if .. else

- Code:
<!--parser:xtscript-->
	var $fruit = Banana
	var $question = Do you have an Apple?

	if $fruit == Apple
		print $question Yes, I have an Apple
	else
		print $question No, I only have $fruit
	endif
<!--/parser:xtscript-->
- Result:
Do you have an Apple? No, I only have Banana
XtScript if .. elseif .. else

- Code:
<!--parser:xtscript-->
	var $money = 1000

	if $money < 500
		print <p>I have $money USD. I can not buy a new smartphone</p>
	elseif $money > 500
		print <p>I have $money USD. I can buy a new smartphone</p>
	else
		print I dont' have any money
	endif
<!--/parser:xtscript-->
- Result:

I have 1000 USD. I can buy a new smartphone

> is a logical operator. if $money > 500 means "if the value of $money variable is more than 500". We can learn more about XtScript logical operator later in the next chapter.