Return index

Conditionals and Loops

BEGIN and END

As we'e already informally seen, and as is the case in related languages like Java, in the C Language, the beginning of a code block is noted with an {-open-squiggly, and the end is marked with a }-closed-squiggly.

You've already seen an example of this with the main() function.

You'll also see that this is used to set off the body of conditionals and loops.

Conditionals

The basic conditional in the C Language is the if-statement, which can have a basic if-only form, or an if-else form. There is no special "else if" syntax, although it can be expressed using a combination of an if-else and a simple if.

  if (x > 5) {
    printf ("X is greater than 5\n");
  }
  

  /*
   * Notice, the example below
   * The condition could also have been expressed as (0 == x) or (x == 0)
   * But, since 0 is false and any non-zero value is true, the simple 
   * expression below captures the same logic in a more compact way and
   * would be a more common way for an experienced C programmer to write
   * the expression
   */
  if (!x) {
    printf ("X is 0 or null\n");
  }
  

  if ( (x < minimum) || (y > maximum) {
    printf ("Value is out of bounds.\n");
  } else {
    printf ("Value is within bounds.\n");
  }
  

  /*
   * This is one way of expressing else-if syntax
   */
  if ( (type == SMALL) && (x < 5)) {
    printf ("Good small.\n");
  } else {
    if (type == MEDIUM) && (x >=5) && (x <=10) ) {
      printf ("Good medium.\n");
    } else {
      if (type == LARGE) && (x >10) ) {
        printf ("Good large.\n");
      else {
        printf ("Size mismatch.\n");
      }
    }
  }
  

  /*
   * But, in the C Language, when the body of a conditional is a single statement large
   * it does not need to be set of with {}-squigglies. But, be careful, this applies to
   * single statements vs single lines. An if-statement or an if-else statement is a single
   * statement, even if it occupies multiple lines. The example above can also be written as 
   * below:
   */
  if ( (type == SMALL) && (x < 5)) 
    printf ("Good small.\n");
  else 
    if (type == MEDIUM) && (x >=5) && (x <=10) ) 
      printf ("Good medium.\n");
    else if (type == LARGE) && (x >10) ) 
        printf ("Good large.\n");
    else printf ("Size mismatch.\n");
  

Loops

The most basic loop in the C Language is the "while loop". It is like an if statement that repeats so long as the condition is true. It is what is known as a "pre test" loop because the condition is tested before the first entry intro the loop and before each entry thereafter. As with conditionals, the {}-squigglies are only required if the body of the loop has more than one statement.

  int x = 1;
  while (x <= 10) {
    printf ("Number: %d\n", x);
    x = x + 1 // or x++
  }
  

The C Language aslo has a "post-test" loop, in which the loop body is always executed at least once because the condition is checked only at the end of the loop. This is known as the "do-while loop":

  int x = 1;
  do {
    printf ("Number: %d\n", x);
    x = x+1; // or x++
  } while (x <= 10); 
  

Programmers often want to initialize a condition before the loop, check a condition before each iteration of a loop, and update the condition after each iteration of a loop. For this purpose, the C Language provides the "for loop". The following loops are equivalent.

  int x = 1;
  while (x <= 10) {
    printf ("Number: %d\n", x);
    x = x + 1; // or x++
  }
  

  for (x = 1; x <=10; x++) {
    printf ("Number: %d\n", x);
  }
  

One can exit a loop at any time using "break", which transfers control to the code immediately after the loop.

  for (x = 1; x <=10; x++) {
    int abort = 0;
    printf ("Number: %d\n", x);

    abort = checkForAbort(); // Returns 0 normally, non-zero if the loop should be aborted
    if (abort) break;

    // Could also be written as below: 
    .. if (checkForAbort()) break;
    
  }
  

One can jump directly to the top of a loop at any time using "continue", which transfers control to the top of the loop. In the case of a do-while loop, the condition is not tested as the end is not reached. In the case of a for loop, the update doesn't occur as the body never completed. In the caseof a while or for loop, the condition is evaluated before re-entering the body.

  for (x = 1; x <=10; x++) {
    int retry = 0;
    printf ("Number: %d\n", x);

    retry = checkForRetry(); // Returns 0 normally, non-zero if the loop body should be repeated
    if (retry) continue;

    // Could also be written as below: 
    .. if (checkForRetry()) continue;
    
  }