Operator: Ternary
Operator: Ternary

Operator: Ternary

Evaluates an expression and returns the first expression if it evaluates to true (or is true-equivalent), or the second expression if it evaluates to false. Resultant value type depends on the expression evaluated after the test expression.

This does not evaluate both potential results - either the first expression is evaluated or the second.

Example

Ternary Example


action general a_testtrue named "test true";
action general a_testfalse named "test false";
action general a_quit named "quit";

world
{
	function firstResult()
	{
		textln("Calling first result: 10");
		return 10;
	}
	
	function secondResult()
	{
		textln("Calling second result: 20");
		return 20;
	}

	function testExpressions(value)
	{
		textln("Passing " + value + "...");

		textln(value + " ? \"apple\" : \"banana\"");
		textln(value ? "apple" : "banana");

		textln(value + " ? firstResult() : secondResult()");
		textln(value ? firstResult() : secondResult());
	}

	onAction(a_testtrue)
	{
		testExpressions(true);
	}

	onAction(a_testfalse)
	{
		testExpressions(false);
	}

	onAction(a_quit)
	{
		quit;
	}

	start()
	{
		textln("Type \"test true\", \"test false\", or \"quit\".");
	}
	
}
×

Modal Header