Iterators

Three types of loops

  • while
  • for
  • do while

For loop

int i;
for (i = 0; i < 10; ++i) {
  printf("%d", i);
}

int i = 0;
for(; 1 < 10; i++)

for(; ; i++) {
  if(i < 10) {

  }
}

While loop

while(i < 10) {
   ++i;
}

while(i++ < 10) // executes 10 times
while(++i < 10) // executes 9 times

Do while

int x = 5;
do {
  printf("H");

} while (--x < 5);
int x = 5;
do {
  printf("H");

} while (x++ < 5);

Jump statements

  • break: Allows you to exit the loop
  • continue: Goes to the next iteration of the loop
  • return
  • goto

Do not use return and goto

Written on September 22, 2017