0
0
Rubyprogramming~10 mins

While 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 print numbers from 1 to 3 using a while loop.

Ruby
i = 1
while [1]
  puts i
  i += 1
end
Drag options to blanks, or click blank then click option'
Ai == 4
Bi < 4
Ci > 4
Di != 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that never becomes false, causing an infinite loop.
Using the wrong comparison operator.
2fill in blank
medium

Complete the code to stop the loop when the variable reaches 5.

Ruby
count = 0
while count [1] 5
  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 greater than operator which causes the loop to never run.
Using equality operator which runs only when count equals 5.
3fill in blank
hard

Fix the error in the loop condition to print numbers from 1 to 4.

Ruby
num = 1
while num [1] 5
  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 > causes the loop to not run or run infinitely.
Using <= prints an extra number 5.
4fill in blank
hard

Fill both blanks to create a while loop that prints even numbers from 2 to 8.

Ruby
i = 2
while i [1] 8
  puts i
  i [2] 2
end
Drag options to blanks, or click blank then click option'
A<=
B+=
C<
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= misses printing 8.
Using -= decreases i causing an infinite loop.
5fill in blank
hard

Fill all three blanks to create a while loop that prints numbers from 10 down to 6.

Ruby
n = 10
while n [1] 5
  puts [2]
  n [3] 1
end
Drag options to blanks, or click blank then click option'
A>
Bn
C-=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causes the loop to never run.
Printing a wrong variable or not decreasing n causes infinite loop.