yiwll
yiwll

Reputation: 65

Trying to better understand switch statements and how their expressions are related/tests

I'm reading through my C textbook and I'm completely stumped on any attempt to understand the following code fragment. The book doesn't give a complete program and only states that the variable angle is a double, and angletype is an int which will somehow determine which case to use? Any help is appreciated, thank you.

switch (angletype)
{
 case DEG:
  angle *= PI / 180.0; /* convert to radians */
  /* fall through */
 case RAD:
  while (angle > PI) /* normalise radians */
   angle -= 2.0*PI;
  while (angle < -PI)
   angle += 2.0*PI;
  break;
 default:
  printf("Error: Invalid type\n");
  break;
}

EDIT: Here is what I have done so far with the fragment:

#include <stdlib.h>
#include <stdio.h>
#define DEG
#define RAD
#define PI 3.14


void main()
{

int angletype;
double angle;

switch (angletype)
{
 case DEG:
 angle *= PI / 180.0; /* convert to radians */
 /* fall through */
 case RAD:
  while (angle > PI) /* normalise radians */
  angle -= 2.0*PI;
  while (angle < -PI)
  angle += 2.0*PI;
 break;
 default:
 printf("Error: Invalid type\n");
 break;
}

}

I'm just having trouble coming up with some working prototype. I'm not sure how the expression "(angletype)" and the cases DEG and RAD will interact.

Upvotes: 0

Views: 36

Answers (3)

John Bode
John Bode

Reputation: 123558

A switch statement uses an integral expression as its control, and each case label is also an integral expression:

switch( some_integer )
{
  case 1: // do something if some_integer == 1
    break;

  case 2: // do something else if some_integer == 2
    break;

  case 3: // do some completely different thing if some_integer == 3
    break;
  ...

  default: // do something else if some_integer isn't a value covered by
           // any of the previous cases
}

Think of a switch as a computed GOTO, and each case label as one of the possible values.

For this particular snippet, DEG and RAD are symbolic constants for some integer value, probably defined as

#define DEG 0
#define RAD 1

or similar.

This particular code snippet is doing an angular calculation using radians, so it checks the angle type first to see if it needs to be converted from degrees to radians. If angletype is DEG, it branches to case DEG: to perform the conversion before doing the calculation. If angletype is RAD, it branches to case RAD: to perform the calculation. If angletype is any other value, it branches to default and prints an error message.

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385284

Your switch statement is equivalent to the [much clearer] following:

if (angletype == DEG)
{
   // Convert to radians
   angle *= PI / 180.0;
   angletype = RAD;
}

if (angletype == RAD)
{
   // Normalise radians
   while (angle > PI)
      angle -= 2.0 * PI;

   while (angle < -PI)
      angle += 2.0 * PI;
}
else
{
   printf("Error: Invalid type\n");
}

In other words, just a load of straightforward conditions. Nothing magic here.

Presumably, DEG and RAD name enums or some other integral value. And, presumably, angletype is set to one of them somewhere.

You can read more about switch in your C book.

Upvotes: 1

Caleb
Caleb

Reputation: 125007

The book doesn't give a complete program and only states that the variable angle is a double, and angletype is an int which will somehow determine which case to use?

It looks like angletype is probably an enumeration, like:

typedef enum {
    DEG,
    RAD
} AngleType;

AngleType angleType;

Somewhere in the code preceding the switch, the value of angletype has to be set to one of the values. (An enumeration isn't the only way to go... DEG and RAD could of course be #defines, but any way you slice it DEG and RAD have some set values known at compile time, and angletype is variable.

In the case where angletype is DEG, then the switch jumps to the DEG case:

 case DEG:
    angle *= PI / 180.0; /* convert to radians */
    /* fall through */
 case RAD:
     while (angle > PI) /* normalise radians */
 //...

First line in the body of the DEG case converts angle to radians by multiplying it by π/180 (because there are 180° in π radians). Once angle is converted to radians, then it makes sense to continue on with the RAD case for whatever additional work is needed. Notice that there's no break statement at the end of the DEG case... that means that execution doesn't stop there, it just keeps going on into the RAD case.

Upvotes: 1

Related Questions