R Academy · Lesson

Control Flow Statements

Use if-else statements, for-loops, and while-loops to guide the execution of your code.

Lesson 2 of 310 steps

Control Flow Statements is a free R Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, and your progress syncs across the web and the CoddyKit app. The R Academy course includes 3 lessons in total.

Introduction to Control Flow in R

Control flow statements allow you to control the execution of your code using conditions and loops.

Control Flow Statements — illustration 1

Using if Statements

The if statement runs code only if a condition is true.

x <- 10
if (x > 5) {
  print('x is greater than 5')
}

Using if-else Statements

The if-else statement provides an alternative when the condition is false.

x <- 3
if (x > 5) {
  print('x is greater than 5')
} else {
  print('x is not greater than 5')
}

Using ifelse() Function

The ifelse() function is a vectorized version of if-else.

x <- c(2, 7, 5)
ifelse(x > 5, 'High', 'Low')

Using for Loops

A for loop repeats a block of code for a fixed number of times.

for (i in 1:5) {
  print(i)
}

Using while Loops

A while loop runs as long as the condition is true.

x <- 1
while (x <= 5) {
  print(x)
  x <- x + 1
}

Using break and next

The break statement exits a loop early, while next skips to the next iteration.

for (i in 1:5) {
  if (i == 3) {
    next # Skips 3
  }
  print(i)
}

Combining Multiple Conditions

You can combine multiple conditions using & (AND) and | (OR).

x <- 10
if (x > 5 & x < 15) {
  print('x is between 5 and 15')
}

Summary

In this lesson, you learned:

  • How to use if and if-else statements.
  • How to work with loops like for and while.
  • How to use break and next for flow control.
Control Flow Statements — illustration 10
Free to start

Learn R with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
43
Lessons
159

Frequently Asked Questions

Is the “Control Flow Statements” lesson free?

Yes — any 3 lessons of the R Academy learning path, including “Control Flow Statements”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The R Academy course includes 3 lessons in total.

What will I learn in “Control Flow Statements”?

Use if-else statements, for-loops, and while-loops to guide the execution of your code. You practise R Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start R Academy?

No prior experience is required. R Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Control Flow Statements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this R Academy lesson?

Yes. Every R Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Writing Functions in R
  2. Control Flow Statements
  3. Debugging Techniques
← Back to R Academy