Conditional Statements

The most commonly used conditional statement in Ruby is the if statement. It contains a conditional expression and a true branch, and it may contain a false branch. The true branch is executed when the conditional expression is true. The false branch, if present, is executed when the conditional expression is false. The following program demonstrates the use of the conditional statement.

We want to write a program that determines eligibility for a driver’s license based on the age input by the user. The eligible age should be 1818 or above.

print("Enter your age: ")
age = Integer(gets) # Taking age input
if age >= 18 # If age is greater than and equal to 18
    print("You are eligible for a driver's license.\n")
else
    print("You are not eligible for a driver's license.\n")
end
Example program for the if statement

The conditional statement used in the program above starts with if, followed by a conditional expression. Line 4 contains the code to be executed if the condition is true. Note the indentation (extra space) at the start of line 4. The word else on line 5 indicates the end of the true branch and the start of the false branch. Line 6 is the code that will be executed if the condition is false. Note the indentation (extra space) at the start of line 6.

Example program

The following example program demonstrates the use of the if statement in a Ruby program.

Sum of the digits

We want to write a program that takes an input number and displays the sum of its digits. We assume that the user will enter a positive number up to four digits.

sum = 0
print("Enter a number with at most 4 digits: ")
number = Integer(gets) # Taking input in variable number
if number > 0 and number <= 9999 # Checking number range between 0 and 9999
    sum +=  number % 10 # Adding remainder to variable sum
    number = number/10 # Adding quotient to the number
    if number > 0
        sum +=  number % 10 # Adding remainder to variable sum
        number = number/10 # Adding quotient to the number
    end
    if number > 0
        sum +=  number % 10 # Adding remainder to variable sum
        number = number/10 # Adding quotient to the number
    end
    if number > 0 
        sum +=  number % 10 # Adding remainder to variable sum
        number = number/10 # Adding quotient to the number
    end
    print("Sum of digits is: #{sum}\n")
else
    print( "Invalid number\n")
end
Calculate the sum of digits

Explanation

  • Line 1: We define a sum integer variable.
  • Line 2: We print a prompt asking for the integer input value.
  • Line 3: We store it in a number variable.
  • Line 4: We use the comparison and logical operators in a conditional expression using an if statement. Lines 5–19 are part of a true branch of this if statement. Note the indentation of these lines. Lines 7, 11, and 15 are considered a nested if statement since their respective true branches are indicated by double indentation.
  • Line 5: We use the compound assignment += and the arithmetic expression.
  • Line 6: We use assignment and integer division as indicated by /.
  • Lines 7–18: We perform the operations of lines 4–6 repeatedly.
  • Line 19: We print the updated value of sum.
  • Line 20: We represent the false branch of the decision using the else statement.
  • Line 21: We print an error message when the user inputs an invalid value.

Practicing the if-else statement

Here are a few examples that can be used to practice writing programs in Ruby. The “Show Solution” button will display a program that solves the respective problem. You can copy and paste this solution into the code widget to execute it. Use the “Show Solution” function to verify that your solution returns the same result as the given solution. If you get the right result but in a different way, your program is still good! There can be several ways to write correct solutions in programming.

Two digits

Write a program that takes a number from the user as input. If it’s between 1010 and 9999, both inclusive, then it will display two digits. Otherwise, it will display not two digits.

Sample input 1

25

Sample output 1

two digits

Sample input 2

100

Sample output 2

not two digits
# Write your code here
Check the input between a specific range

Pay calculator

This program should calculate the pay of an employee based on hours worked. The input includes the employee’s total hours worked per week and their hourly pay rate. The employee will be paid a base wage for the first 40 hours worked and time-and-a-half (150% of base pay) for any hours past 40 as overtime pay. Output the regular pay, overtime pay, and total pay for the week on the screen.

If the employee worked 40 hours or less, then don’t show any output regarding overtime pay.

Sample input 1

30
10

Sample output 1

Regular pay: 300

Sample input 2

50
10

Sample output 2

Regular pay: 400
Overtime pay: 150
Total pay: 550
# Write your code here
Calculate the regular and overtime pay of an employee

Find the quadrant

Write a program that takes a point, (x,y)(x, y), from the user and finds the quadrant where the point lies on a Cartesian plane.

The following illustration will help to understand where each point lies in the Cartesian plane.

Press + to interact
A Cartesian plane
A Cartesian plane

Sample input 1

2
6

Sample output 1

The point lies in the 1st Quadrant.

Sample input 2

-2
-6

Sample output 2

The point lies in the 3rd Quadrant.
# Write your code here
Find the quadrants for a given point (x,y)