Forum: Ruby Problem with nested while do

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
9f05b1b9bd41a8f23c9d70703e4fa99c?d=identicon&s=25 Martin G. (mawgaw)
on 2015-12-02 23:56
I am new to Ruby and I can't solve this Problen.

I've got an Array w with 100 Elements called "w".
I want to divide the array with 1..100 divisors.

i = 0
j = 1.0

w.each do |i|

  while j <= 100.0 do

       print i/j,  ", "

       j +=1.0



but it's not working.
Only the first Element of the Array is divided with
the hundred divisors.

I'd be very grateful for help.
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (virtuoso)
on 2015-12-07 21:44
you need to declare "j" inside the "each" loop. By declaring it at the
top, you're reaching j == 100, but you never set it back to 1.
3c8f18f92139d940fa5d3aa047c904ae?d=identicon&s=25 Nivin P. (nivin17)
on 2015-12-28 09:00
You never reset the value of j to 0, and as such, your inner loop
condition is never true after the first run. Assigning j = 0; in the
outer loop afterward should fix it.
9f05b1b9bd41a8f23c9d70703e4fa99c?d=identicon&s=25 Martin G. (mawgaw)
on 2016-01-01 21:34
thanks for the answers,

at the moment I haven't the time to follow it up, but I think my
question is solved

Ef5e284b00aede956c12d4e7bfcf2e92?d=identicon&s=25 Swati S. (swti6690)
on 2016-01-12 12:39
C programming allows to use one loop inside another loop.
6c9767d3611b9a5521cae198e2b003f7?d=identicon&s=25 Giri M. (girimani)
on 2016-03-24 11:34
Swati Sharma wrote in post #1180553:
> C programming allows to use one loop inside another loop.

Very happy to see this blog. Gives a wonderful information with coded
explanaion. Thank you for this blog. very useful to me.
<a href= "">Android Training in
2b7c62500ae5a391a24cd29a64bea1b0?d=identicon&s=25 Shree S. (shreesai)
on 2016-08-18 13:08
very helpful blog to the learners like me thanks a lot...<a
Selenium Training in Chennai</a> | <a
Db52f6f0d6e415bf38664ae7860523ed?d=identicon&s=25 Priya R. (rajapriyainchenni1)
on 2016-09-02 08:05
really nice and helpful aricle to me

training in chennai</a> | <a href="">selenium
training institute in chennai</a> | <a
training in chennai</a> | <a
Fa4beed8e6f878fdbf5b0b4d8fa4cc6a?d=identicon&s=25 MarkC P. (markcphillips)
on 2016-12-07 05:18
Wow. cool post. I’d like to write like this too – taking time and real
hard work to make a great article… but I put things off too much and
never seem to get started. Thanks though.
1a482a77b877e2d16f1c5b55a428d59b?d=identicon&s=25 Hong91 H. (hong91)
on 2017-02-09 04:01
I want you to thank for your time of this wonderful read!!! I definately
enjoy every little bit of it and I have you bookmarked to check out new
stuff of your blog a must read blog!
0da4f0836e81a3a6b9a83c97ebb06c2c?d=identicon&s=25 Mathu M. (mathumitha)
on 2017-02-17 14:43

you can solve the issues by contact our software trainers. Make sure,
They will easily all your programming problems.
F80f81dbae1e9e63fd0f87a68071f99a?d=identicon&s=25 Suresh K. (sureshkuppam)
on 2017-03-02 12:38
Attachment: TOP_6_Fr__1_.jpg (300 KB)
FITA is the best training institute for Android Training in Chennai.
Android Course in Chennai offers the well trained MNC professionals as
trainers. Android Training institute in Chennai offers the placements in
top IT Companies. We have trained more than 10,000+ students from 125+
IT Courses. We are providing the unique teaching methodology. Enroll for
the demo class.
Call Us:@9841746595
B0d20fb7c94ede9223257079e7abf576?d=identicon&s=25 Saravanan R. (linuxsys)
on 2017-03-31 20:00

you can solve the issues by contact our software trainers. Make sure,
They will easily all your programming problems. |
676d9c27bd949be21855a9985f15749c?d=identicon&s=25 Jai T. (jai_technomind)
on 2017-04-01 05:58
56731341751000a995380d1e155d46e8?d=identicon&s=25 Vignes V. (vignesjoseph)
on 2017-04-07 09:09
The reason your inner loop only executes once is because you initialize
j to 0 outside the loop and then never reset it again. After it runs the
first time the value of j is 10. It will never be less than 10 again.

A better way to do this is to use a for loop:

for (int i = 0; i < 10; i++){
    printf("i:%i\n", i);
    for (int j = 0; j < 10; j++){
        printf("j:%i\n", j);
It also makes the code look cleaner.
This topic is locked and can not be replied to.