In this tutorial, we going to look at control structures so conditional statements, loop statements, and break, next, repeat statements. Before beginning, you can read Data Types In R and Subsetting In R to prepare.
Condition Statements (If – Else If – Else)
With conditional structures, we can make a program produce different results. For example, if-else can be used for an operation with 2 different outcomes. In R, 2 different conditional structures can be used, the first method is the classical condition structure, the second way is to use it by assigning it to the variable.
1 – Classic Condition Syntax
2 – Condition In Variable Syntax
First “if statement” is executed, if the condition is met, the output is written to the screen, if not, “if-else” runs. If this is not provided, the else output runs. In other words, the sequence is as follows. The first “if statement” runs, then the “if-else” structure works, then the program ends with “else statement”. If assigned to a variable, all these conditional statements will be stored in a variable and then can call with the same variable.
Loop Statements (While – For)
loops are used to perform an operation a certain number of times. For example, instead of typing 3 times to request a variable from a user, you can loop this process, so loops save you from rewriting a transaction.
We divide the loops into 2 as “For” and “While” loops. “While” loops can be used to make endless loops or work with variables, “For” loops can be used to perform a repetitive action or for index scanning.
While Loop Statement
If we want to add a condition statement we can add a condition statement in the loop. For example, we want to write “this is a 5” when the key equals 5, for this, we can use the if statement.
For Loop Statement
x <- c (1, 2, 3, 4) print (x ) the above code actually automates this process. Every time the cycle starts, another index is switched and the data is checked. Now, we going to look at “Nested For Loop”.
Nested loops can be a bit confusing, but all you need to know is that every time the first loop starts 1 time, it won’t start again until the under loop is finished. Same like while loop you can use condition in for loop.
Control Structures – Next, Break, Repeat
We can stop the loop with a break statement if you want to exit the loop you can use a break statement. Below, I gave break syntax.
Same like “While Loop” you can use a break statement in “For Loop”, we use the break statement mostly in condition structures.
Repeat is a simple infinity loop so every time work and never stop (you can exit this loop with Break) I recommend using it only for repetitive operations (I don’t even recommend it much either, you can use While instead)
We can use exit this loop with a break, this syntax is so similar to while loop. Below, I gave this syntax.
It is used to keep an event going, for example, we have a variable a and when this variable is 10, we want not to print any results, but after 11 it will start to be written again. Used to briefly pass a section.
CONGRATULATIONS, YOU PASSED CONTROL STRUCTURES IN R PROGRAMMING!