标签:size if else switch code inf while color class nbsp
1、for语句
for (i in 1:5) {
print("hello world!")
}
2、for语句
sum = 0
for (i in 1:100){
sum = sum + i
}
print(sum)
3、for语句
sum = 0
for (i in 1:100) {
if (i %% 2 == 0)
{
sum = sum + i
}
}
print(sum)
4、while语句
i = 5
while (i > 0) {
print("hello world!")
i = i - 1
}
5、while语句
i = 1
j = 5
while (i <= j) {
print("hello world!")
i = i + 1
}
6、while语句
sum = 0
i = 100
while (i > 0) {
sum = sum + i
i = i - 1
}
sum
7、while语句
sum = 0
i = 100
while (i > 0) {
if (i %% 2 != 0){
sum = sum + i
}
i = i - 1
}
print(sum)
8、if语句
i = 1
if (i > 0) {
print("positive")
}
i = -1
if (i > 0) {
print("positive")
}
9、if语句
i = 1
if (i > 0) {
print("positive")
} else
print("<= 0")
i = -7
if (i > 0) {
print("positive")
} else
print("<= 0")
10、if语句
i = 1
if (i > 0) {
print("positive")
} else if (i == 0) {
print("zero")
} else
{
print("negative")
}
i = 0
if (i > 0) {
print("positive")
} else if (i == 0) {
print("zero")
} else
{
print("negative")
}
i = -1
if (i > 0) {
print("positive")
} else if (i == 0) {
print("zero")
} else
{
print("negative")
}
11、if语句
i = 1
ifelse(i > 0, print("positive"), print("<= 0"))
i = 0
ifelse(i > 0, print("positive"), print("<= 0"))
12、switch语句
switch (1,"aa","bb","cc","dd")
switch (2,"aa","bb","cc","dd")
switch (3,"aa","bb","cc","dd")
R语言中for、while、if else、switch语句
标签:size if else switch code inf while color class nbsp
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14708464.html