Chapter 12 Control Statements

Learning Objectives

After completing this chapter, you will be able to:

  • Use if, else if, and else to execute code conditionally
  • Write for loops to iterate over sequences and vectors
  • Use while loops and repeat for condition-based iteration
  • Apply break and next to control loop execution

In the past few chapters, we have been relying on a few R packages including ggplot2 for visualization, dplyr for manipulation, readr for export and import data, tidyr for tidying data, and stringr for handling strings. While these packages are super powerful and make writing codes much easier, they still can’t do everything we want. In this chapter, we will introduce a set of control statements, which are essential in programming complex tasks.

At a glance – Chapter ROADMAP

Section 12.1. Conditional Execution: Use if, else if, and else to run code based on specific conditions.
Section 12.2. For Loops: Automate repetitive tasks by iterating over sequences and vectors.
Section 12.3. While & Repeat: Implement more complex loops that continue until a logical condition is met.


Buy Me A Coffee