Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

control-flow-r

Control flow

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Control flow: the syntax, patterns, and safety habits you need before advancing in R.

Coroutines replace callback hell on Android and in Ktor—structured concurrency is interview-critical.

You will apply Control flow in contexts like: Research pipelines, Shiny dashboards, and statistical reporting.

Write R in the editor and click Run on server—the dev runner executes with Rscript; use print() or cat() and base R in playground snippets (tidyverse locally; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

R control flow uses if, else, for, while, and repeat—similar to Python but with parentheses around conditions and braces for blocks.

if / else

score <- 85
if (score >= 90) {
  grade <- "A"
} else if (score >= 80) {
  grade <- "B"
} else {
  grade <- "C"
}

for loops (prefer vectorization when possible)

total <- 0
for (x in c(1, 2, 3, 4)) {
  total <- total + x
}

Vectorized sum() is idiomatic—loops remain useful for side effects and complex logic.

Important interview questions and answers

  1. Q: Truthy values in R?
    A: Only TRUE/FALSE are logical; non-zero numbers and non-empty strings are not auto-boolean—use explicit comparisons.
  2. Q: Vectorized if?
    A: Use ifelse() for element-wise conditions on vectors.

Self-check

  1. How do you write else if in R?
  2. When prefer vectorization over for loops?

Tip: Prefer vectorized ops and ifelse() over loops for numeric transforms.

Interview prep

ifelse use case?

Vectorized conditional assignment across entire vectors.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • if missing else?
  • Vectorized if?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump