0% found this document useful (0 votes)
2 views

Chapter_5_Control structures

The document provides an overview of control structures in R programming, including decision structures (if, if-else, multiple if, and switch statements) and looping structures (repeat, for, and while loops). It explains the syntax and provides examples for each type of control structure. The summary highlights that R allows conditional execution of statements and has three types of loops for iteration.

Uploaded by

hypergenome76
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter_5_Control structures

The document provides an overview of control structures in R programming, including decision structures (if, if-else, multiple if, and switch statements) and looping structures (repeat, for, and while loops). It explains the syntax and provides examples for each type of control structure. The summary highlights that R allows conditional execution of statements and has three types of loops for iteration.

Uploaded by

hypergenome76
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

R PROGRAMMING GUIDE

5. Control Structures
Control structures include two basic types in R like any other programming
language:
Decision structures
Loop structures

Decision making structures or conditional control structures require the


developer to specify one or more conditions in the form of statements to be
evaluated or tested by the program, along with a statement or statements
which is executed if the condition is determined to be true and other
statements to be executed if the condition turns out to be false. The flowchart
representation of typical decision making structure found in most of the
programming languages which is mentioned below:

Page 25 Eduonix Learning Solutions Pvt Ltd


R PROGRAMMING GUIDE

The various conditional control structures are mentioned below:


If statement
If else statement
Multiple if statement
Switch Statement

If Statement:
If statement includes an Boolean expression followed by one or more
statements. The basic syntax of if statement is mentioned below:
if (test_expression) {
statement
}

The basic demonstration of the same is mentioned below:


> x<-9
> if(x>0){
+ print("The number is a positive number")
+}
[1] "The number is a positive number"

If else statement:
If statement is usually followed with optional else statement, which executes
when the Boolean expression is false.
The complete structure (basic syntax) of if-else statement is mentioned below:
if (test_expression) {
statement1
} else {
statement2
}

Page 26 Eduonix Learning Solutions Pvt Ltd


R PROGRAMMING GUIDE

The basic demonstration of if-else statement with R programming language is


mentioned below:
> x <- -5
> if(x > 0){
+ print("It is a positive number")
+ } else {
+ print("It is a negative number")
+}
[1] "It is a negative number"

Multiple If statements (Nested if-else statement):


The if…else nested block (if…else…if) statement allows the developer to
execute a block of code among more than 2 alternatives and the complete
structure is mentioned below:
The complete syntax of if…else statement is:
if ( test_expression1) {
statement1
} else if ( test_expression2) {
statement2
} else if ( test_expression3) {
statement3
} else {
statement4
}
The demonstration of executing nested if statements in R is mentioned below:
> x <- 0
> if (x < 0) {
+ print("Negative number")
+ } else if (x > 0) {
+ print("Positive number")
+ } else
+ print("Zero")
[1] "Zero" #Output

Page 27 Eduonix Learning Solutions Pvt Ltd


R PROGRAMMING GUIDE

Switch Statement:
The switch function in R is usually designed to test an expression against
elements of a list. If the value which is used for evaluation from the expression
matches item from the list, the corresponding value is returned.
The complete syntax for creating a switch statement in R is –
switch(expression, case1, case2, case3....)
The demonstration of switch statement is demonstrated below:
> switch(2,"red","yellow","blue")
[1] "yellow"
> switch(1,"red","green","blue")
[1] "red

Note: If the value evaluated is a number, that item of the list is returned.
Looping Structures:
R supports various looping structures which is mentioned below. These
looping structures help us in delivering iterations in systematic manner.
The flowchart of looping structure is mentioned in following snapshot:

Page 28 Eduonix Learning Solutions Pvt Ltd


R PROGRAMMING GUIDE

The various looping structures are mentioned below:


Repeat Loop
For Loop
While Loop

Repeat Loop:
Repeat loop in R executes the same code again and again until the loop
condition required is met.
The standard syntax for creating repeat loop in R is mentioned below:
repeat {
commands
if(condition) {
break
}
}

The flowchart of working repeat loop is mentioned below:

Page 29 Eduonix Learning Solutions Pvt Ltd


R PROGRAMMING GUIDE

The illustration of working of repeat loop is mentioned below:


> v <- c("Repeat","loop")
> count <- 2
>
> repeat {
+ print(v)
+ count <- count+1
+
+ if(count > 5) {
+ break
+ }
+}
[1] "Repeat" "loop"
[1] "Repeat" "loop"
[1] "Repeat" "loop"
[1] "Repeat" "loop"

For Loop:
For loop works in the similar way like repeat loop with a sequence of
instructions under certain conditions. The sequence of instructions with
respect to iterations creating a for loop structure. The complete structure of
“for loop execution” is as follows:
for (value in vector) {
statements
}

Page 30 Eduonix Learning Solutions Pvt Ltd


R PROGRAMMING GUIDE

The execution of for loop with respect to flowchart is described below:

For loops in R programming language are considered to be completely flexible


in that specific order that they are not limited to integers, or even numbers in
the input. A user can pass character vectors, logical vectors, lists or
expressions.

The complete demonstration of for loop when executed in R programming


language is mentioned below:
> v <- LETTERS[1:26]
> for ( i in v) {
+ print(i)
+}
[1] "A"
[1] "B"
[1] "C"
[1] "D"

Page 31 Eduonix Learning Solutions Pvt Ltd


R PROGRAMMING GUIDE

[1] "E"
[1] "F"
[1] "G"
[1] "H"
[1] "I"
[1] "J"
[1] "K"
[1] "L"
[1] "M"
[1] "N"
[1] "O"
[1] "P"
[1] "Q"
[1] "R"
[1] "S"
[1] "T"
[1] "U"
[1] "V"
[1] "W"
[1] "X"
[1] "Y"
[1] "Z"
In the above example, the alphabets are listed just by specifying the respective
index number. All the uppercase characters are printed in systematic manner.

While loop:
The while loop in R executes the condition with iteration until stop condition is
met. The difference between while loop and for loop differs with respect to the
syntax. The syntax of while loop working is mentioned below:
while (test_expression) {
statement
}

Page 32 Eduonix Learning Solutions Pvt Ltd


R PROGRAMMING GUIDE

The basic demonstration of while loop working is mentioned below:


> v <- c("This is example of ","while loop")
> count <- 2
>
> while (count < 7) {
+ print(v)
+ count = count + 1
+}
[1] "This is example of " "while loop"
[1] "This is example of " "while loop"
[1] "This is example of " "while loop"
[1] "This is example of " "while loop"
[1] "This is example of " "while loop"

Summary
The user can conditionally execute statements using if and else.
The if-else function is a vector size equivalent of these.
R has three kinds of loops: repeat, while, and for.

Page 33 Eduonix Learning Solutions Pvt Ltd

You might also like