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

Part B Half R M2

The document explains how to write conditional statements in R programming using if, else if, and else constructs. It provides examples for checking conditions such as age for voting eligibility and determining if a number is positive or negative. Additionally, it discusses the switch statement for multiway branching and outlines its features and syntax.

Uploaded by

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

Part B Half R M2

The document explains how to write conditional statements in R programming using if, else if, and else constructs. It provides examples for checking conditions such as age for voting eligibility and determining if a number is positive or negative. Additionally, it discusses the switch statement for multiway branching and outlines its features and syntax.

Uploaded by

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

21 January 2024 21:11

Part B
Q.How do you write a conditional statements?
Ans. In R programming, you can use the if, else if, and else statements to create
conditional logic. The basic syntax for an if statement looks like this:
R
if (condition) {
# Code to be executed if the condition is TRUE

}
If you want to include an alternative block of code to be executed when the con
FALSE, you can use the else statement:
R
if (condition) {
# Code to be executed if the condition is TRUE
} else {
# Code to be executed if the condition is FALSE

}
If you have multiple conditions to check, you can use else if:
R
if (condition1) {
# Code to be executed if condition1 is TRUE
} else if (condition2) {
# Code to be executed if condition2 is TRUE
} else {
# Code to be executed if none of the conditions are TRUE
}
Here's a simple example to illustrate:
R
# Example x <- 10
if (x > 0) {
print("x is positive")
} else if (x < 0) { print("x is negative")
} else {
print("x is zero")

}
In this example, the program checks whether the variable x is positive, negative
and
prints the corresponding message.
You can also use logical operators (& for AND, | for OR) and parentheses to com
multiple conditions.
Q. Draw a flow chart for if conditonal statements

Ans,
Q.Draw a flow chart for if-else conditional statements.
Ans.
Q.Write R program if age greater than 18,allow vote otherwise donot allow.
Ans.
# Get the age as input
age <- as.numeric(readline(prompt = "Enter your age: "))
# Check if age is greater than 18 if (age > 18) {
print("You are eligible to vote.")
} else {
print("Sorry, you are not eligible to vote.")

}
Q.Write R program for checking whether a number is positive or negative.
Ans.
# Get the number as input
number <- as.numeric(readline(prompt = "Enter a number: "))
# Check if the number is positive, negative, or zero if (number > 0) {
print("The number is positive.")
} else if (number < 0) {
print("The number is negative.")
} else {
print("The number is zero.")

}
Q.Define syntax and flow chart for if..else if else statement. Ans.
if (condition1) {
# Code block executed if condition1 is TRUE
} else if (condition2) {
# Code block executed if condition2 is TRUE
} else {
# Code block executed if none of the conditions are TRUE

}
Q.Write R program for checking a number is positive. If positive,check whether i
even or not.
Ans.
# Get the number as input
number <- as.numeric(readline(prompt = "Enter a number: "))
# Check if the number is positive if (number > 0) {
print("The number is positive.")

# Check if the positive number is even


if (number %% 2 == 0) {
print("The positive number is even.")
} else {
print("The positive number is odd.")
}
} else {
print("The number is not positive.")

}
Q.Define syntax and flow chart for nested if else statement.
Ans.
if (condition1) {
# Code block executed if condition1 is TRUE

if (condition2) {
# Code block executed if both condition1 and condition2 are TRUE
} else {
# Code block executed if condition1 is TRUE and condition2 is FALSE

}
} else {
# Code block executed if condition1 is FALSE
}
Q.Switch case in R is a multiway branch statement.True or False?Why? Ans.
True. R does indeed have a switch statement, and it is a multiway branch state
switch statement is used for selecting one of several possible options based on
of an expression. It allows you to specify multiple cases without having to write
if-else if- else statements.
The basic syntax of the switch statement in R is as follows:

R
switch(EXPR, case1, case2, ..., default)
Here, EXPR is the expression whose value determines the case to be executed.
case2, etc., are the different cases to be evaluated based on the value of EXPR,
default is optional and will be executed if none of the cases match.
Q.Write some features of switch statement in r programming
Ans.
Multiway Branching:
The primary purpose of the switch statement is to perform multiway branching
the value of an expression.
Syntax:
The basic syntax of the switch statement is straightforward, allowing you to spe
multiple cases and a default case:
R

switch(EXPR, case1, case2, ..., default) Expression-Based Evaluation:


The value of the expression (EXPR) is used to determine which case should be e
Multiple Cases:
You can specify multiple cases within the switch statement without the need for
else statements.
Default Case:
You can include a default case to handle situations where none of the specified
match the value of the expression.
No Fall-Through:
Unlike some other programming languages, R's switch statement doesn't have f
through behavior. Once a matching case is found and executed, the switch state
exits.
Flexibility with Case Labels:
The case labels can be of various types, including numbers, characters, or string
providing flexibility in handling different types of expressions.
Expression Evaluation Only Once:
The expression (EXPR) is evaluated only once, which can be advantageous for
performance in certain situations
Q.Draw flow chart for switch statement with suitable example.
Ans.

Example:

You might also like