This answer uses the most brute-force while loop. The result is in summation.
while
summation
a=c(1,2,-1,3,1,0) n=length(a) i=1 sum=0 summation=0 while(i<=n){ if (a[i]>=0){ sum=sum+a[i] } summation[i]=sum i=i+1 }