Skip to content

Commit

Permalink
Add clarification for for loops and not being able to modify the inde…
Browse files Browse the repository at this point in the history
…x variable
  • Loading branch information
Gautham Chandra committed Jan 20, 2020
1 parent 4ed4fc1 commit d3ce73c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions looping_and_control_statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ So **switch** keyword in JS translates to **case** in Ruby
unlike JS, Ruby has a different ```for``` syntax where it defines the range:

There are two forms for this:

* with two dots used in between the range (to be max bound inclusive)
* with three dots used in between the range (to be max bound exclusive)

Expand All @@ -107,6 +108,28 @@ for i in 0...10 # same as "for(var i = 0; i < 10; i++)" in JS. NOTE THE THREE DO
end
```

**CAVEAT**: You cannot increment the variable in the for loop delcaration manually.

In other words,

```javascript
// javascript
for (let i = 0; i < 100; i++) {
console.log(i)
i += 3
}
// => prints 0, 4, 8, 12...
```

```ruby
# ruby
for i in (0..100)
puts i
i += 3
end
# => prints 0, 1, 2, 3, ... to 100
```

### Using the ```loop``` keyword (i.e do..while statement in JS)

In Ruby, there is another way to loop through code called the ```loop``` statement. It takes in a code block and asks for a condition to exit out of the loop if the condition is met with the ```break``` keyword.
Expand Down

0 comments on commit d3ce73c

Please sign in to comment.