Jumpstart Live (JSL)
- Loop
- Repeating a sequence of statements; telling the program to do something a certain number of times, or until a certain condition is met
- Infinite loop
- A loop that runs until you kill the program
- Sentinel-controlled loop
- When the number of loops cannot be determined prior to loop execution (e.g, while, until)
- Counter-controlled loop
- When the number of loops can be determined prior to loop execution (e.g, times)
- Executes code over and over again, while a condition is true
# code syntax
while <boolean expression> do
code
end
# code example
rand_num = rand(5)
guess = gets.chomp.to_i
while rand_num != guess do
guess = gets.chomp.to_i
end
- Executes code over and over again, until the condition is true
# code syntax
until <boolean expression> do
code
end
# code example
rand_num = rand(5)
guess = gets.chomp.to_i
until rand_num == guess do
guess = gets.chomp.to_i
end
- when
times
is used without an iteration variable it is a loop, when it is used with an iteration variable it becomes an iterator times
must be associated with a block
# times syntax as a loop with no iteration variable
Fixnum.times
code
end
# code example
# prints out "hello" 5 times
5.times do
puts "hello"
end
Create a loop table for the code below, assuming the inputs noted below
puts "Hello! We are going to total some numbers!"
puts "Enter a negative number to quit."
total = 0
input = gets.chomp.to_i
while input > -1
total += input
input = gets.chomp.to_i
end
puts "Result: #{total}"
1. inputs; 0, -1, 2
2. inputs: 33, 6, 2, 9, 0, -1
3. inputs: 4.2, 1.1, 9.9, -1.0
- Ada Loops Video (9:11)