R for loop

For loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly. The for loop will run the code in that appears between the braces once for each member of that. Simple meaning of for loop is “iterates over an object”.

Syntax:

for(var in seq) { expr }

Where var is the for loop variable
seq is the vector expression
expr is grouped expressions with sub expressions.


Examples:

techs<-c("R","Python","SAS","MySQL")

Print each element of techs

for(i in techs) { print(i) }

Output:

[1] "R" [1] "Python" [1] "SAS" [1] "MySQL

Print length of each element in techs

for(i in techs) { print(nchar(i)) }

Output:

[1] 1 [1] 6 [1] 3 [1] 5

Powered by WordPress