![]() ![]() So the output is displaying only the number is positive. For example, we take the no value as 6, so the no is positive. R code example where first it checks whether the no is positive or not and if the no is positive, it will check whether the no is even or not. The if statement can have another if statement or even if-else inside it it is called nested if statement and not only in if, the else part also can have nested if also nested if-else statement. Print(" Number is positive and less than 10 ") Print(" Number is positive and greater than 10 ") Among all condition, only one condition gets true, so only one statement gets to execute. The if statement checks the condition, but if more than one or alternative conditions are to check, the alternative conditions can be checked with else if statement. If not only one condition, if multiple conditions to be check and base on the specific condition the specific statement or statements to be executed, then the if-else if statement can be used. ![]() No 0) print(" Number is positive ") else print(" Number is Negative ") The above R code can be rewritten as a ternary operator R code example to see how the if-else statement works with no = -10 The R code example to see how the if-else statement works with no = 10 The Flow Diagram of the if-else statement The important note here is the else should be in the same line as the if statement closing braces. If cond is true, then all the statements inside the body are executed else, if the cond is false, then all statements inside the else body are executed. The above code checks whether the no is greater than zero if true, then print ” Number is positive, “else nothing.Īs in the above code, if the no is not greater than zero, nothing is performed, but if we need to perform or execute soothing, we can the else body. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |