Loops & Iterators Flashcards
while
repeats an action while a statement is true ie. counter = 1 while counter < 11 puts counter counter = counter + 1 end
until
repeats an action until a function is true
+=
increase a variable by a certain amount in each repetition
i.e. i + i += 1
for
i.e. for every number in a sequence do the following
i.e.
for num in 1…10
puts num
end
…
exclusive range.
i.e. 1… 10 will not include 10
..
inclusive range
i.e. 1.. 10 will include 10
loop
loops an action
i.e. loop { print “hello World!” }
do, end
replaces the { } , opens an action and closes an action
next
skips an action if condition is true
break
breaks a loop if a condition is true
array
a range of values all assigned to one variable
i. e. [1, 2, 3, 4, 5]
i. e. [ “a”, “e”, “i”]
.each
performs a function on each element of an array
i.e.
array = [1,2,3,4,5]
array.each do |x|
x += 10
puts “#{x}”
end
.times
repeats a function a specified number of times
i.e. 5.times { print “I love you”}
.split
takes in a string and returns an array
index
each value in an array has an index number starting from 0, 1, 2, 3….
i.e
my_array = [hi, my, name, is, jordan]
array [2]
returns -> name