Flat 10% & upto 40% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!

# Decision Making and Loops

R Programming supports decision-making like any other programming language with the help of following statements :

1. if statement – The if statement holds a logical or numeric value and executes the codes if it is TRUE. If the logical value is FALSE, nothing happens.
```Syntax of if statement -
if (test_expression)

{

statement

}

For example,

x <- 5

if (x > 0)

print ("positive number")

Output-    [1] "positive number"```
1. If..else statement – Here the if statement is followed by an optional statement which is executed only when the condition is FALSE.
```Syntax of if...else statement -

if (test_expression)

{

statement1

}

else

{

statement2

}

For example,

x <- -5

if(x > 0)

{

print("Non-negative number")

}

else

{

print("Negative number")

}```
1. switch statement – Here a variable is checked against a number of case values.
```            Syntax of switch statement-

switch (test_expression, case1, case2, case3 .... caseN)

For example,

gk <- switch ( 2,

"First",

"Second",

"Third",

"Fourth"

)

print (gk)

## [1] "Second"```

## Loops in R Programming

When a certain activity in programming needs to be repeated, then loops are used. R programming supports four types of loop s:

1. for loop – A looping vector is assigned a value which is iterated in a loop for certain number of times. For each iteration the statement is evaluated.
```Syntax of for loop

for (val in sequence)

{

statement

}

For example,

x <- c(2,5,3,9,8,11,6)

count <- 0

for (val in x)

{

if(val %% 2 == 0)  count = count+1

}

print(count)

Output-    [1] 3```
1. while loop – The while continues executing a block of codes until a specific condition is met.
```while (test_expression)

{

statement

}

For example,

i <- 1

while (i < 6)

{

print(i)

i = i+1

}

Output

[1] 1

[1] 2

[1] 3

[1] 4

[1] 5```
1. break & next statements – Break statement interrupts the execution of a loop and directs the flow outside. These are used in case of nested loops to come out of the loops.
```For example,

x <- 1:5

for (val in x)

{

if (val == 3)

{

break

}

print(val)

}

Output

[1] 1

[1] 2```

Upon encountering next statement the parser skips the current loop without terminating it and jumps to next iteration.

```x <- 1:5

for (val in x) {

if (val == 3){

next

}

print(val)

}

Output

[1] 1

[1] 2

[1] 4

[1] 5```
1. repeat loop – It repeats a block of code over and over again as there is not conditional statement to stop the loop. The programmer needs to put the conditions inside the loop and use break statement to exit.
```Syntax of repeat loop

repeat {

statement

}

For example,

x <- 1

repeat {

print(x)

x = x+1

if (x == 6){

break

}

}

Output -

[1] 1

[1] 2

[1] 3

[1] 4

[1] 5```

### "0 Responses on Decision Making and Loops"

Training in Cities: Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

## Sales Offer

• To avail this offer, enroll before 24th January 2018.
• This offer cannot be combined with any other offer.
• This offer is valid on selected courses only.
• Please use coupon codes mentioned below to avail the offer