Control Structures – if, elsif, else Statements in Ruby

Control Structures – If, elsif, else Statements in Ruby

Control Structure statement is the statement that controls the working flow of any program depending on its condition.


Today in this article we will discuss the Basic Control Statements of Ruby i.e. if, elsif, else.


if Statements

You can use an if expression to execute the lines of code when a certain condition met. Each if has to have a corresponding end.
For Example:

The if modifiers can also be used to execute code.
For example:

elsif Statements

The elsif (short form of else if) block is useful when you want to test multiple if conditions.
A series of if elsif expressions can have a final else block, which is called if none of the if or elsif expressions are true. When an elsif block executes the entire if expression is exited.
For example:

else Statements

An else block is a block that will be executed when none of the above it’s if or elsif conditions evaluates to false. The end keyword is only needed for the if statement, as the else block is part of the if expression.
For example:

Leave a Reply