How to Repeat My Function Again Until if Statement Is Not Satisfied

In R programming, we require a control structure to run a cake of lawmaking multiple times. Loops come in the grade of the most central and strong programming concepts. A loop is a control statement that allows multiple executions of a statement or a set up of statements. The word 'looping' ways cycling or iterating.

A loop asks a query, in the loop structure. If the respond to that query requires an action, it volition exist executed. The same query is asked over again and once more until further action is taken. Whatever time the query is asked in the loop, it is known equally an iteration of the loop. There are ii components of a loop, the control statement, and the loop body.  The control statement controls the execution of statements depending on the condition and the loop trunk consists of the set of statements to exist executed.

In order to execute the identical lines of code numerous times in a program, a developer can simply use a loop.

There are three types of loop in R programming:

  • For Loop
  • While Loop
  • Repeat Loop

For Loop in R

It is a blazon of control statement that enables i to hands construct a loop that has to run statements or a set of statements multiple times. For loop is normally used to iterate over items of a sequence. Information technology is an entry controlled loop, in this loop the exam condition is tested commencement, so the body of the loop is executed, the loop body would not be executed if the examination status is false.

R – For loop Syntax:

for (value in sequence) {   statement }

For Loop Flow Diagram:

For Loop Flow Diagram

Below are some programs to illustrate the use of for loop in R programming.

Example 1: Program to display numbers from one to five using for loop in R.

R

for (val in 1: 5)

{

print (val)

}

Output:

[1] 1 [ane] 2 [1] three [1] 4 [ane] 5

Here, for loop is iterated over a sequence having numbers from i to 5. In each iteration, each item of the sequence is displayed.

Instance 2: Plan to brandish days of a calendar week.

R

calendar week < - c ( 'Sunday' ,

'Monday' ,

'Tuesday' ,

'Wed' ,

'Th' ,

'Friday' ,

'Saturday' )

for (day in week)

{

print (day)

}

Output:

[1] "Sunday" [1] "Monday" [1] "Tuesday" [1] "Wednesday" [ane] "Thusrday" [1] "Friday" [1] "Sat"

In the above plan, initially, all the days(strings) of the calendar week are assigned to the vector week. And so for loop is used to iterate over each string in a week. In each iteration, each twenty-four hour period of the week is displayed.

While Loop in R

It is a type of control statement which will run a argument or a set of statements repeatedly unless the given status becomes imitation. It is likewise an entry controlled loop, in this loop the test condition is tested first, then the body of the loop is executed, the loop torso would not be executed if the test condition is false.

R – While loop Syntax:

while ( status )  {   statement }

While loop Flow Diagram:

While loop Flow Diagram

Below are some programs to illustrate the use of the while loop in R programming.

Instance 1: Programme to display numbers from i to 5 using while loop in R.

R

val = 1

while (val <= v)

{

print (val)

val = val + 1

}

Output:

[i] 1 [i] 2 [1] 3 [1] 4 [1] 5

Initially, the variable value is initialized to one. In each iteration of the while loop the condition is checked and the value of val is displayed and and so information technology is incremented until it becomes 5 and the condition becomes false, the loop is terminated.

Instance 2: Program to calculate factorial of a number.

R

n < - 5

factorial < - one

i < - ane

while (i <= n)

{

factorial = factorial * i

i = i + 1

}

print (factorial)

Output:

[one] 120

Here, at offset, the variable n is assigned to 5 whose factorial is going to be calculated, then variable i and factorial are assigned to ane. i will be used for iterating over the loop, and factorial will be used for calculating the factorial. In each iteration of the loop, the condition is checked i.e. i should be less than or equal to 5, and after that factorial is multiplied with the value of i, then i is incremented. When i becomes 5, the loop is terminated and the factorial of 5 i.e. 120 is displayed beyond the scope of the loop.

Repeat Loop in R

It is a simple loop that will run the same statement or a group of statements repeatedly until the stop condition has been encountered. Repeat loop does not take any condition to cease the loop, a programmer must specifically place a condition within the loop's body and utilise the declaration of a interruption statement to terminate this loop. If no condition is nowadays in the body of the repeat loop then information technology will iterate infinitely.

R – Repeat loop Syntax:

repeat  {     argument      if( condition )     {       break    } }

Repeat loop Flow Diagram:

Repeat loop Flow Diagram

To terminate the repeat loop, we use a jump statement that is the break keyword. Below are some programs to illustrate the use of repeat loops in R programming.

Example 1: Program to display numbers from 1 to 5 using repeat loop in R.

R

val = ane

echo

{

print (val)

val = val + 1

if (val > v)

{

suspension

}

}

Output:

[1] i [1] 2 [1] three [1] four [i] 5

In the in a higher place program, the variable val is initialized to 1, then in each iteration of the repeat loop the value of val is displayed and then it is incremented until it becomes greater than five. If the value of val becomes greater than five then break statement is used to cease the loop.

Example 2: Programme to display a statement five times.

R

i < - 0

repeat

{

print ( "Geeks four geeks!" )

i = i + 1

if (i == five)

{

pause

}

}

Output:

[i] "Geeks 4 geeks!" [1] "Geeks 4 geeks!" [ane] "Geeks 4 geeks!" [1] "Geeks four geeks!" [i] "Geeks 4 geeks!"

Here, initially the variable i is initialized with 0 then in each iteration of the repeat loop after printing Geeks 4 geeks! the value of i is incremented till it becomes 5 and the condition in the if statement becomes true so, the break statement is executed to terminate the echo loop.

Jump Statements in Loop

We utilise a leap statement in loops to terminate the loop at a particular iteration or to skip a particular iteration in the loop. The two about commonly used jump statements in loops are:

  • Break Argument: The break keyword is a jump statement that is used to stop the loop at a particular iteration.

Example:

R

for (val in 1: v)

{

if (val == 3)

{

break

}

print (val)

}

Output:

[1] 1 [one] 2

In the above program, if the value of val becomes 3 then the break argument will be executed and the loop will terminate.

  • Next Statement: The adjacent keyword is a spring statement which is used to skip a detail iteration in the loop.

Example:

R

for (val in 1: 5)

{

if (val == three)

{

next

}

print (val)

}

Output:

[1] i [1] two [1] iv [1] v

In the above program, if the value of Val becomes iii then the next statement will be executed hence the current iteration of the loop volition be skipped. So 3 is not displayed in the output.

As we can conclude from the higher up 2 programs the basic divergence between the two jump statements is that the suspension argument terminates the loop and the adjacent argument skips a particular iteration of the loop.


stockinggivename.blogspot.com

Source: https://www.geeksforgeeks.org/loops-in-r-for-while-repeat/

0 Response to "How to Repeat My Function Again Until if Statement Is Not Satisfied"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel