Monday, April 1, 2013

The traffic light program (switch)

A program that displays the recommended actions depending on the color of a traffic light. Unlike the previous program, this implementation uses the switch statement.

/* The traffic light program (switch) */
#include <stdio.h> int main (void) { char colour; /* ask user for colour */ printf ("Enter the colour of the light (R,G,Y,A): "); scanf ("%c", &colour); /* test the alternatives */ switch (colour) { /* red light */ case 'R': case 'r': printf ("STOP! \n"); break; /* yellow or amber light */ case 'Y': case 'y': case 'A': case 'a': printf ("CAUTION! \n"); break; /* green light */ case 'G': case 'g': printf ("GO! \n"); break; /* other colour */ default: printf ("The colour is not valid.\n"); } return (0); }
Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
Enter the colour of the light (R,G,Y,A): G
GO!

8 comments:

  1. Great coding for trafice signal..share some tips to avoid speeding ticket.
    Limestone County Alabama Speeding Ticket

    ReplyDelete
  2. I think this is a standout amongst the most critical data for me. What's more, i'm happy perusing your article. Be that as it may, ought to comment on some broad things, The site style is flawless, the articles is truly awesome : D. Great job, here's to you Easy Traffic School Online California for Traffic Tickets

    ReplyDelete