CodeInfoNet

Learn programming on the go!

Home » Looping structures And Conditional statements

Looping structures And Conditional statements

In this article we are going to work on various looping structures and conditional statements. We will see while loop, for loop and various forms of if statement.

In previous article we have seen the logical operators. If you have missed it, here is the link: logical operators in R

Here we will see how does the while loop and for loop and if statement work. In R programming while loop is not actually used very much extensively. R has other ways of doing the same operations that while loop does. But it is always good to have the knowledge of things that exists.

Hence, the topics will be covered in this article are:

  1. While loop
  2. For loop
  3. If – else statement

  1. While loop

While loop is used when we want to execute some part of the code for particular time or till the particular condition is satisfied. As we discussed earlier, while loop is not used very much extensively as it is used in other programming languages. Still it is better to have the knowledge of it.

Syntax:

                while(condition)

                {

                                Body of the Loop

               }

Ex.:

a <- 1

b <- 20

while (a < b) # Condition to be checked

   {
      # Body of the loop
         print("While loop test")
         # increment the value of a to match the condition
         a <- a + 1
   } 
While loop example
While loop

2. For loop

Both for loop and while loop are used for same operations just the main difference is in the syntax of both. For loop iterates through the provided values. And another difference between for loop in R and for loop in other programming languages is, for loop in R iterates through the vector.

It is an interesting part in for loop here. Vector is an integral part in the R programming language. We are not going to get into vector in this article though.

Syntax:

for(i in a:b) # a:b is a vector

{

Body of the loop

}

Ex.:

# 1:10 is a vector R uses in a for loop
# if we select and execute 1:10, we will get output as
# 1  2  3  4  5  6  7  8  9 10

    for(i in 1:10) # iterations

       {
         # Body of the loop
         # These statements in the body will run for 10 times
             print("You are in the loop")
       }
For loop example
For loop
  1. If-else statement

If-else statement is a conditional statement which is available in any programming language.

The basic use of if-else statement is to check the conditions and execute the required code block.

To study the principle of if-else statement, we are going to use the rnorm() function of R.

rnorm() function generates the random number from specified range. If we don’t specify the particular range, it takes 0 to 1 as a default range. Also we can specify how many random numbers we want. We are going to generate only one in our case.

Syntax:

if(condition)

{

         Code block

}

Else

{

Code block

}

Ex.:

randomNumber <- rnorm(1)

if(randomNumber > 1)
{
    print("Number is greater than 1")
}

We have set the code block to be executed on successful condition but there might be the case that we may want to perform some other operations if the condition fails.

Here else statement comes into picture.

Let’s see the example of if-else statement.

# Nested If-Else Statement

if(randomNumber > 1)
{
     print("Number is greater than 1")

}else
{
  if(randomNumber >= -1)
   {
        print("Number is between -1 and 1")
   }else
   {
        print("Number is less than -1")
   }
}

The above example shows that it is nested if else. There is nothing wrong in it. It will run smoothly.

But the issue here is the nesting of if else statement goes on and it creates a complex and longer code.

As a programmer it is important to create an efficient as well as short code.

As we all know there is a solution to every problem.

Below is the alternative way to write the above code.

# Correct way to write If-Else Statement

if(randomNumber > 1)
{
    print("Number is greater than 1")
} else if(randomNumber >= -1)
    {
         print("Number is between -1 and 1")
     } else
{
  print("Number is less than -1")
}

This is the correct way to write if-else statements.

If-else statement example
If-else statement

Hence we have seen while loop, for loop and 3 forms of if-else statement.

Let us know if you like the article. You can fill the feedback form given below or mail us directly at admin@codeinfo.net.

[wpforms id=”22″]

Sumit Rajguru

Back to top