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

R Statements_04

Uploaded by

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

R Statements_04

Uploaded by

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

ICS422 Applied Predictive Analytics [3- 0-0-3]

R Programming Statements

Class 07 Presented by
Dr. Selvi C
Assistant
Professor
IIIT Kottayam
R if Statement
• The if statement consists of the Boolean
expressions followed by one or more
statements. The if statement is the
simplest decision-making statement
which helps us to take a decision on the
basis of the condition.
• The if statement is a conditional
programming statement which
performs the function and displays the
information if it is proved true.
• The block of code inside the if
statement will be executed only when
the boolean expression evaluates to be
true. If the statement evaluates false,
then the code which is mentioned after
the condition will run.
• The syntax of if statement in R is as
follows:

2
Example

3
Example

4
If-else statement
• In the if statement, the inner code is
executed when the condition is true. The
code which is outside the if block will be
executed when the if condition is false.
• There is another type of decision-making
statement known as the if-else
statement. An if-else statement is the if
statement followed by an else statement.
An if-else statement, else statement will
be executed when the boolean
expression will false. In simple words, If
a Boolean expression will have true
value, then the if block gets executed
otherwise, the else block will get
executed.
• R programming treats any non-zero and
non-null values as true, and if the value
is either zero or null, then it treats them
as false.
• The basic syntax of If-else statement is
5
as follows:
Example

6
Example

7
R else if statement
• This statement is also known as nested
if-else statement. The if statement is
followed by an optional else if..... else
statement. This statement is used to test
various condition in a single if......else if
statement. There are some key points
which are necessary to keep in mind
when we are using the if.....else if.....else
statement. These points are as follows:
• if statement can have either zero or
one else statement and it must come
after any else if's statement.
• if statement can have many else
if's statement and they come before
the else statement.
• Once an else if statement succeeds,
none of the remaining else
if's or else's will be tested.
• The basic syntax of If-else statement is as
follows:

8
Example

9
Example

10
Nested If Statements
• You can also have if statements inside if statements,
this is called nested if statements.

11
R Switch Statement
• A switch statement is a selection control mechanism that allows the value of an
expression to change the control flow of program execution via map and search.
• The switch statement is used in place of long if statements which compare a variable
with several integral values. It is a multi-way branch statement which provides an easy
way to dispatch execution for different parts of code. This code is based on the value of
the expression.
• This statement allows a variable to be tested for equality against a list of values. A
switch statement is a little bit complicated. To understand it, we have some key points
which are as follows:
• If expression type is a character string, the string is matched to the listed cases.
• If there is more than one match, the first match element is used.
• No default case is available.
• If no case is matched, an unnamed case is used.

12
• There are basically two ways in which one of the cases is selected:
1) Based on Index
• If the cases are values like a character vector, and the expression is
evaluated to a number than the expression's result is used as an index to
select the case.
2) Based on Matching Value
• When the cases have both case value and output value like
["case_1"="value1"], then the expression value is matched against case
values. If there is a match with the case, the corresponding value is the
output.
• The basic syntax of If-else statement is as follows:
switch(expression, case1, case2, case3....)
13
Switch

14
Example

15
R next Statement
• The next statement is used to skip any
remaining statements in the loop and
continue executing. In simple words, a
next statement is a statement which
skips the current iteration of a loop
without terminating it. When the next
statement is encountered, the R parser
skips further evaluation and starts the
next iteration of the loop.
• This statement is mostly used with for
loop and while loop.
Syntax
• There is the following syntax for
creating the next statement in R
16
next
Example

17
R Break Statement
• In the R language, the break statement is used to break
the 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. We can use it to various loops like: for,
repeat, etc.
• There are basically two usages of break statement
which are as follows:
• When the break statement is inside the loop, the loop terminates
immediately and program control resumes on the next statement
after the loop.
• It is also used to terminate a case in the switch statement.

18
Example

19
Any
Queries?
Thank you

You might also like