Last active
August 29, 2015 14:00
-
-
Save MilanGrubnic70/11091190 to your computer and use it in GitHub Desktop.
Loops: while, until, & for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The 'For' Loop: | |
# A while loop checks to see if a certain condition is true, and while it is, the loop keeps running. | |
# As soon as the condition stops being true, the loop stops! Beware infinite loops! | |
counter = 1 | |
while counter < 11 | |
puts counter | |
counter = counter + 1 | |
end | |
#=> 1 2 3..10 | |
# The "Until" Loop: | |
# The until loop is a complement to the while loop. It's sort of like a backwards while loop. | |
counter = 1 | |
until counter > 10 | |
puts counter | |
counter += 1 | |
end | |
#=> 1 2 3..10 | |
#The 'For' Loop | |
# Sometimes you do know how many times you'll be looping, however, and when that's the case, you'll want to use a for loop. | |
for variable in range #.. or ... | |
puts variable | |
end | |
for num in 1..10 | |
puts num | |
end | |
#=> 1 2 3..10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment