'Build Your Own Lisp' Solutions: 3.12

Q: What does the 'typedef' keyword do exactly?


A: 'typedef' lets you define a type, which is to say, it lets you give a new name to a pre-existing type (which can be useful for organizational/readability purposes), or to define a struct as a new type, unique to your program.



'Build Your Own Lisp' Solutions: 3.11

Q: What is the continue keyword and what does it do?


A: In a while or for loop, 'continue' will skip the rest of the block and go straight to the next iteration of the loop. Here is a program that counts to twenty, using 'continue' to filter out undesirable multiples of three:

#include <stdio.h>

int main(void)
{
  int i = 0;

  while (++i <= 20) {
    if (i % 3 == 0) {
      continue;
    }
    printf("%d\n", i);
  }

  return 0;
}
1
2
4
5
7
8
10
11
13
14
16
17
19
20


'Build Your Own Lisp' Solutions: 3.10

Q: What is the break keyword and what does it do?


A: Break breaks out of a while loop or a for loop, from within the body of that loop. Here is a program that will run a loop over nine quintillion times on most computers, before the number gets too bug for 'unsigned int' and goes back to zero:

#include <stdio.h>

int main(void)
{
  unsigned int i = 0;

  while (++i) {
    printf("%d: Looping...\n", i);
  }

  return 0;
}
1: Looping...
2: Looping...
3: Looping...
4: Looping...
5: Looping...
6: Looping...
7: Looping...
8: Looping...
9: Looping...
10: Looping...
...
9223372036854775806: Looping...

Since nine quintillion is overkill for most programs, we can use a break statement to stop it at, say, three.

#include <stdio.h>

int main(void)
{
  unsigned int i = 0;

  while (++i) {
    printf("%d: Looping...\n", i);
    if (i == 3) {
      break;
    }
  }

  return 0;
}
1: Looping...
2: Looping...
3: Looping...


'Build Your Own Lisp' Solutions: 3.9

Q: What is the switch statement and how does it work?

A: A switch statement syntactic sugar for certain combinations of 'if' and 'else', which is typically used where there are very many different possible conditions.

It is wildly controversial. Some feel that it is extremely convenient and clear, others feel that it can cause unexpected bugs (and they dislike it for other reasons a bit too technical for this post). I'm part of the former group, but I sympathize with the latter.

Here is an example:

#include <stdio.h>

void c_finder(char c)
{
  switch(c) {
    case 'a':
    case 'b':
      printf("The letter is too small!\n");
      break;
    case 'c':
      printf("The letter is just right!\n");
      break;
    default:
      printf("The letter is too large!\n");
  }
}

int main(void)
{
  c_finder('a');
  c_finder('b');
  c_finder('c');
  c_finder('z');

  return 0;
}
The letter is too small!
The letter is too small!
The letter is just right!
The letter is too large!

That switch statement is equivalent to:

  if (c == 'a' || c == 'b') {
    printf("The letter is too small!\n");
  } else if (c == 'c') {
    printf("The letter is just right!\n");
  } else {
    printf("The letter is too large!\n");
  }

Notice that 'switch' only tests equality with a constant. Something like 'else if (a > b)' doesn't really have a direct equivalent.

The reason some people dislike switch is the fall-through behaviour in the example. If a case is true, switch will execute all code down to the next 'break'. If you forget to write your breaks, you can end up with unexpected bugs. I think switch can be a useful tool, and can make code more readable, but caution and discipline are heavily recommended.



'Build Your Own Lisp' Solutions: 3.8

Q: What is the do loop, and how does it work?

A: A do loop is like a while loop, except that it checks the while condition after the block. In practice, this means that the block will always execute at least once. For example:


#include <stdio.h>

int main(void)
{
  int n = 1;

  while (n != 1) {
    printf("This will not be printed.\n");
  }

  do {
    printf("This will be printed.\n");
  } while(n != 1);

  return 0;
}
This will be printed.


'Build Your Own Lisp' Solutions: 3.7

Q: What other mathematical operators are there other than add +, and subtract -?

A: "n += 3" is equivalent to "n = n + 3". For example:


#include <stdio.h>

int main(void)
{
  int a, b;
  a = b = 0;

  a = a + 3;
  b += 3;

  printf("%d\n", a);
  printf("%d\n", b);

  return 0;
}
3
3


'Build Your Own Lisp' Solutions: 3.6

Q: What other mathematical operators are there other than add +, and subtract -?


A:




'Build Your Own Lisp' Solutions: 3.5

Q: What other conditional operators are there other than greater than >, and less than <?

A:



'Build Your Own Lisp' Solutions: 3.4

Q: What built in types are there other than the ones listed?


A: There are many. Consult the Wikipedia article for a detailed list.