Loops and iterators Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a loop?

A

The repetitive execution of a piece of code for a given amount of repetitions or until a certain condition is met

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

What is the key combination to manually intervene a loop?

A

Ctrl + c

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

Create an eternal loop!

A

Loop do
puts “this will keep printing until ctrl + c is hit”
end

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

How do you quit a loop internally? (+example)

A

With the “break” keyword (note that it only breaks out of the loop, not of the whole program)

e.g.

i = 0
loop do
i += 2
puts i
if i == 10
break
end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you skip an operation in a loop?

A

With the “next” keyword

e.g.

i = 0
loop do
i += 2
if i == 4
next 
end
puts i
if i == 10
break 
end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does a do/while loop work? (+example)

A
Do/while loop goes on until the condition is not met, but the code gets executed at least once
e.g.
loop do
puts "Do you wanna do it again"
answer = gets.chomp
if answer != "Y"
break 
end
end

(The loop keeps going as long as your input is “Y”, and breaks if it’s anything else)

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

What is the difference between a “while” and a “for” loop?

A

“for” loop returns the collection of elements after it executes, whereas “while” loop returns “nil”

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

How does “while” loop work? (+example)

A

A while loop is given a parameter that evaluates to a boolean. If false, the while loop is not executed again, and the program continues after the while loop.

e.g.
x = gets.chomp

while x > 0
puts x
x += 1
end

puts “Done!”

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

Describe the “unless” loop with an example!

A

It’s another way to express things

It is the opposite of the “while” loop.
e.g.
x = gets.chomp

until x < 0
puts 0
x -= 1
end

puts “Done!”

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

What are “for” loops used for?

A

For loops are used to loop over a collection of elements.
They have a definite end since it’s looping over a finite number of elements.
e.g.
x = [1, 2, 3, 4, 5]

for i in x do
puts i
end

puts “Done again!”

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

Use “next” in a conditional “while” loop!

A

x = 0

while x <= 10
if == 3
x += 1
next
elsif x.odd?
puts x
end
x += 1
end

=> 1, 5, 7, 9

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

Use “break” in a conditional “while” loop!

A

x = 0

while x <= 10
if x == 7
break
elsif x.odd?
puts x
end
x += 1
end

=>1, 3, 5

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

What are iterators?

A

Methods that naturally loop over a given set of data and allow you to operate on each element in the collection.

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

Iterate over an array with the “each” method!

A

fruit = [“banana”, “kiwi”, “apple”, “orange”, “peach”]

fruit.each {|yummy| puts yummy}

=>
banana
kiwi
apple
orange
peach
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you design the starting and ending points of blocks?

A

Either with {} in case of a one line expression and “do” “end” in case of multiple line expression

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

What happens inside the block and how does the “pipeline variable” work in a block?

A

Each time we iterate over the array, we need to assign the value of the element to a variable. (e.g. |number|) After that, we write the logic that we want to use to operate on the variable, which represents the current array element.

17
Q

What is recursion?

A

Recursion is the act of calling a method from within itself.

e.g.
def tripler(num)
num * 3
end

tripler(6)
=>18

18
Q

Write a loop using recursion to see the triple of the previously tripled
number until it’s greater than or equal to 200!

A
def tripler(num)
puts num
if num <= 200
tripler(num * 3)
end
end
tripler(3)
=>9
27
81
243
nil
19
Q

What are the Fibonacci numbers?

A

It is a sequence of numbers in which each number is the sum of the previous two numbers in the sequence.
(F0 = 0, F1 = 1) and (Fn = (Fn -1) + (Fn -2))

20
Q

Write a method that counts down to zero using recursion!

A
def count_to_zero(num)
if num <= 0
puts num
else 
puts num 
count_to_zero(num - 1)
end
end
count_to_zero(4)
=>4
3
2
1
0
21
Q

Modify the code so each loop stops after the first iteration.

loop do
puts ‘This is the outer loop.’

loop do
puts ‘This is the inner loop.’
end
end

puts ‘This is outside all loops.’

A

loop do
puts ‘This is the outer loop.’

  loop do
    puts 'This is the inner loop.'
    break
  end
break
end

puts ‘This is outside all loops.’

22
Q

What does the “times” method return?

A
It returns the initial integer (it starts counting from zero)
e.g. 
def stars
3.times do |count|
puts count
end
end

puts stars

0
1
2
3
It should end at 2, but since the initial integer is returned, it's gonna be the return value of the 'stars' method.
23
Q

What is the return value of a “while” loop?

A

nil, unless break is used (the value passed to break)