Australian National University · FACULTY OF STATISTICS

BIOL2001 Chap.2 Conditional Statements and Loops in R

- one subject, every graph, every model, every mark
7 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 2 of 15 · BIOL2001

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.

In this chapter

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

Q [3 marks]. Trace n <- 1; while (n < 20) n <- n * 3; print(n). List every value n takes and state what is printed. The practice weighting is assigned by AskSia and is not an official assessment.
A while loop drawn as a flowchart: R tests n < 20, runs the body while the test is TRUE, returns to the test after each pass, and leaves through the FALSE brancA while loop drawn as a flowchart: R tests n < 20, runs the body while the test is TRUE, returns to the test after each pass, and leaves through the FALSE branch, so n takes the values 1, 3, 9 and 27 before 27 is printed.n <- 1n < 20 ?n <- n * 3TRUE1, 3, 9, 27print(n): 27FALSE
A while loop drawn as a flowchart: R tests n < 20, runs the body while the test is TRUE, returns to the test after each pass, and leaves through the FALSE branch, so n takes the values 1, 3, 9 and 27 before 27 is printed.
  • 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.
n takes the values 1, 3, 9 and 27, and 27 is printed. A while loop always stops at the first value that breaks its condition, so the result overshoots the limit of 20.
Sia tip — Re-check the condition after every pass, not just at the start: the last pass runs on a value that still meets the test and produces one that does not.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 46 of your Australian National University subjects - and 1,000+ Bibles across every Australian university.
Sia - your BIOL2001 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works