Only used at the beginning of a conditional statement
Else if statement: if previous statements aren't true, try this
Can be used an unlimited number of times in an if statement
Else statement: catch-all for anything outside of prior statements
Only used to end a conditional statement
Inputs:
#If statementa <-2b <-1if (a > b){print("a is greater than b")}#Else if statementx <-10y <-10if (x > y){print("x is greater than y")} elseif (x <= y){print("x is less than or equal to y")}#Else statementd <-3if (d >5){print("d is greater than 5")} elseif (d ==5){print("d is equal to 5")} else {print("d is less than or equal to 5")}
Outputs:
#If statement[1] "a is greater than b"#Else if statement[1] "x is less than or equal to y"#Else statement[1] "d is less than or equal to 5"
Loops
Repeats a block of code a specified number of times or until some condition is met
While loop
For loop
Use break to terminate loop
Inputs:
#While loopi <-1while (i <5){print(i)i <- i +1}#While loop with breakj <-1while (j <5){print(j)j <- j +1if (j ==4){break}}#For loopfruit <-list("apple", "banana", "peach")for (x in fruit) {print(x)}#Nested for loopadjectives <-list("scrumptious", "overripe", "delicious")fruit <-list("apple", "banana", "peach")for (x in adjectives) {for (y in fruit) {print(paste(x, y))}}