Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - Example Listing

Node:Example 15, Next:, Previous:The switch statement, Up:Decisions



Example Listing


#include <stdio.h>

int main ();
void morse (int);


int main ()
{
  int digit;

  printf ("Enter any digit in the range 0 to 9: ");
  scanf ("%d", &digit);

  if ((digit < 0) || (digit > 9))
  {
    printf ("Your number was not in the range 0 to 9.\n");
  }
  else
  {
    printf ("The Morse code of that digit is ");
    morse (digit);
  }
  return 0;
}


void morse (int digit)        /* print out Morse code */
{
  switch (digit)
  {
    case 0 : printf ("-----");
      break;
    case 1 : printf (".----");
      break;
    case 2 : printf ("..---");
      break;
    case 3 : printf ("...--");
      break;
    case 4 : printf ("....-");
      break;
    case 5 : printf (".....");
      break;
    case 6 : printf ("-....");
      break;
    case 7 : printf ("--...");
      break;
    case 8 : printf ("---..");
      break;
    case 9 : printf ("----.");
  }
  printf ("\n\n");
}

The morse function selects one of the printf statements with switch, based on the integer expression digit. After every case in the switch, a break statement is used to jump switch statement's closing bracket }. Without break, execution would fall through to the next case and execute its printf statement.

Here is an example of using fallthrough in a constructive way. The function yes accepts input from the user and tests whether it was 'y' or 'Y'. (The getchar function is from the standard library and reads a character of input from the terminal. See getchar.)

#include <stdio.h>

int main ()
{
  printf ("Will you join the Free Software movement? ");
  if (yes())
  {
    printf("Great!  The price of freedom is eternal vigilance!\n\n");
  }
  else
  {
    printf("Too bad.  Maybe next life...\n\n");
  }

  return 0;
}


int yes()
{
  switch (getchar())
  {
    case 'y' :
    case 'Y' : return 1;
    default  : return 0;
  }
}

If the character is y, then the program falls through and meets the statement return 1. If there were a break statement after case 'y', then the program would not be able to reach case 'Y' unless an actual Y were typed.

Note: The return statements substitute for break in the above code, but they do more than break out of switch -- they break out of the whole function. This can be a useful trick.

 
 
  Published under free license. Design by Interspire