×

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 program to demonstrate the nested 'do' loop

Last Updated : December 15, 2025

Problem Solution

In this program, we will print tables from 2 to 5 using a nested "do" loop.

Program/Source Code

The source code to demonstrate the nested "do" loop is given below. The given program is compiled and executed successfully.

#Ruby program to demonstrate the 
# nested "do" loop. 

cnt1=2;
cnt2=0;

loop do
    cnt2=1;

    loop do
        print (cnt1*cnt2)," ";
        cnt2=cnt2+1;
        if cnt2>10
            break;
        end
    end

    cnt1=cnt1+1;
    puts;
    
    if (cnt1>5)
        break;
    end
end

Output

2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50

Explanation

In the above program, we created two variables cnt1, cnt2 that are initialized with 2, 0 respectively. Then we used a nested "do" loop to print tables from 2 to 5.

Ruby Looping Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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