Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views6 pages

Ex4 - DA Lab

This document outlines the aim to study branching and looping instructions in R and create user-defined functions. It explains various control statements such as break, next, if, for, while, and repeat loops, as well as user-defined functions and data handling in R. The procedure includes executing example codes in R Studio to observe and analyze the results of these instructions.

Uploaded by

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

Ex4 - DA Lab

This document outlines the aim to study branching and looping instructions in R and create user-defined functions. It explains various control statements such as break, next, if, for, while, and repeat loops, as well as user-defined functions and data handling in R. The procedure includes executing example codes in R Studio to observe and analyze the results of these instructions.

Uploaded by

abirami.nayagam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex.

No: 4
Date: 23.07.2025
BRANCHING AND LOOPING INSTRUCTIONS
AIM:
1. To study the branching and looping instructions in R
2. To create user defined functions in R

REQUIRED SOFTWARE:
Posit Cloud Platform
THEORY:
Branching and looping instructions help to control the flow of active statement within our applications.

1. Branching instructions: causes a computer to begin executing different instruction sequence and thus deviate from its
default behavior of executing instructions in order. Common branching statements include break, continue, return and
goto. These statements allow flow of execution to jump to different part of program.
a. Break command – used to break execution and for an immediate exit from the loop. In nested loops, break exits from
the innermost loop only and control transfer to the outer loop. It is useful to manage and control the program execution
flow.
b. Next command – it is a loop control statement. This command is used to skip the current iteration of a loop without
terminating it.
2. Looping instructions: they are used to repeat a specific block of code instructions. When a loop is created, R will
execute the instructions in the loop, until a specified condition is met.
In R programming, types of control statements are –
1) If condition – these are decision making statements which are used to decide whether a certain statement will be
executed or not.
2) If else – it execute statements if condition is true and not if it is false. While else
Statement is used to execute with if statement a block of code when condition is false.
3) For loop – used for iterating over a sequence.
4) Nested loops – used to manipulate or allow to loop through elements in multiple vectors and perform some operations.
5) While loop – it is an entry controlled loop which runs the given statement until condition is false.
6) Repeat loop – it is a simple loop that will run the same statement repeatedly until a stop
Condition is encountered.

Some other functions are prt console( ) function which writes a function to print value, math( ) function which provides
various mathematical functions to perform calculations, switch ( ) function which tests an expression against element of a
list – if value evaluated from the expression matches item from the list ,corresponding value is returned.
User defined functions are created using the function() directive and are stored as R objects. In particular, they are
R objects of class “function”. Functions can be passed as arguments to other functions . Functions can be nested, such that
you a function can be defined inside of another function. Functions can have named arguments.
Redirecting the output: The sink() function can be used to either print the data or export the data or the R output to text or
CSV file types.
Saving the data structures: Data can be written in R data format to preserve data structures, such as column data types
(numeric, character or factor).
PROCEDURE:
1. Open R studio and create a new script.
2. Run the example codes needed to be executed using branching and looping instructions.
3. Observe and interpret the results.
COMMANDS:
Task 1: Find the factorial of a given number

Task 2: Get a number from user and check if it is odd or even

Task 3: Get a number from user and check if it is prime


RESULT:
Thus the branching and looping instructions are executed with certain examples in R studio IDE to understand
their functioning. The outputs are obtained and analyzed. The branching and looping instructions aid in solving repetitive
and complex tasks.

You might also like