Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Control Structures in R

r program control structure

Uploaded by

vijayendrar219
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views

Control Structures in R

r program control structure

Uploaded by

vijayendrar219
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
| Control Structures in R Decision making is about deciding the order of execution of statements based on certain conditions. in decision making programmer needs to provide some condition which is evaluated by the program, along with it there also provided some statements which are executed if the condition is true and optionally other statements if the condition is evaluated to be false. In R programming, there are 8 types of control statements as follows: if condition if-else condition for loop nested loops while loop repeat and break statement return statement Next statement if condition This control structure checks the expression provided in parenthesis is true or not. If true, the execution of the statements in braces (} continues Syntax if(expression){ statements } Example: x<100 if > 10) print(paste(x, “is greater than 10")) } if-else condition Itis similar to if condition but when the test expression in if condition fails, then statements in else condition are executed syntax if(expression)( statements | J else statements # Check value is less than or greater than 10 if( > 10)( print(paste(x, "is greater than 10") Jelsel print(paste(x, "is less than 10")) } ifelse-if ladder Itis similar to if-else statement, here the only difference is that an if statement is attached to else. If the condition provided to if block is true then the statement within the if block gets executed, else-if the another condition provided is checked and if true then the statement within the block gets executed, Syntax if(condition 1 is true) { execute this statement }else if(condition 2 is true) { execute this statement felse ( execute this statement } Example | | # R ifelserf ladder Example a<67 b<76 c<99 if(a> b&b >) { print(‘condition a > b > c is TRUE") Jelse if(a ) { print(‘condition a c is TRUE") }else if(a 5 break } } return statement return statement is used to retum the result of an executed function and returns control to the calling function. Syntax return(expression) Example # Checks value is either positive, negative or zero func < function(»){ if > O){ return(‘Positive") Jelse if(x < 0)( return(‘Negative") Jelsel return(‘Zero") } } func(1) func(0) func(-1) next statement next statement is used to skip the current iteration without executing the further statements and continues the next iteration cycle without terminating the loop. | - Example: # Defining vector x< 1:10 # Print even numbers for(i in x} if (%%2 I= 0) next #Jumps to next loop } print(i) }

You might also like