• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/12

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

12 Cards in this Set

  • Front
  • Back

The code below is an example of an infinite loop. The name describes exactly what it does: loop infinitely. This loop isn't useful in a real program, though. Modify the code so the loop stops after the first iteration.




loop do


puts 'Just keep printing...'


end

loop do


puts 'Just keep printing...'


break


end




Stopping a loop is as simple as adding the reserved word break inside the loop. break is nearly always necessary when using loop and doesn't require any conditions. When loop executes a break, it stops iterating immediately and exits the block.

The code below is an example of a nested loop. Both loops currently loop infinitely. 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.'




Looping within a loop is not uncommon. Therefore, it's important to understand how to avoid infinite loops and where to place break statements. When it comes to nested loops, it can be difficult to clearly understand what's going on. As you digest the code, focus on one loop at a time.We begin by modifying the innermost block. This loop can be stopped by placing break on the line following #puts. This forces the loop to iterate only once. After we've fixed the innermost loop, our attention is now focused on the parent loop. We modify this loop the same way we modified the child loop: by placing break on the line following the end of the innermost loop.




The code in this exercise is considered bad practice. It's used to illustrate how to break out of a nested loop, not to encourage the use of nested loops.

Modify the following loop so it iterates 5 times instead of just once.




iterations = 1




loop do


puts "Number of iterations = #{iterations}"


break


end

iterations = 1




loop do


puts "Number of iterations = #{iterations}"


iterations += 1


break if iterations > 5


end




It's important to understand the basics when it comes to loops in Ruby. loop demonstrates what the simplest loop can look like. It iterates over the given block and stops only when the reserved word break is executed. In the original code, break is written with no conditions. This means the loop will stop after the first iteration.To modify the loop so it stops only once it's iterated 5 times, we append an if statement to break. How do we know when the loop has iterated 5 times? We need to increment iterations by 1 each time the loop iterates over the block. We can then add the condition iterations > 5 to our if statement.




Further Exploration


If the break statement is moved up one line so it runs before iterations is incremented, what would need to be changed?

Modify the code below so the loop stops iterating when the user inputs 'yes'.




loop do


puts 'Should I stop looping?'


answer = gets.chomp


end

loop do


puts 'Should I stop looping?'


answer = gets.chomp


break if answer == 'yes'


puts 'Incorrect answer. Please answer "yes".'


end




This is a practical example of how a loop can be used to retrieve and handle user input. In the initial code, the loop continued iterating regardless of the input's value. To change that, we add break with an if condition. The condition, in this case, needs to be answer == 'yes'. This tells break to only execute if the input value is 'yes'.




We added a simple error message after break to demonstrate that break doesn't have to be at the end of the loop to do its job. Also, providing an error message can be useful in providing a better user experience when dealing with inputs.

Modify the code below so "Hello!" is printed 5 times.




say_hello = true




while say_hello


puts 'Hello!'


say_hello = false


end

say_hello = true


count = 0



while say_hello


puts 'Hello!'


count += 1


say_hello = false if count == 5


end




Loops are used regularly in Ruby, therefore, it's important to understand how to control them based on the program's conditions. In this exercise, the while loop is set to run based on the condition of say_hello. Since say_hello was initialized as true, the loop will execute at least once. Upon the first iteration, however, say_hello is set to false. Therefore, when while evaluates the condition of say_hello on the second iteration, it won't execute the block because say_hello equals false.




Now that we understand how this while loop works, we need to modify it to fit the requirements. In this case, we need to only change say_hello to false if we've said "Hello!" 5 times. We can accomplish this by adding a counter variable and ifcondition. We need count to track the number of times the loop has executed. Once count reaches 5, our if condition will evaluate to true.




We use Ruby's shorthand way of writing the if condition but it could also be written like this:




if count == 5


say_hello = false


end




Our solution works well, but there are simpler ways of accomplishing the same thing.




5.times do


puts 'Hello!'


end




Although using #times may be simpler and lets you write less code, it's still important to understand the fundamentals of how a method like #times actually works. Just keep in mind that there's usually more than one way to accomplish a given task. Watch You marked this exercise as completed Go to the next exercise

Using a while loop, print 5 random numbers between 0 and 99. The code below shows an example of how to begin solving this exercise.


numbers = []

while

# ...

end


Example output (your numbers will most likely be different):


6296311636

numbers = []



while numbers.size < 5


numbers << rand(100)


end



puts numbers




Implementing a while loop that iterates under set conditions should be fairly trivial. The more difficult part of this exercise is, perhaps, getting random numbers and keeping track of them. To accomplish this, we use #rand. This method works well because it returns a random number between 0 and one less than the number provided. In this case, that number is 100.




Once the random number is returned, we want to avoid printing it immediately. If we simply printed the returned number, while would iterate infinitely. Instead, we want to add the returned number to an array. This way, we can tell while to stop iterating after 5 numbers have been added to the array.



documentation

rand(max) → number

size()

Note! #size is the array version of #length


The following code outputs a countdown from 10 to 1. Modify the code so that it counts from 1 to 10 instead.


count = 10


until count == 0


puts count


count -= 1


end

count = 1


until count > 10


puts count


count += 1


end




The until loop is the opposite of the while loop. while iterates until the condition evaluates to false. until iterates until the condition evaluates to true. In this case, the condition evaluates to true when count equals 0. We want it to be the opposite though. We would like until to iterate until count is greater than 10. We use greater than here so 10 is included in the output. If we used ==, then 9 would be the last number printed.




There are two more things that need to be changed to fill the requirements. count should be initialized as 1 instead of 10 and, within the loop, count should add 1 instead of subtract 1.

While loop iterates until the condition is ____

False

Until loop iterates until the condition is ____

True

Given the array of several numbers below, use an until loop to print each number.


numbers = [7, 9, 13, 25, 18]

numbers = [7, 9, 13, 25, 18]


count = 0


until count == numbers.size


puts numbers[count]


count += 1


end



There are multiple ways we could have used until to accomplish this task. Our solution uses a counter variable to track the current iteration number. This number gets incremented upon each iteration.Using count helps us in two ways. First, it lets us control the number of iterations. In this case, we want to stop iterating when counts equals the length of numbers. Second, we use it to select the next value in the array. This works because countmatches the index of each number we want to print.

The code below shows an example of a for loop. Modify the code so that it only outputs i if i is an odd number.


for i in 1..100

puts i

end

for i in 1..100


puts i if i.odd?


end




for loops aren't used very often in Ruby, but it's still important to know how they work. It's typically used to iterate over a collection. In this case, we use it to iterate over the range 1..100, therefore, the i variable represents the current iteration number. This makes it easy to count from 1 to 100 by outputting the value of i.




For this exercise, we'd like to only output the odd numbers. We can do this by adding an if statement to #puts. We can tell #puts to only output i if i is an odd number by using the #odd? method on i.

Your friends just showed up! Given the following array of names, use a for loop to greet each friend individually.




friends = ['Sarah', 'John', 'Hannah', 'Dave']

friends = ['Sarah', 'John', 'Hannah', 'Dave']




for friend in friends


puts "Hello, #{friend}!"


end




The for loop is useful for looping over a set number of elements. Using for allows us to easily do something with each element in the given array. In this case, we want to output each name along with a greeting. We can do this by using the friend variable which represents the current element. We simply need to output our greeting and friend.When naming variables in a for loop it's typical to use the standard format: for friend in friends, for cat in cats, etc. This makes it clear that we're iterating over friends and doing something with each friend.