Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loops concept #278

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions concepts/loops/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"authors": ["colinleach"],
"contributors": [],
"blurb": "R provides the usual looping constructs, though they are needed less often than in most languages."
}
61 changes: 61 additions & 0 deletions concepts/loops/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# About

## Iterating over a vector

This is often unnecessary in R.
As discussed in other concepts, many functions will operate on entire vectors.

However, explicit loops are sometimes unavoidable.
This is especially true when the loop body has side effects such as printing or file I/O.

```R
> words <- c("This", "is", "a", "loop")
> for (w in words) { print(w) } # the braces are optional here
[1] "This"
[1] "is"
[1] "a"
[1] "loop"
```

If the numerical index is needed, use `seq_along()`.

```R
> v <- LETTERS[1:3]
> v
[1] "A" "B" "C"

> for (i in seq_along(v)) { print(sprintf("%s%i", v[i], i)) }
[1] "A1"
[1] "B2"
[1] "C3"
```

Using `i in 1:length(v)` is not recommended, as it will cause problems with length-zero vectors: the range `1:0` is equivalent to `c(1, 0)`, so the loop body will execute and probably fail.
`seq_along` is designed to handle this case correctly.


## `while` and `repeat`

These work much as you might guess, based on many C-family languages.
If necessary, use `break` to exit a loop completely and `continue` to exit the current iteration.

These three variants are equivalent and all end with `x == 0.4444...`

```R
x <- 12
while (x > 1) {
x <- x / 3
}

x <- 12
while (TRUE) {
x <- x/3
if (x <= 1) break
}

x <- 12
repeat { # no boolean clause
x <- x/3
if (x <= 1) break
}
```
60 changes: 60 additions & 0 deletions concepts/loops/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Introduction

## Iterating over a vector

This is often unnecessary in R.
As discussed in other concepts, many functions will operate on entire vectors.

However, explicit loops are sometimes unavoidable.
This is especially true when the loop body has side effects such as printing or file I/O.

```R
> words <- c("This", "is", "a", "loop")
> for (w in words) { print(w) } # the braces are optional here
[1] "This"
[1] "is"
[1] "a"
[1] "loop"
```

If the numerical index is needed, use `seq_along()`.

```R
> v <- LETTERS[1:3]
> v
[1] "A" "B" "C"

> for (i in seq_along(v)) {print(sprintf("%s%i", v[i], i))}
[1] "A1"
[1] "B2"
[1] "C3"
```

Using `i in 1:length(v)` is not recommended, as it will cause problems with length-zero vectors.
`seq_along` is designed to handle this case correctly.

## `while` and `repeat`

These work much as you might guess, based on many C-family languages.
If necessary, use `break` to exit a loop completely and `continue` to exit the current iteration.

These three variants are equivalent and all end with `x == 0.4444...`

```R
x <- 12
while (x > 1) {
x <- x / 3
}

x <- 12
while (TRUE) {
x <- x/3
if (x <= 1) break
}

x <- 12
repeat { # no boolean clause
x <- x/3
if (x <= 1) break
}
```
6 changes: 6 additions & 0 deletions concepts/loops/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"url": "https://intro2r.com/loops.html",
"description": "An Introduction to R: Loops"
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,11 @@
"uuid": "2751b6f2-7d71-4397-b063-9bf927a57756",
"slug": "booleans",
"name": "Booleans"
},
{
"uuid": "d48dec37-b0fd-4193-954a-f005c72f8eb6",
"slug": "loops",
"name": "Loops"
}
],
"key_features": [
Expand Down