Loops & Iterators Flashcards

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

while

A
repeats an action while a statement is true ie. 
counter = 1
while counter < 11
  puts counter
  counter = counter + 1
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

until

A

repeats an action until a function is true

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

+=

A

increase a variable by a certain amount in each repetition

i.e. i + i += 1

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

for

A

i.e. for every number in a sequence do the following

i.e.
for num in 1…10
puts num
end

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

A

exclusive range.

i.e. 1… 10 will not include 10

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

..

A

inclusive range

i.e. 1.. 10 will include 10

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

loop

A

loops an action

i.e. loop { print “hello World!” }

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

do, end

A

replaces the { } , opens an action and closes an action

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

next

A

skips an action if condition is true

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

break

A

breaks a loop if a condition is true

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

array

A

a range of values all assigned to one variable

i. e. [1, 2, 3, 4, 5]
i. e. [ “a”, “e”, “i”]

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

.each

A

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

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

.times

A

repeats a function a specified number of times

i.e. 5.times { print “I love you”}

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

.split

A

takes in a string and returns an array

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

index

A

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

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

multidimensional array

A

arrays within arrays

ie.
multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

17
Q

hash

A

a substitute for using default numerical indices in arrays. (remember index on an array). You can assign custom values to keys using any object for boh the key and the value.

ie
hash = { “name” = “Eric” }

print hash [“name”]

returns > Eric

18
Q

Hash.new

A

setting a variable equal to Hash.new creates a new, empty hash.

** remember to always capitalize Hash

pets = Hash.new

pets[“Luna”] = “cat”

puts pets[“Luna”]

returns > cat

19
Q

Iterating over arrays

A

languages = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]

languages.each { |code| puts code }

prints out
HTML
CSS
JavaScript
Python
Ruby
20
Q

iterating over a hash

A
lunch_order = {
  "Ryan" => "wonton soup",
  "Eric" => "hamburger",
  "Jimmy" => "sandwich",
  "Sasha" => "salad",
  "Cole" => "taco"
}

lunch_order.each do |name, order|
puts “#{order}”
end

returns:
wonton soup
hamburger
sandwich
salad
taco
21
Q

.to_s

A

converts a value to a string