0% found this document useful (0 votes)
2 views11 pages

Control Statements in R Programming

The document discusses control statements in R programming, outlining eight types including if conditions, loops, and return statements, with examples for each. It also explains unconditional statements, formal and actual parameters, and named arguments, emphasizing their importance for clarity and flexibility in coding. The document provides syntax and practical examples to illustrate the concepts effectively.

Uploaded by

uucms nep
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Control Statements in R Programming

The document discusses control statements in R programming, outlining eight types including if conditions, loops, and return statements, with examples for each. It also explains unconditional statements, formal and actual parameters, and named arguments, emphasizing their importance for clarity and flexibility in coding. The document provides syntax and practical examples to illustrate the concepts effectively.

Uploaded by

uucms nep
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Control Statements in R Programming

Control statements are expressions used to control the execution and flow of the program
based on the conditions provided in the statements. These structures are used to make a
decision after assessing the variable. In this article, we'll discuss all the control statements
with the examples.
In R programming, there are 8 types of control statements as follows:
 if condition
 if-else condition
 for loop
 nested loops
 while loop
 repeat and break statement
 return statement
 next statement
if condition
This control structure checks the expression provided in parenthesis is true or not. If true, the
execution of the statements in braces {} continues.
Syntax:
if(expression){
statements
....
....
}
Example:
x <- 100

if(x > 10){


print(paste(x, "is greater than 10"))
}
Output:
[1] "100 is greater than 10"
if-else condition
It is similar to if condition but when the test expression in if condition fails, then statements
in else condition are executed.
Syntax:
if(expression){
statements
....
....
}
else{
statements
....
....
}
Example:
x <- 5

# Check value is less than or greater than 10


if(x > 10){
print(paste(x, "is greater than 10"))
}else{
print(paste(x, "is less than 10"))
}
Output:
[1] "5 is less than 10"
for loop
It is a type of loop or sequence of statements executed repeatedly until exit condition is
reached.
Syntax:
for(value in vector){
statements
....
....
}
Example:
x <- letters[4:10]

for(i in x){
print(i)
}
Output:
[1] "d"
[1] "e"
[1] "f"
[1] "g"
[1] "h"
[1] "i"
[1] "j"
Nested loops
Nested loops are similar to simple loops. Nested means loops inside loop. Moreover, nested
loops are used to manipulate the matrix.
Example:
# Defining matrix
m <- matrix(2:15, 2)

for (r in seq(nrow(m))) {
for (c in seq(ncol(m))) {
print(m[r, c])
}
}
Output:
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
[1] 12
[1] 14
[1] 3
[1] 5
[1] 7
[1] 9
[1] 11
[1] 13
[1] 15
while loop
while loop is another kind of loop iterated until a condition is satisfied. The testing expression
is checked first before executing the body of loop.
Syntax:
while(expression){
statement
....
....
}
Example:
x=1

