Javatpoint Logo
Javatpoint Logo

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:

Ruby if else 1

Example:

Output:

Ruby if else 2


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:

Ruby if else 3

Example:

Output:

Ruby if else 4


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:

Ruby if else 5

Example:

Output:

Ruby if else 6


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:

Ruby if else 7


Next TopicRuby case




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA