ICS422 Applied Predictive Analytics [3- 0-0-3]
R Programming Loops
Class 07
Presented by
Dr. Selvi C
Assistant
Professor
IIIT Kottayam
R For Loop
• A for loop is the most popular control flow statement. A for loop is
used to iterate a vector. It is similar to the while loop. There is only
one difference between for and while, i.e., in while loop, the condition
is checked before the execution of the body, but in for loop condition is
checked after the execution of the body.
• There is the following syntax of For loop in C/C++:
for (initialization_Statement; test_Expression; update_Statement)
{
// statements inside the body of the loop
}
2
How For loop works in C and C++ ?
• The for loop in C and C++ is executed in the following way:
• The initialization statement of for loop is executed only once.
• After the initialization process, the test expression is evaluated. The for loop
is terminated when the test expression is evaluated to false.
• The statements inside the body of for loop are executed, and expression is
updated if the test expression is evaluated to true.
• The test expression is again evaluated.
• The process continues until the test expression is false. The loop terminates
when the test expression is false.
3
For loop in R Programming
• In R, a for loop is a way to repeat a sequence of instructions
under certain conditions. It allows us to automate parts of our
code which need repetition. In simple words, a for loop is a
repetition control structure. It allows us to efficiently write the
loop that needs to execute a certain number of time.
In R, a for loop is defined as :
• It starts with the keyword for like C or C++.
• Instead of initializing and declaring a loop counter variable, we
declare a variable which is of the same type as the base type of
the vector, matrix, etc., followed by a colon, which is then
followed by the array or matrix name.
• In the loop body, use the loop variable rather than using the
indexed array element.
• There is a following syntax of for loop in R:
4
Example
5
Example
6
R repeat loop
• A repeat loop is used to iterate a block of code. It
is a special type of loop in which there is no
condition to exit from the loop. For exiting, we
include a break statement with a user-defined
condition. This property of the loop makes it
different from the other loops.
• A repeat loop constructs with the help of the
repeat keyword in R. It is very easy to construct
an infinite loop in R.
• The basic syntax of the repeat loop is as follows:
7
Steps
• First, we have to initialize our variables than it will enter into the Repeat
loop.
• This loop will execute the group of statements inside the loop.
• After that, we have to use any expression inside the loop to exit.
• It will check for the condition. It will execute a break statement to exit from
the loop
• If the condition is true.
• The statements inside the repeat loop will be executed again if the condition
is false.
8
Example
9
Example
10
R while loop
• A while loop is a type of control flow
statements which is used to iterate a
block of code several numbers of times.
The while loop terminates when the
value of the Boolean expression will be
false.
• In while loop, firstly the condition will
be checked and then after the body of
the statement will execute. In this
statement, the condition will be checked
n+1 time, rather than n times.
• The basic syntax of while loop is as
follows:
11
Example
12
Example
13
Example
14
Any
Queries?
Thank you