Search code examples
cif-statementstatements

Consecutive if statement flow


if (srcbloc == NULL) {
    fprintf(stderr, "warning!: memrip source is null!\n");
    exit(1);
}
if (destbloc == NULL) {
    destbloc = malloc(len);
}
if (srcbloc == destbloc) {
    fprintf(stderr, "warning!: srcbloc = destbloc\n");
    exit(1);
}
if (offset < 0) {
    fprintf(stderr, "warning!: offset = %i\n", offset);
}
if (len < 0) {
    fprintf(stderr, "warning!: len = %i\n", len);
}

I am wondering if all of the if statements will be tested when this program is run?


Solution

  • Given your code

    if (srcbloc == NULL) { /* <-- if this block is entered then, */
      fprintf(stderr, "warning!: memrip source is null!\n"); 
      exit(1); /* <-- Program will exit */ 
    }
    if (destbloc == NULL) {  /* <-- Allocate destbloc of len length. */
      destbloc = malloc(len); 
    }
    if (srcbloc == destbloc) { /* <-- if this block is entered then, */
      fprintf(stderr, "warning!: srcbloc = destbloc\n"); 
      exit(1); /* <-- Program will exit */ 
    }
    if (offset < 0) { 
      fprintf(stderr, "warning!: offset = %i\n", offset); 
    }
    if (len < 0) { 
      fprintf(stderr, "warning!: len = %i\n", len); 
    }
    

    So, if (srcbloc == NULL) or (srcbloc == destbloc) the program will warn (and exit). If any of the other tests match the warnings will be printed but the program will continue to process.