YouTip LogoYouTip

R Decision Making

Decision making structures require the programmer to specify one or more conditions to be evaluated or tested, along with the statements to be executed if the condition is true (required) and the statements to be executed if the condition is false (optional). The following is the general form of a typical decision making structure in most programming languages: !(#) R provides the following types of decision making statements: * if statement * if...else statement * switch statement ### if Statement An if statement consists of a Boolean expression followed by one or more statements. Syntax: if(boolean_expression) { // statement(s) will be executed if the Boolean expression is true} If the Boolean expression boolean_expression evaluates to true, the code inside is executed. If it evaluates to false, the code is not executed. ## Example ```r x <- 50L if(is.integer(x)){ print("X is an integer") } Executing the above code produces the following result: "X is an integer" ### if...else Statement An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Syntax: ```r if(boolean_expression) { // statement(s) will be executed if the Boolean expression is true} else { // statement(s) will be executed if the Boolean expression is false} If the Boolean expression boolean_expression evaluates to true, the code inside the if block is executed. If the Boolean expression evaluates to false, the code inside the else block is executed. ## Example ```r x <-c("google","tutorial","taobao") if("tutorial"%in% x){ print("contains tutorial") }else{ print("does not contain tutorial") } Executing the above code produces the following result: "contains tutorial" For multiple condition checks, you can use if...else if...else: ```r if(boolean_expression 1) { // statement(s) will be executed if the Boolean expression 1 is true} else if( boolean_expression 2) { // statement(s) will be executed if the Boolean expression 2 is true} else if( boolean_expression 3) { // statement(s) will be executed if the Boolean expression 3 is true} else { // statement(s) will be executed if all the above Boolean expressions are false} ## Example ```r x <-c("google","tutorial","taobao") if("weibo"%in% x){ print("first if contains weibo") }else if("tutorial"%in% x){ print("second if contains tutorial") }else{ print("not found") } Executing the above code produces the following result: "second if contains tutorial" ### switch Statement A switch statement allows testing a variable for equality against a list of values. Each value is called a case. Syntax: ```r switch(expression, case1, case2, case3....) **switch** statement must follow these rules: * The **expression** in the **switch** statement is a constant expression, which can be an integer or a string. If it is an integer, it returns the value at the corresponding case position. If the integer is out of range, it returns NULL. * If multiple values match, the first one is returned. * If **expression** is a string, it corresponds to the variable name in the case, and returns its value. If no match is found, there is no return value. * switch has no default parameter available. The following example returns the third value: ## Example ```r x <-switch( 3, "google", "tutorial", "taobao", "weibo" ) print(x) Executing the above code produces the following result: "taobao" If it is a string, it returns the value corresponding to the string variable: ## Example ```r you.like x x NULL > x x NULL
← R FunctionsR Basic Operators β†’