Sunday 12 July 2015

R: if-else statement

if statement
“if ” tell the program execute the section of code when the condition evaluates to true.

if(condition){
         Do some action
}

if-else statement
If the condition true, then if block code executed. other wise else block code executed.

if(condition){
         Do some action
}
else{
         Do some action
}

if-else if-else
By using if-else if-else construct, you can choose number of alternatives.

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions.

if(condition){
         Do some action
}
else if{
         Do some action
}
else{
         Do some action
}


IfStmt.R
marks = 45

if(marks < 35){
  print("You are failed")
}else if(marks >=35 & marks < 60){
  print("Second class")
}else if(marks >= 60 & marks < 70){
  print("First class")
}else if(marks >= 70 & marks <101 ){
  print("Distinction")
}else{
  print("Wrong marks")
}

Open command prompt (or) terminal, run following command.

$ Rscript IfStmt.R

[1] "Second class"





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment