循环语句:
1.while(condition){
expression
}
遍历list p,打印出其值
p<-list('a','b','c')
index<-1
while(index<=length(p)){
print(p[index])
index<-index+1
}
2.for (n in x) {
expression
}
遍历list p,打印出其值:
p<-list('a','b','c')
for(index in 1:length(p)){
print(p[index])
}
**********
判断语句:
1.if (conditon) {expression1} else {expression2}
遍历list p,打印出元素为'b'的下标和值:
p<-list('a','b','c')
for(index in 1:length(p)){
if(p[index]=='b'){
print(p[index])
print(index)
}
}
2.ifelse(condition,'val1,'val2')
类似三目运算符,condition为真那么val1否则为val2
遍历list p,打印出元素为'b'的下标,非b的话打印NAN:
p<-list('a','b','c')
for(index in 1:length(p)){
val<-ifelse(p[index]=='b',index,'NAN')
print(val)
}