0
0
Rubyprogramming~10 mins

Until loop in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run the loop until the counter reaches 5.

Ruby
counter = 0
until counter [1] 5
  puts counter
  counter += 1
end
Drag options to blanks, or click blank then click option'
A==
B<
C>=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < makes the loop not run at all.
Using == stops the loop too early or infinite.
2fill in blank
medium

Complete the code to print numbers from 1 to 3 using an until loop.

Ruby
num = 1
until num [1] 3
  puts num
  num += 1
end
Drag options to blanks, or click blank then click option'
A<=
B==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or <= makes the loop not run.
Using == causes an infinite loop.
3fill in blank
hard

Fix the error in the until loop condition to avoid an infinite loop.

Ruby
count = 10
until count [1] 1
  puts count
  count -= 1
end
Drag options to blanks, or click blank then click option'
A<
B>
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > causes the loop not to run.
Using == may cause infinite loop or skip values.
4fill in blank
hard

Fill both blanks to create a loop that counts down from 3 to 1.

Ruby
i = 3
until i [1] 1
  puts i
  i [2] 1
end
Drag options to blanks, or click blank then click option'
A>
B<
C-=
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > in the condition causes the loop not to run.
Using += increases the counter, causing an infinite loop.
5fill in blank
hard

Fill all three blanks to create a loop that prints even numbers from 2 to 6.

Ruby
num = 2
until num [1] 7
  if num [2] 2 == 0
    puts num
  end
  num [3] 2
end
Drag options to blanks, or click blank then click option'
A<
B%
C+=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < in the loop condition stops the loop immediately.
Using -= decreases num causing infinite loop or wrong numbers.