Loops and iterators Flashcards
What is a loop?
The repetitive execution of a piece of code for a given amount of repetitions or until a certain condition is met
What is the key combination to manually intervene a loop?
Ctrl + c
Create an eternal loop!
Loop do
puts “this will keep printing until ctrl + c is hit”
end
How do you quit a loop internally? (+example)
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 can you skip an operation in a loop?
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 does a do/while loop work? (+example)
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)
What is the difference between a “while” and a “for” loop?
“for” loop returns the collection of elements after it executes, whereas “while” loop returns “nil”
How does “while” loop work? (+example)
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!”
Describe the “unless” loop with an example!
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!”
What are “for” loops used for?
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!”
Use “next” in a conditional “while” loop!
x = 0
while x <= 10 if == 3 x += 1 next elsif x.odd? puts x end x += 1 end
=> 1, 5, 7, 9
Use “break” in a conditional “while” loop!
x = 0
while x <= 10 if x == 7 break elsif x.odd? puts x end x += 1 end
=>1, 3, 5
What are iterators?
Methods that naturally loop over a given set of data and allow you to operate on each element in the collection.
Iterate over an array with the “each” method!
fruit = [“banana”, “kiwi”, “apple”, “orange”, “peach”]
fruit.each {|yummy| puts yummy}
=> banana kiwi apple orange peach
How can you design the starting and ending points of blocks?
Either with {} in case of a one line expression and “do” “end” in case of multiple line expression
What happens inside the block and how does the “pipeline variable” work in a block?
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.
What is recursion?
Recursion is the act of calling a method from within itself.
e.g.
def tripler(num)
num * 3
end
tripler(6)
=>18
Write a loop using recursion to see the triple of the previously tripled
number until it’s greater than or equal to 200!
def tripler(num) puts num if num <= 200 tripler(num * 3) end end
tripler(3) =>9 27 81 243 nil
What are the Fibonacci numbers?
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))
Write a method that counts down to zero using recursion!
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
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.’
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.’
What does the “times” method return?
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.
What is the return value of a “while” loop?
nil, unless break is used (the value passed to break)