Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Conditional Statements

R Conditional Statements

A decision structure requires the programmer to specify one or more conditions to be evaluated or tested, along with a statement (required) to execute if the condition is true and an optional statement to execute if the condition is false.

The following is the general form of a typical decision structure in most programming languages:

R provides the following types of conditional statements:

if statement

An if statement consists of a Boolean expression followed by one or more statements.

The syntax format is as follows:

if(boolean_expression) {

    // statement to be executed if the boolean expression is true

}

If the Boolean expression boolean_expression is true, the code here is executed; if it is false, it is not executed.

Example

x

<-

50L

if

(

is.integer

(

x

)

)

{

   

print

(

"X is an integer"

)

}

The output of the code above is:

[1] "X 是一个整数"

if...else statement

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

The syntax format is as follows:



if(boolean_expression) {

    // statement to execute if the boolean expression is true

} else {

    // statement to execute if the boolean expression is false

}

If the boolean expression boolean_expression is true, the code inside the if block is executed. If the boolean expression is false, the code inside the else block is executed.

Example

x

<-

c

(

"google"

,

"runoob"

,

"taobao"

)

if

(

"runoob"

%

in

%

x

)

{

   

print

(

"contains runoob"

)

}

else

{

   

print

(

"does not contain runoob"

)

}

The output of the code above is:

[1] "包含 runoob"

If there are multiple conditions to check, you can use if...else if...else:

if(boolean_expression 1) {

    // statement to execute if boolean_expression 1 is true

} else if( boolean_expression 2) {

    // statement to execute if boolean_expression 2 is true

} else if( boolean_expression 3) {

    // statement to execute if boolean_expression 3 is true

} else {

    // Execute when all the above boolean expressions are false

}

Example

x

<-

c

(

"google"

,

"runoob"

,

"taobao"

)

if

(

"weibo"

%

in

%

x

)

{

   

print

(

"the first if contains weibo"

)

}

else

if

(

"runoob"

%

in

%

x

)

{

   

print

(

"the second if contains runoob"

)

}

else

{

   

print

(

"not found"

)

}

The output of the code above is:

[1] "第二个 if 包含 runoob"

switch statement

A switch statement allows testing a variable against multiple values. Each value is called a case.

The syntax format is as follows:

switch(expression, case1, case2, case3....)

switch statements must follow these rules:

The following example returns the third value:

Example

x

<-

switch

(

   

3

,

   

"google"

,

   

"runoob"

,

   

"taobao"

,

   

"weibo"

)

print

(

x

)

The output of the code above is:

[1] "taobao"

If it is a string, the value of the corresponding string variable is returned:

Example

you.

like

<-

"runoob"

switch

(

you.

like

, google

=

"www.google.com"

, runoob

=

"www.runoob.com"

, taobao

=

"www.taobao.com"

)

The output of the code above is:

[1] "www.runoob.com"

If the integer is out of range, NULL is returned

Example

x

<-

switch

(

4

,

"google"

,

"runoob"

,

"taobao"

)

x

NULL

x

<-

switch

(

4

,

"google"

,

"runoob"

,

"taobao"

)

x

NULL