Skip to content

Latest commit

 

History

History
84 lines (72 loc) · 1.38 KB

File metadata and controls

84 lines (72 loc) · 1.38 KB

#Loops

##while

// calling do_thing 10 times
int value = 0;
int max = 10;
while (value <= max) {
  do_thing();
  value++;
}

##for

// calling do_thing 10 times
int i;
int n = 10;
for (i = 0; i < n; i++) {
  do_thing();
}

##do while This is useful for cases where the body of the conditional must be executed at least once.

// calling do_thing once, even if n is 0
int n = 0;
do {
  do_thing();
}
while (n);

##break This keyword will exit from any surrounding for, while, do, or switch statement.

// Only calls do_thing once, and then exits loop
while (1) {
  do_thing();
  break;
}

##continue The continue keyword will skip to the next iteration of a surrounding loop.

// Only prints odd numbers
int i;
int n = 10;
for (i = 0; i < n; i++) {
  if ((i % 2) == 0)
    continue;

  printf("%d", i);
}

##Goto; an antipattern The use of goto should be avoided, but be aware that it exists.

// When we hit the first odd number
// jump to the label and handle the error
int i;
int n = 10;
for (i = 0; i < n; i++) {
  if ((i % 2) == 0)
    goto error;

  printf("%d", i);
}

error:
  printf("We encountered an odd number.");

You can always achieve the same functionality without using goto.

Reference: "The C Programming Language" sections 3.5-3.8