# Print 1 to 5
while(x <= 5){
print(x)
x=x+1
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
repeat loop and break statement
repeat is a loop which can be iterated many number of times but there is no exit condition to
come out from the loop. So, break statement is used to exit from the loop. break statement
can be used in any type of loop to exit from the loop.
Syntax:
repeat {
statements
....
....
if(expression) {
break
}
}
Example:
x=1

# Print 1 to 5
repeat{
print(x)
x=x+1
if(x > 5){
break
}
}

Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
return statement
return statement is used to return the result of an executed function and returns control to the
calling function.
Syntax:
return(expression)
Example:
# Checks value is either positive, negative or zero
func <- function(x){
if(x > 0){
return("Positive")
}else if(x < 0){
return("Negative")
}else{
return("Zero")
}
}

func(1)
func(0)
func(-1)
Output:
[1] "Positive"
[1] "Zero"
[1] "Negative"
next statement
next statement is used to skip the current iteration without executing the further statements
and continues the next iteration cycle without terminating the loop.
Example:
# Defining vector
x <- 1:10

# Print even numbers


for(i in x){
if(i%%2 != 0){
next #Jumps to next loop
}
print(i)
}
Output:
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10

Unconditional Statements
An unconditional statement in R programming language means a statement that executes
directly without checking any condition. There is no decision-making (no if, else, etc.)
involved.
🔹 Simple Meaning

It is a statement that runs every time the program reaches it.


🔹 Examples of Unconditional Statements in R

1. Assignment Statement
x <- 10
y <- 20
z <- x + y
print(z)
👉 These statements execute one by one without any condition.

2. Function Call
print("Hello World")
👉 This will always print the message when executed.

3. Sequential Execution
a <- 5
b <- 3
sum <- a + b
print(sum)
👉 All lines are executed in order (unconditional flow).

Formal and Actual Parameters in Programming


In programming, parameters are variables or values passed to a function or method during its
invocation. They enable the function to receive input data, perform operations, and optionally
return a result.
What are Formal Parameters?
Formal parameters, also known as formal arguments, are placeholders defined in the function
signature or declaration. They represent the data that the function expects to receive when
called. Formal parameters serve as variables within the function's scope and are used to
perform operations on the input data.
Syntax of Formal parameters:
Below is the syntax for Formal parameters:
// Here, 'name' is the formal parameter
function gfgFnc(name) {
// Function body
}
What are Actual Parameters?
Actual parameters, also called actual arguments or arguments, are the values or expressions
provided to a function or method when it is called. They correspond to the formal parameters
in the function's definition and supply the necessary input data for the function to execute.
Actual parameters can be constants, variables, expressions, or even function calls.
Syntax of Actual parameters:
Below is the syntax for Actual parameters:
function gfgFnc(name) {
// Function body
}
// Actual Parameter
gfgFnc("Geek");
Formal and Actual parameters in C:
Below is the implementation of Formal and Actual Parameters in C:
#include <stdio.h>
// a and b are formal parameters
int sum_numbers(int a, int b) { return a + b; }
int main()
{
// 3 and 5 are actual parameters
int result = sum_numbers(3, 5);
printf("Sum: %d\n", result);
return 0;
}
Formal and Actual Parameters in C++:
#include <iostream>
using namespace std;
// a and b are formal parameters
int sum_numbers(int a, int b) { return a + b; }
int main()
{
// 3 and 5 are actual parameters
int result = sum_numbers(3, 5);
cout << "Sum: " << result << endl;
return 0;
Named Arguments in R Programming
Meaning of Named Arguments
In R programming language, named arguments are arguments passed to a function by
explicitly specifying their parameter names.
Instead of relying on position, you assign values using:
argument_name = value
2. Why Use Named Arguments?
 Makes code more readable
 Avoids confusion about argument order
 Allows arguments to be passed in any order
 Useful when functions have many parameters
3. Basic Syntax
function_name(arg1 = value1, arg2 = value2)
4. Example
# Function
my_function <- function(a, b, c) {
return(a + b + c)
}

# Calling using named arguments


my_function(c = 3, a = 1, b = 2)
👉 Output: 6
✔ Order doesn’t matter because arguments are named.
5. Mixed Arguments (Named + Unnamed)
You can mix both, but:
 Unnamed arguments must come first
My_function(1, b = 2, c = 3)
✔ Valid

my_function(a = 1, 2, 3)
❌ Invalid

6. Default Arguments with Names


Functions often have default values:
power <- function(x, y = 2) {
return(x^y)
}

power(x = 5) # Uses default y = 2 → 25


power(x = 5, y = 3) # Overrides default → 125
7. Partial Matching of Names
R allows partial matching of argument names:
power(x = 5, y = 3)
power(x = 5, y = 3)
power(x = 5, y = 3)
Even:
power(x = 5, y = 3)
8. Advantages
 Improves clarity
 Reduces errors in large functions
 Helps in debugging
9. Disadvantages
 Slightly longer syntax
 Partial matching can lead to mistakes

You might also like