In this program, You will learn how to find the largest of three characters using nested if in R.
R Example: How to find the largest of three characters using nested if in R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
{ ch1 <- readline(prompt = "Enter first Character :") ch2 <- readline(prompt = "Enter second Character :") ch3 <- readline(prompt = "Enter third Character :") if (ch1 > ch2) { if (ch1 > ch3) print(paste("Greatest is :", ch1)) else print(paste("Greatest is :", ch3)) } else { if (ch2 > ch3) print(paste("Greatest is :", ch2)) else { print(paste("Greatest is :", ch3)) } } } |
Output:
1 2 3 4 5 6 |
Enter first Character :B Enter second Character :A Enter third Character :C [1] "Greatest is : C" |
The code first reads in values for ch1
, ch2
, and ch3
from the user and stores them as strings. It then enters a series of if
statements which compare the values of ch1
, ch2
, and ch3
and print the largest value to the console.
The first if
statement checks if ch1
is greater than ch2
. If it is, the code enters the second if
statement which checks if ch1
is also greater than ch3
. If it is, the code prints a message saying that ch1
is the largest. If ch1
is not the largest, the code enters the else
block and prints a message saying that ch3
is the largest. If the first if
statement is not true, the code enters the second if
statement which checks if ch2
is greater than ch3
. If it is, the code prints a message saying that ch2
is the largest. If neither of these conditions are true, the code enters the else
block and prints a message saying that ch3
is the largest.
It is important to note that this code compares the characters based on their ASCII values. ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents letters, numbers, and other symbols as integers. ASCII values are used to represent characters in computers and other devices, and they can be compared using the same operators that are used to compare numbers.