Saturday, November 21, 2020

If-else statement

 In the if statement, the inner code is executed when the condition is true. The code which is outside the if block will be executed when the if condition is false.

There is another type of decision-making statement known as the if-else statement. An if-else statement is the if statement followed by an else statement. An if-else statement, else statement will be executed when the boolean expression will false. In simple words, If a Boolean expression will have true value, then the if block gets executed otherwise, the else block will get executed.

R programming treats any non-zero and non-null values as true, and if the value is either zero or null, then it treats them as false.


The basic syntax of If-else statement is as follows:

  1. if(boolean_expression) {  
  2.    // statement(s) will be executed if the boolean expression is true.  
  3. else {  
  4.    // statement(s) will be executed if the boolean expression is false.  
  5. }  

Flow Chart

R If-else statement

Example 1

  1. # local variable definition  
  2. a<- 100  
  3. #checking boolean condition  
  4. if(a<20){  
  5.     # if the condition is true then print the following  
  6.     cat("a is less than 20\n")  
  7. }else{  
  8.     # if the condition is false then print the following  
  9.     cat("a is not less than 20\n")  
  10. }  
  11. cat("The value of a is", a)  

Output:

R If-else statement

Example 2

  1. x <- c("Hardwork","is","the","key","of","success")  
  2.   
  3. if("key" %in% x) {    
  4.    print("key is found")  
  5. else {  
  6.    print("key is not found")  
  7. }  

Output:

R If-else statement

Example 3

  1. a<- 100  
  2. #checking boolean condition  
  3. if(a<20){  
  4.     cat("a is less than 20")  
  5.     if(a%%2==0){  
  6.         cat(" and an even number\n")  
  7.     }  
  8.     else{  
  9.         cat(" but not an even number\n")  
  10.     }  
  11. }else{  
  12.     cat("a is greater than 20")  
  13.     if(a%%2==0){  
  14.         cat(" and an even number\n")  
  15.     }  
  16.     else{  
  17.         cat(" but not an even number\n")  
  18.     }  
  19. }  

Output:

R If-else statement

Example 4

  1. a<- 'u'  
  2. if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u'||a=='A'||a=='E'||a=='I'||a=='O'||a=='U'){  
  3.     cat("character is a vowel\n")     
  4. }else{  
  5.     cat("character is a constant")  
  6. }  
  7. cat("character is =",a)  
  8. }  

Output:

R If-else statement

Example 5

  1. a<- 'u'  
  2. if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u'||a=='A'||a=='E'||a=='I'||a=='O'||a=='U'){  
  3.     cat("character is a vowel\n")     
  4. }else{  
  5.     cat("character is a constant")  
  6. }  
  7. cat("character is =",a)  
  8. }  

Output:

R If-else statement

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...