BIOL2001 Chap.2 Conditional Statements and Loops in R
Conditional Statements and Loops in R
Making a program decide and repeat
Without flow control, R runs a script from its first line to its last.
This chapter adds the structures that change that order: the if statement, which runs one expression when a condition is TRUE and optionally another when it is FALSE; compound expressions in braces, whose value is the value of their last line; and the two loops, for, which runs once per element of a vector, and while, which runs as long as a condition holds.
Tracing instead of guessing
The chapter teaches a five-step tracing routine that predicts every value a loop produces, with a worked trace that sums only the larger spider webs, and a flowchart of a while loop that overshoots its limit.
It closes with break, next, repeat and switch, the common trap of a while condition that never changes, and the lecture's caution about nesting loops too deeply.
What this chapter covers
- 01
Programs as ordered instructions
- 02
Conditional if and else statements
- 03
Compound expressions in braces
- 04
For loops over a vector
- 05
While loops and their stopping condition
- 06
Tracing a loop pass by pass
- 07
Loop controls: break, next, repeat and switch
Worked example · free
Tracing a while loop that triples a number
- 1Start with n = 1; the test 1 < 20 is TRUE, so n becomes 3.
- 13 < 20 is TRUE, so n becomes 9; 9 < 20 is TRUE, so n becomes 27.
- 127 < 20 is FALSE, so the loop ends and print(n) shows 27.
Key terms
- Conditional Statement
- An if statement that runs an expression only when its logical condition is TRUE.
- Compound Expression
- Several expressions grouped in braces, returning the value of the last one.
- For Loop
- A loop that runs its body once for each element of a vector.
- While Loop
- A loop that repeats its body for as long as a condition remains TRUE.
- Loop Variable
- The variable that takes each element of the sequence in turn inside a for loop.
- Infinite Loop
- A loop whose stopping condition never becomes FALSE, so it never ends.
Conditional Statements and Loops in R FAQ
When should I use a for loop rather than a while loop?
Use a for loop when you know what you are iterating over, such as the elements of a vector or the quarters of a year. Use a while loop when you only know the condition for stopping, such as continuing until a value reaches a threshold.
What does a compound expression return in R?
A compound expression, written as several expressions inside braces, returns the value of its last expression. For example, a block that sets x to 2, sets y to twice x and then computes x to the power y returns 16.
What is the difference between break and next?
break leaves the loop immediately, so no further passes run. next skips the rest of the current pass and moves to the following one. In a loop over 1 to 3 that prints ten times i, next at i equal to 2 prints 10 and 30, while break prints only 10.
Assessment move
Trace at least three loops on paper each week, writing every variable after every pass, and only then run them in RStudio to check. Rewrite one for loop as a while loop and back again, so you see how the stopping rule differs. Keep loops short and commented, because nested loops are where hidden errors collect.
When a loop misbehaves, insert a print() of the loop variable on the first line of the body; watching the values scroll past is the fastest way to see whether the loop runs the number of times you intended.
Working through Conditional Statements and Loops in R in BIOL2001? Sia is AskSia’s AI Statistics tutor — ask any BIOL2001 Conditional Statements and Loops in R question and get a clear, step-by-step explanation grounded in how BIOL2001 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.
STAT1008 Quantitative Research Methods · BIOL1030 Diversity of Life · STAT7038 Regression Modelling