Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Worksheet 1.

Student Name: Paramita Halder UID: 22MSM40017


Branch: M.Sc Data Science Section/Group: 212MSD-1/A
Semester: Ist Date of Performance:- 17-09-2022
Subject Name: Fundamentals of programming Subject Code: 22SDP-607

Overview of the practical:


In this experiment, we will learn about the conditional statements in R
programming.
Tasks to be performed:
1. Write a program to check whether the day entered by the user is
Sunday.
2. Write a program to check whether the student is Pass or fail.

Theory 1.
If statement: If statement is used to decide whether a certain
statement or block of statements will be executed or not i.e if a
certain condition is true then a block of statement is executed
otherwise not.
Syntax: if (expression) {
#statement to execute if condition is true

}
Three ways to write the above statement: -

Version 1: In separate lines


if (x < 10) { cat("x is smaller than 10\n") }
Version 2: In a single line
if (x < 10) { cat("x is smaller than 10\n") }
Version 3: In a single line without brackets
if (x < 10) cat("x is smaller than 10\n")

If the expression is true, the statement gets executed. But if the expression is
FALSE, nothing happens. The expression can be a logical/numerical vector, but
only the first element is taken into consideration. In the case of a numeric vector,
zero is taken as FALSE, rest as TRUE.

3. If-Else Statement

1. The if-statement in Programming Language alone tells us that if a condition is


true it will execute a block of statements and if the condition is false it won’t.
But what if we want to do something else if the condition is false. Here comes
the R else statement. We can use the else statement with the if statement to
execute a block of code when the condition is false.
Syntax:
if (condition)
{
// Executes this block if // condition is true
}
Else
{
// Executes this block if // condition is fail.
}

stop
2. Multiple Else-If

Statements Here we can check as many conditions as you like. But beware that the
conditions are tested from top to bottom. What this means is that first condition 1
is checked then 2, then 3 and so on. Only, if none of the requirements is met, else
gets executed. It may lead to slow processing of the code.

Syntax: - if (condition)

Block to be executed if this condition is TRUE

Else if

Block to be executed if this condition is TRUE

elseif

Block to be executed if this condition is TRUE

Else

Block to be executed if none of the statements are TRUE


}

3. Nested If-Else Statements

If-else statements can also be nested. Nested means that one of the actions
itself contains another if-else statement. Important: the two if-else
statements are independent.

Syntax: -

if (condition){

Block to be executed if this condition is TRUE

} else {if (condition){

Block to be executed if this condition is TRUE

} else {

Block to be executed if this condition is TRUE

}
4. Switch Statements In R,
the switch is also sometimes called the switch() function. A switch
statement checks a variable for equality against a predefined list of values.
The individual values in a list are also called as a case. A switch statement
can prove to be faster than if-else because the compiler knows that the case
constants carry the same type and it thus only tests for equality. Whereas in
case if-else, the compiler has no such knowledge.

Syntax: -
switch(expression,case1,case2,case3,…)
Practical implementation of all the Statements:
If statement:

2. If-Else Statements :

3. Multiple Else-if Statements:


4. Nested If-Else Statement :

5 . Swift Function:

Practice Questions:
1. Write a program to check whether the day entered is Sunday.
2. Write a program to check if the student is pass or fail.

Learning outcomes (What I have learnt):


1. I learned about conditional execution in R programming.
2. 2. I learned how to implement conditional execution in R programming using
various examples and practice questions.

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the
faculty :
SI. NO. Parameters Marks obtained Maximum Marks
1.
2.
3.

You might also like