76
Ruby If-else Statement
The Ruby if else statement is used to test condition. There are various types of if statement in Ruby.
- if statement
- if-else statement
- if-else-if (elsif) statement
- ternay (shortened if statement) statement
Ruby if statement
Ruby if statement tests the condition. The if block statement is executed if condition is true.
Syntax:
Example:
Output:
Ruby if else
Ruby if else statement tests the condition. The if block statement is executed if condition is true otherwise else block statement is executed.
Syntax:
Example:
Output:
Ruby if else if (elsif)
Ruby if else if statement tests the condition. The if block statement is executed if condition is true otherwise else block statement is executed.
Syntax:
Example:
Output:
Ruby ternary Statement
In Ruby ternary statement, the if statement is shortened. First it evaluats an expression for true or false value then execute one of the statements.
Syntax:
Example:
Output:
Next TopicRuby case