Part B Half R M2
Part B Half R M2
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.")
}
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
Example: