×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby until loop with examples

By IncludeHelp Last updated : November 16, 2024

Ruby Until Loop

The until loop is one of the great features of Ruby which makes it different from other programming languages. The support of until loop specifies how much user-friendly language Ruby is?

The until loop is just the opposite of while loop if we justify it in terms of functionality. while loop is executed as long as the Boolean condition stands to be false but in case of until loop, it executes as long as the Boolean condition does not come out to be true. It is the example of the entry-control loop where the specified condition is checked prior to the execution of the loop's body.

Syntax

The until loop is implemented with the help of the following syntax:

until conditional [do]
  # code to be executed
end

Example 1: Print a Message 10 Times Using until Loop

=begin
  Ruby program to print a message 10 times using until loop
=end

num = 0

# Using an until loop to repeat the message
until num == 10
  puts "Hello there! Message from Includehelp.com! Happy learning!"
  num += 1 # Increment the counter
end

Output

Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!
Hello there! Message from Includehelp.com! Happy learning!

Example 2: Sum of All Digits of a Given Number Using until Loop

=begin
  Ruby program to find the sum of all
  digits of a given number using until loop
=end

puts "Enter the number:"
num = gets.chomp.to_i

temp = num
sum = 0

# Implementation of until loop
until num == 0
  rem = num % 10   # Extract the last digit
  num = num / 10   # Remove the last digit
  sum += rem       # Add the digit to the sum
end

puts "The sum of the digits in #{temp} is #{sum}."

Output

Enter the number
456672
The sum of 456672 is 30

Example 3: Count of Even and Odd Digits Using until Loop

=begin
  Ruby program to find the count of even
  and odd digits in the given number
  using until loop
=end

puts "Enter the number:"
num = gets.chomp.to_i

temp = num
even_count = 0
odd_count = 0

# Implementation of until loop
until num == 0
  rem = num % 10    # Extract the last digit
  num = num / 10    # Remove the last digit

  if rem % 2 == 0
    puts "#{rem} is even"
    even_count += 1 # Increment even count
  else
    puts "#{rem} is odd"
    odd_count += 1  # Increment odd count
  end
end

puts "Number of even digits: #{even_count}"
puts "Number of odd digits: #{odd_count}"

Output

Enter the number
7896532
2 is even
3 is odd
5 is odd
6 is even
9 is odd
8 is even
7 is odd
Number of even numbers are 3
Number of odd numbers are 4

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.