Shiny Language Reference

Language Reference > Basic Language


if...else...

if(expression)
{
    // Code segment 1
}
else
{
    // Code segment 2
}

 

Executes code if a boolean test evaluates to TRUE. Optionally executes alternate code if the test evaluates to FALSE.

The statement in the brackets is evaluated first. If the statement evaluates to TRUE, code segment 1 (above) is executed. If only a single line of code is required the braces ( "{ }" ) may be omitted. If multiple lines of code are required the braces are mandatory.

The "else" clause is optional and will be executed if the statement in the IF clause evaluates to FALSE. If only a single line of code is required the braces ( "{ }" ) may be omitted. If multiple lines of code are required the braces are mandatory.

 

Parameters

expression

Any expression that evaluates (or can reasonably be converted) to a boolean result. If the expression isn't explicitly boolean, implict type conversion is used to convert it.

 

Return Value

No return value, this is a statement.

 

Example Code

if(Day < 10)
{
    print "The first 10 days of the month";
}
else
{
    print "The last 20 days of the month";
}