English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The conditional structure requires the programmer to specify one or more conditions to be evaluated or tested, as well as the statements to be executed when the condition is true (mandatory) and the statements to be executed when the condition is false (optional).
The general form of the typical conditional structure in most programming languages is as follows:
R language provides the following types of conditional statements:
if statement
if...else statement
switch statement
An if statement consists of a boolean expression followed by one or more statements.
The syntax format is as follows:
if(boolean_expression) { // statements to be executed if the boolean expression is true }
If the boolean expression boolean_expression is true, execute the code inside here, if it is false, do not execute.
x <- 50L if(is.integer(x)) { print("X is an integer" }
Execute the above code, and the output result is:
[1] "X is an integer"
An if statement can be followed by an optional else statement, which is executed when the boolean expression is false.
The syntax format is as follows:
if(boolean_expression) { // statements to be executed if the boolean expression is true } else { // statements to be executed if the boolean expression is false }
If the boolean expression boolean_expression is true, execute the code within the if block. If the boolean expression is false, execute the code within the else block.
x <- if("weibo" %in% x) {3codebox,"taobao") if("w"3codebox " %in% x) { print("Contains w"3codebox) } else { print("Does not contain w"3codebox) }
Execute the above code, and the output result is:
[1] "contains w"3codebox"
if there are multiple condition checks, you can use if...else if...else:
else if( boolean_expression 1) { // if the boolean expression boolean_expression 1 statements to be executed if true } 2) { // if the boolean expression boolean_expression 2 statements to be executed if true } 3) { // if the boolean expression boolean_expression 3 statements to be executed if true } else { // execute the statements when all boolean expressions are false }
x <- if("weibo" %in% x) {3codebox,"taobao") print("The first if contains weibo" else if ("w" }3codebox " %in% x) { print("The second if contains w"3codebox) } else { print("Not found") }
Execute the above code, and the output result is:
[1] "The second if contains w3codebox"
A switch statement allows testing the situation when a variable equals multiple values. Each value is called a case.
The syntax format is as follows:
switch(expression, case1, case2, case3....)
switch The statement must follow the following rules:
switch In the statement expression It is a constant expression, which can be an integer or a string. If it is an integer, it returns the corresponding case position value. If the integer is not within the range of positions, it returns NULL.
If multiple values match, the first one is returned.
expressionIf it is a string, it corresponds to the value of the variable name in the case, and if there is no match, there is no return value.
switch does not have a default parameter available.
The following example returns the third value:
x <- switch( 3, "google", "w3codebox", "taobao", "weibo" ) print(x)
Execute the above code, and the output result is:
[1] "taobao"
If it is a string, it returns the value corresponding to the string variable:
you.like<-"w3codebox" switch(you.like, google="www.google.com", w3codebox = "www.oldtoolbag.com", taobao = "www.taobao.com")
Execute the above code, and the output result is:
[1] "www.oldtoolbag.com"
If the integer is not within the range, it returns NULL
> x <- switch(4,"google","w3codebox,"taobao") > x NULL > x <- switch(4,"google","w3codebox,"taobao") > x NULL