Open In App

goto statement in R Programming

Goto statement in a general programming sense is a command that takes the code to the specified line or block of code provided to it. This is helpful when the need is to jump from one programming section to the other without the use of functions and without creating an abnormal shift.

Unfortunately, R doesn’t support goto but its algorithm can be easily converted to depict its application. By using following methods this can be carried out more smoothly:



Flowchart

  1. Goto encountered
  2. Jump to the specified line number/ name of the code block
  3. Execute code

Example 1: Program to check for even and odd numbers






a <- 4
if ((a %% 2) == 0)
    print("even"
else 
{
    print("odd")
}

Output:

[1] "even"

Explanation:

Example 2: Program to check for prime numbers




a <- 16
b <- a/2
flag <- 0
i <- 2
repeat
{
    if ((a %% i)== 0)
    {
        flag <- 1
        break 
    
}
  
if (flag == 1)
{
    print("composite")
}
else 
{
    print("prime")
}

Output:

[1] "composite"

Explanation:

Note: Since R doesn’t have the concept of the goto statement, the above examples were made using simple if-else and break statements.


Article Tags :