Skip to content

Latest commit

 

History

History
125 lines (91 loc) · 2.81 KB

loops.md

File metadata and controls

125 lines (91 loc) · 2.81 KB

JumpStart Live (JSL)

Day 3

Loops

Definitions

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)

Sentinel-controlled Loops

while loop

A while loop executes code over and over again, while a condition is true.

  • Executes code over and over again, while a condition is true

In the example below this loop will run while the number input with gets.chomp is not equal to `rand_num.

# code example
rand_num = rand(5)
guess = gets.chomp.to_i
while rand_num != guess do
  guess = gets.chomp.to_i
end

until loop

until loops are similar to while loops except that they execute code over and over again, until the condition is true, so as long as the condition is false.

# 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

It is common to use sentinel controlled loops for input validation. The loop below will run until the user enters a value in the correct range.

puts "Please enter a grade (0-100)."
grade = gets.chomp.to_i

until grade >= 0 && grade <= 100 do
	puts "That was an invalid grade.  Please enter a value 0-100."
	grade = gets.chomp.to_i
end

puts "Accepted thank you..."

Counter-controlled Loops

times

Times loops are always used for counter-based loops. The number of executions can be determined by a constant number 3.times do or using a varaible x.times do.

  • 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
Integer.times
   code
end
# code example
# prints out "hello" 5 times
5.times do
	puts "hello"
end

Loop Table

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

Resources