Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed/removed incorrect statement about "//the incrementer isn't executed automatically when we 'continue'" in a for loop

...

Code Block
def list= []
for(def i=0; i<5; i++){
      //first value an initializer, second a condition, third an incrementer
  list<< i
}
assert list == [0, 1, 2, 3, 4]

//equivalent while-statement...
list= []
try{
  def i=0 //initializer
  while( i<5 ){ //condition
    list<< i
    i++ //incrementer
  }
}
assert list == [0, 1, 2, 3, 4]

//for-statement with 'break'
list= []
for(def i=0; i<5; i++){
  list<< i
  if( i == 2 ) break
}
assert list == [0, 1, 2]

//equivalent while-statement with 'break'
list= []
try{
  def i=0
  while( i<5 ){
    list<< i
    if( i == 2 ) break
    i++
  }
}
assert list == [0, 1, 2]

//for-statement with 'continue'
list= []
for(def i=0; i<5; i++){
  if( i == 2 ){ i++; continue }
      //the incrementer isn't executed automatically when we 'continue'
  list<< i
}
assert list == [0, 1, 3, 4]

//equivalent while-statement with 'continue'
list= []
try{
  def i=0
  while( i<5){
    if( i == 2 ){ i++; continue }
    list<< i
    i++
  }
}
assert list == [0, 1, 3, 4]

...