WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

R · Basic · question 7 of 100

What are R’s control structures, such as loops and conditional statements?

📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

In R, control structures are used to control the flow of execution in a program. Control structures allow you to execute specific blocks of code under certain conditions or to iterate over a sequence of values. Some of the main control structures in R are loops and conditional statements.

Conditional statements: Conditional statements are used to execute code based on certain conditions. In R, conditional statements are implemented using the if, else if, and else keywords. The basic syntax for an if statement is:

    if (condition) {
        # code to execute if the condition is TRUE
    } else {
        # code to execute if the condition is FALSE
    }

Here’s an example that uses an if statement to check if a number is even or odd:

    x <- 5
    
    if (x %% 2 == 0) {
        print("x is even")
    } else {
        print("x is odd")
    }

In this example, the % operator returns the remainder of x divided by 2. If the remainder is 0, then x is even, and the first code block is executed. Otherwise, x is odd, and the second code block is executed.

Loops: Loops are used to execute a block of code repeatedly, either a fixed number of times or until a certain condition is met. In R, there are several types of loops, including for loops, while loops, and repeat loops.

The basic syntax for a for loop is:

    for (variable in sequence) {
        # code to execute for each value of the variable
    }

Here’s an example that uses a for loop to print the numbers from 1 to 5:

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

The basic syntax for a while loop is:

    while (condition) {
        # code to execute while the condition is TRUE
    }

Here’s an example that uses a while loop to print the numbers from 1 to 5:

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

In this example, the loop continues to execute as long as i is less than or equal to 5. The i <- i + 1 statement increments i by 1 for each iteration of the loop.

The basic syntax for a repeat loop is:

    repeat {
        # code to execute repeatedly
        if (condition) {
            break
        }
    }

Here’s an example that uses a repeat loop to print the numbers from 1 to 5:

    i <- 1
    
    repeat {
        print(i)
        i <- i + 1
        if (i > 5) {
            break
        }
    }

In this example, the loop continues to execute indefinitely until the break statement is reached. The if (i > 5) condition checks if i has reached 5, and if so, the loop is terminated using break.

In summary, R has several control structures, including conditional statements and loops. Conditional statements allow you to execute code based on certain conditions, while loops allow you to execute a block of code repeatedly until a certain condition is met. Understanding and using these control structures effectively is essential for writing efficient and effective R programs.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic R interview — then scores it.
📞 Practice R — free 15 min
📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

All 100 R questions · All topics