Skip to content
This repository was archived by the owner on May 23, 2020. It is now read-only.

Commit 57174c1

Browse files
loops and iterators added
1 parent ddcb792 commit 57174c1

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,47 @@ print 'Good work!!' unless jobNotDone
114114
Complex expressions can be created using these operators: E.g. *(x && (y || w)) && z*
115115
Expressions in parentheses are always evaluated before anything outside parentheses.
116116

117+
## Loops and Iterators
118+
Use of while:
119+
```ruby
120+
i=0
121+
while i<5
122+
puts i
123+
i=i+1
124+
end
125+
```
126+
Use of **until**: continues looping _until_ the condition given turns to be true. It is compliment to _while_.
127+
```ruby
128+
counter = 1
129+
until counter == 11
130+
puts counter
131+
counter+=1
132+
end
133+
```
134+
This prints from 1 to 10, each number in new line.
135+
**_Note_**: These operators are supported in Ruby: += -= *= /=
136+
137+
When you don't know how many times you have to loop in, a different syntax of _for_ can be used.
138+
```ruby
139+
for num in 1...10
140+
puts num
141+
end
142+
```
143+
This will print numbers from 1 to 9 each in different line.
144+
1...10 will print 1 to 9
145+
1..10 will print 1 to 10
146+
147+
Ruby supports iterators. One such simple method is **_loop_**.
148+
```ruby
149+
loop { print "Hello there!!" }
150+
```
151+
The curly braces {} are interchangeable with keywords _do_ and _end_.
152+
```ruby
153+
i = 0
154+
loop do
155+
i += 1
156+
print "#{i}"
157+
break if i > 5
158+
end
159+
```
160+
You have to give a condition inside the block which will be used to decide when to break the loop.

0 commit comments

Comments
 (0)