R programming language - Loops -


i have final coming soon, here examples teacher has posted can't understand it.

1.for , next

 for(i in 1:3)    {     print("a")     next     print("b")    } 

prints

[1] "a" [1] "a" [1] "a" 

2.nested for

how next statement work here ? continues loop if condition set know, example (if i==3) {next} ignore proceeds i=3 how work when not under condition ? searched web couldn't find answer

for(i in 1:3){   for(j in 1:3)     for(k in j:i)       print("*") } 

the code above produces 17 stars. how ?

3.another mystery

my.vector <- c(1,5,9,13) my.data <- c() for(i in my.vector){   if(i <= 5){     my.data <- c(my.data, "small")   }else{     my.data <- c(my.data, "huge")   }   my.data <- c(my.data, i) } 

the output of code above "9","huge","13"

seriously how ? shouldn't "small","small","huge","huge" ?

  1. from r control flow: next halts processing of current iteration , advances looping index. after printing "a", restarts beginning of loop while adding index 1.
  2. when = j (3 occasions), 3 stars produced; when |i-j| = 1 (4 occasions), 4*2 stars produced; , when |i-j| = 2 (2 occasions), 2*3 stars produced. in total, 3 + 8 + 6 = 17 stars.
  3. my output is: "small" "1" "small" "5" "huge" "9" "huge" "13"

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -