Rouge Documentation Help

Conditions

On many occasions it is useful to be able to execute a section of code only when certain conditions are met. For this reason, Rouge provides us with something called an if-statement. The following program prompts the user to input their age, based on which it tells them whether they are allowed to vote in an election.

age = request_number("Please enter your age:") if (age >= 18) { print("You are allowed to vote.") }

An if-statement, as can be seen in the example, looks somewhat similar to a function definition. All if-statements begin with the if keyword, followed by a set of parentheses. The parentheses encase any expression that evaluates to a Boolean value. Finally, there is a set of curly braces that can contain any number of statements. When the language encounters an if-statement, it determines whether the expression, or condition, inside the parenthesis evaluates to true. In this case, the statements inside the body are executed. Should the condition equal false, the language will skip executing any statements inside the body and continue with the next statement after the if-statement has concluded. In the example above, the text "You are allowed to vote." will only appear on the screen in case the condition, signaling that the user is at least 18 years old, is met.

In case you also wanted to display some text in case the user is underage, you could create a second if-statement checking for the opposite case.

age = request_number("Please enter your age:") if (age >= 18) { print("You are allowed to enter.") } if (age < 18) { print("You are not allowed to enter.") }

As you can see, the above example will display a different line of text depending on the age of the user. However, there is a more convenient way to react to the opposite case of the condition by using an if-else statement, as shown in the following example.

age = request_number("Please enter your age:") if (age >= 18) { print("You are allowed to enter.") } else { print("You are not allowed to enter.") }

An if-else statement consists of a regular if-statement extended by an else block. The statements inside the else block are only executed in case the condition of the if-statement is not met.

If-statements can also be nested, in case you wanted to perform multiple checks in series. For instance, the following program does not handle all underage users the same. Instead, it differentiates between users 16 and up, who are allowed to participate in youth elections, and users younger than that, who are not allowed a vote at all. Both of these cases are only checked in case the user is less than 18 years old to begin with, due to the nested if-statement.

age = request_number("Please enter your age:") if (age >= 18) { print("You are allowed to enter.") } else { if (age >= 16) { print("You are allowed to participate in youth elections.") } else { print("You are not allowed to enter.") } }
Last modified: 07 January 2026