Control structures are fundamental in programming as they allow you to control the flow of your program. In Ruby, control structures include conditionals and loops, which help you make decisions and repeat actions. This section will cover the following topics:

  1. If-Else Statements
  2. Unless Statements
  3. Case Statements
  4. Ternary Operator

  1. If-Else Statements

The if-else statement is used to execute code based on a condition. If the condition evaluates to true, the code inside the if block is executed. Otherwise, the code inside the else block is executed.

Syntax

if condition
  # code to execute if condition is true
else
  # code to execute if condition is false
end

Example

age = 18

if age >= 18
  puts "You are eligible to vote."
else
  puts "You are not eligible to vote."
end

Explanation

  • age >= 18 is the condition.
  • If age is greater than or equal to 18, the message "You are eligible to vote." is printed.
  • Otherwise, the message "You are not eligible to vote." is printed.

  1. Unless Statements

The unless statement is the opposite of the if statement. It executes the code block only if the condition is false.

Syntax

unless condition
  # code to execute if condition is false
else
  # code to execute if condition is true
end

Example

age = 16

unless age >= 18
  puts "You are not eligible to vote."
else
  puts "You are eligible to vote."
end

Explanation

  • age >= 18 is the condition.
  • If age is not greater than or equal to 18, the message "You are not eligible to vote." is printed.
  • Otherwise, the message "You are eligible to vote." is printed.

  1. Case Statements

The case statement is used to execute one block of code out of many based on the value of an expression. It is similar to the switch statement in other programming languages.

Syntax

case expression
when value1
  # code to execute if expression == value1
when value2
  # code to execute if expression == value2
else
  # code to execute if expression does not match any value
end

Example

day = "Saturday"

case day
when "Monday"
  puts "Start of the work week."
when "Friday"
  puts "End of the work week."
when "Saturday", "Sunday"
  puts "Weekend!"
else
  puts "Midweek day."
end

Explanation

  • day is the expression.
  • If day is "Monday", the message "Start of the work week." is printed.
  • If day is "Friday", the message "End of the work week." is printed.
  • If day is "Saturday" or "Sunday", the message "Weekend!" is printed.
  • If day does not match any of the specified values, the message "Midweek day." is printed.

  1. Ternary Operator

The ternary operator is a shorthand for the if-else statement. It is used to execute one of two expressions based on a condition.

Syntax

condition ? expression_if_true : expression_if_false

Example

age = 20
message = age >= 18 ? "You are eligible to vote." : "You are not eligible to vote."
puts message

Explanation

  • age >= 18 is the condition.
  • If age is greater than or equal to 18, the expression "You are eligible to vote." is assigned to message.
  • Otherwise, the expression "You are not eligible to vote." is assigned to message.
  • The value of message is then printed.

Practical Exercises

Exercise 1: If-Else Statement

Write a Ruby program that checks if a number is positive, negative, or zero.

Solution

number = -5

if number > 0
  puts "The number is positive."
elsif number < 0
  puts "The number is negative."
else
  puts "The number is zero."
end

Exercise 2: Case Statement

Write a Ruby program that takes a grade (A, B, C, D, F) and prints the corresponding message.

Solution

grade = "B"

case grade
when "A"
  puts "Excellent!"
when "B"
  puts "Good job!"
when "C"
  puts "Well done!"
when "D"
  puts "You passed."
when "F"
  puts "Better luck next time."
else
  puts "Invalid grade."
end

Exercise 3: Ternary Operator

Write a Ruby program that checks if a person is an adult (age 18 or older) using the ternary operator.

Solution

age = 17
message = age >= 18 ? "You are an adult." : "You are not an adult."
puts message

Common Mistakes and Tips

  • Common Mistake: Forgetting to use elsif instead of else if in Ruby.
    • Tip: Remember that Ruby uses elsif for additional conditions in an if statement.
  • Common Mistake: Using = instead of == for comparison.
    • Tip: Use == to compare values and = to assign values.

Conclusion

In this section, you learned about control structures in Ruby, including if-else, unless, case statements, and the ternary operator. These constructs allow you to control the flow of your program based on conditions. Practice the exercises to reinforce your understanding, and you'll be ready to move on to loops in the next section.

© Copyright 2024. All rights reserved