Loops Quiz

  # A
  i = 1
  while i < 0
    # ...
    i += 1
  end

  # B
  i = 1
  until i == 100
    # ...
    i += 1
  end

  # C
  i = 1
  while i > 0
    # ...
    i += 1
  end

Which of the above are infinite loops?

A B C
def oolala
  i = 1
  while i <= 100
    puts "oo-la-la"
    return
    i += 1
  end
  puts "oo-la-la"
end

oolala

How many times will "oo-la-la" be printed?

1 2 100 101 The return statement ends the method immediately
def oolala
  i = 1
  while i <= 100
    puts "oo-la-la"
    break
    i += 1
  end
  puts "oo-la-la"
end

oolala

How many times will "oo-la-la" be printed?

1 2 100 101 The break statement ends the loop immediately, but not the method

results matching ""

    No results matching ""