Looping Flashcards

1
Q

How do you make a loop method using the loop construct?

A
loop do
  #your code here
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How many times will the following code run?

loop do
  #your code here
end
A

An infinite amount of times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many times will the following code run?

loop do
  #your code here
  break
end
A

Once

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How many times will the following code run?

counter = 0
loop do
counter = counter + 1
puts “Iteration #{counter} of the loop”

if counter >= 10
break
end
end

A

10 times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can we rewrite the following code using += instead?

counter = counter + 1

A

counter += 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the following code do to the variable age?

age = 20
age += 1

A

Reassigns the variable age to the value of 21

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you make a loop method using the times construct?

A

5.times do
puts “Penguins like to jump off icebergs!”
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How many times will the following code run?

7.times do
puts “I am doing the dishes left by my former friends.”
end

A

7 times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will the value of jewels_in_bag be after the code has run?

jewels_in_bag = 100

3.times do
puts “Hiding 10 stolen jewels.”
jewels_in_bag = jewels_in_bag - 10
end

A

70

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can we rewrite the following code using -= instead?

jewels_in_bag = jewels_in_bag - 10

A

jewels_in_bag -= 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many times will the following code run?

x=0
while x < 10 do
puts “so many loops”
end

A

An infinite amount of times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or False: The line in the following loop puts “Hi” will never run.

10.times do
break
puts “Hi”
end

A

True

The break stops the loop from reaching the line puts “Hi”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you read this code in sentences?

counter = 0
while counter < 20
  puts "The current number is less than 20."
  counter += 1
end
A

Variable counter is 0.

While counter is less than 20, output “The current number is less than 20.”, then increment counter by 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you read this code in sentences?

counter = 0
until counter == 20
  puts "The current number is less than 20."
  counter += 1
end
A

Variable counter is 0.

Until counter is equal to 20, output “The current number is less than 20.”, then increment counter by 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is the until loop different from the while loop?

A

Until is simply the inverse of a while loop. An until keyword will keep executing a block until a specific condition is true. In other words, the block of code following until will execute while the condition is false. One helpful way to think about it is to read until as “if not”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly