I’m brand new to programming and Ruby is my first language. One exercise I’m working on is to create a multiplication table that does the following:
1x1 = 1
1x2 = 2
(etc)
2x1 = 2
2x2 = 4
I figured I’d do this by creating a nested while loop:
a = 1
b = 1
while a <= 5
while b <= 5
puts "#{a} * #{b} = #{a * b}"
b += 1
end
a += 1
end
When I run the script it prints the first set of times table (1×1 – 1×5) then stops. It doesn’t iterate the parent loop. What am I doing wrong?
You never reset your
bto1. Look at the secondwhileloop:At the end of this loop,
b = 6, and the loop exits. Thena += 1is executed, and our outer loop begins. On all the next inner loop iterations,b = 6however, and therefore isn’t executed. Thus you need: