You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: intro/03_loops/README.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#Loops#
1
+
#Loops
2
2
3
3
If I asked you to write a program that prints ```Hi, mom!``` three times, how would you write it? Probably something like this:
4
4
@@ -10,7 +10,7 @@ puts "Hi, mom!"
10
10
11
11
That's manageable, isn't it? Okay, but what if I asked you to do the same thing except 1000 times? A million times? This is where **loops** come to the rescue.
12
12
13
-
##While##
13
+
##While
14
14
15
15
```ruby
16
16
count =0
@@ -31,7 +31,7 @@ while count < 1000 # Count will never equal 1000
31
31
end
32
32
```
33
33
34
-
##Until##
34
+
##Until
35
35
36
36
Similar to ```unless``` and ```if```, ```until``` mirrors the behavior of ```while``` except it tests that a certain condition is false rather than true.
37
37
@@ -46,7 +46,7 @@ end
46
46
puts"Blast off!"
47
47
```
48
48
49
-
##For##
49
+
##For
50
50
51
51
**For-loops** are a common and popular alternative to **while-loops** when you must repeat a sequence of code a certain number of times. We can rewrite the above examples in the following way:
52
52
@@ -80,7 +80,7 @@ The important thing to note about these for-loops is that they are storing the i
80
80
end
81
81
```
82
82
83
-
##Loop##
83
+
##Loop
84
84
85
85
The last kind I'd like to mention is invoked using the ```loop``` keyword. Go figure.
86
86
@@ -99,7 +99,7 @@ end
99
99
puts"Game Over"
100
100
```
101
101
102
-
##Break##
102
+
##Break
103
103
104
104
Anything that you put in a ```loop``` block will happen infinitely unless you use the keyword ```break```. Using ```break``` inside of a loop will cause the program to leave that block of code and return to the next highest scope. Let's get a bit more complicated and show what that means.
105
105
@@ -124,7 +124,7 @@ Outer block
124
124
125
125
From this, we can see that the break statement will only escape from whatever the immediate loop block of code is and return to the next highest scope.
126
126
127
-
##Next##
127
+
##Next
128
128
129
129
The next closest relative to ```break``` is the keyword **next**. Using ```next``` in a loop will immediately skip to the subsequent iteration of the loop, ignoring the rest of the statements in the block.
130
130
@@ -147,11 +147,11 @@ As you can see, while ```break``` leaves the entire enclosing loop immediately,
147
147
148
148
Finally, although Ruby also offers the ```redo``` and ```retry``` keywords when working with loops, you probably won't need to know them right now. If you're curious, please look them up and think of ways to use them.
149
149
150
-
#Knowledge Check#
150
+
#Knowledge Check
151
151
152
152
1. Are variables declared in a for-loop available after a loop has ended?
153
153
154
-
#Assignment#
154
+
#Assignment
155
155
fibonacci.rb
156
156
157
157
The Fibonacci sequence is a popular programming exercise for new programmers. If you're not familiar with it, the first ten Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, and 55. You should be able to see a pattern in this sequence. Using conditionals and looping techniques we've covered so far, write some code that will print each of first ten Fibonacci numbers on its own line.
0 commit comments