Biostatistics with R

if else statements

The if condition

The 'if' conditional statement helps us to execute certain commands subject to the condition that a given statement is TRUE.


The general syntax of 'if' statement is given by

if(condition) statement

Here if is a reserved key word. the condition typed inside braces refers to a logical statement. The statement refers to a single or a set of statements which will be executed if the condition is true.


First the condition is logically evaluated and if it evaluates to TRUE, the statement is executed. If the condition evaluates to FALSE, the statement is not executed.

Following script illustrates this:


a = 5.0 b = 10.0 if(a < b) print("a is less than b")

In the above code, the condition ( a < b ) is evaluated. Since a = 5.0 and b = 10.0, the condition evaluates to TRUE, and hence the print statement is executed. If the condition evaluates to false, print statement will not be executed.

Executing the above code prints this output in R prompt:

[1] "a is less than b"

The else branch

When the condition in the if statement fails, we can provide an alternate path with an else statement followed by if. The general format is as follows:


if(condition) statement1 else statement2

When condition evaluates to TRUE, statement1 is executed. When it fails, statement2 is executed. See the illustration below:


a = 5.0 b = 10.0 c = 15.0 d = 20.0 if(a > b) { print("a is greater than b") } else { print("a is not greater than than b") }

Since a=5.0 and b=10.0, the condition (a > b) evaluates to false in the above code, and the control is transferred to the else condition and the following line is printed when the script is executed:

[1] "a is less than b"
In the above script, note the use of curly brackets to enclose the statements. When there are more than one statement under "if" or "if else" condition, enclosing them inside curly bracket is a must. Otherwise, only the statement immediately following the "if" or "if else" condition will be taken to be a part of the condition, and other statements will be left out.

Nested if else conditions

A set of nested if...else if conditions can be set up as shown in the example below. The code locates the appropriate coordinate or or axis for a given angle in degrees for a cartesian coordinate system. The code is self explanatory.


# This script gets the quadrant or axis for a given angle. # theta is the angle in degrees: theta = 181 if(theta == 0) { print("Positive X axis") } else if ( (theta > 0) & (theta < 90)) { print("First quadrant") } else if (theta == 90) { print("Positive Y axis") } else if ( (theta > 90) & (theta < 180)) { print("Second quadrant") } else if (theta == 180) { print("Negative X axis") } else if ( (theta > 180) & (theta < 270)) { print("Third quadrant") } else if (theta == 270) { print("Negative Y axis") } else if ( (theta > 270) & (theta < 360)) { print("Fourth quadrant") } else if (theta == 360) { print("Positive X axis") }

For theta = 39.2 degrees, the above code, when executed as a file "coord.r" prints:

> source("coord.r")
[1] "First quadrant"