×

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 Do While Loop

By IncludeHelp Last updated : November 16, 2024

The do while loop

In programming, do...while loop works in the same way as a while loop with a basic difference that the specified Boolean condition is evaluated at the end of the block execution. This result in the execution of statements at least for once and later iterations depend upon the result of the Boolean expression. A process symbol and a Boolean condition is used to make a do...while iteration.

In this loop, at first, the block defined inside the code is executed then the given condition is checked. Due to this property, it is also known as post-test loop.

A do...while loop is an example of the exit-control loop because in this the code is executed before the evaluation of the specified condition. It runs till the Boolean condition stands to be true, once it evaluates to be false the loop gets terminated at that point of time.

Syntax

In Ruby, the do...while loop is implemented with the help of the following syntax:

loop do
  # code block to be executed
  break if Boolean_Expression #use of break statement
end

Example 1: Check Armstrong Number Using do-while Loop

=begin
  Ruby program to check whether the given
  number is Armstrong or not using a do-while loop
=end

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

# Store the original number for comparison
temp = num
sum = 0

# Implementation of the do-while loop using a `loop` construct
loop do
  rem = num % 10       # Get the last digit
  num = num / 10       # Remove the last digit
  sum += rem**3        # Add the cube of the digit to the sum
  puts "Checking......"
  
  break if num == 0    # Exit loop when no digits are left
end

# Check if the sum of cubes equals the original number
if temp == sum
  puts "The number #{temp} is an Armstrong number."
else
  puts "The number #{temp} is not an Armstrong number."
end

Output

First run:
Enter the number
135
Checking......
Checking......
Checking......
The 135 is not Armstrong

Second run:
Enter the number
1
Checking......
The 1 is Armstrong

Example 2: Check for the Presence of the Digit 5 Using a Do-While Loop

=begin
  Ruby program to check whether the given number
  contains the digit `5` using a do-while loop
=end

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

temp = num
flag = false # Flag is a boolean variable to indicate presence of 5

# Implementation of the do-while loop using a `loop` construct
loop do
  rem = num % 10   # Extract the last digit
  num = num / 10   # Remove the last digit

  if rem == 5
    flag = true    # Set flag to true if `5` is found
  end

  puts "Checking......"

  break if num == 0 # Exit the loop when no digits are left
end

if flag
  puts "We have found the presence of five in #{temp}."
else
  puts "We have not found the presence of five in #{temp}."
end

Output

First run:
Enter the number
1234566
Checking......
Checking......
Checking......
Checking......
Checking......
Checking......
Checking......
We have found the presence of five in 1234566

Second run:
Enter the number
198762
Checking......
Checking......
Checking......
Checking......
Checking......
Checking......
We have not found the presence of five in 198762

Comments and Discussions!

Load comments ↻





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