Thursday, May 27, 2010

Example




A traffic light showing red

Suppose a computer is being employed to operate a traffic light at an intersection between two streets. The computer has the following three basic instructions.

  1. ON(Streetname, Color) Turns the light on Streetname with a specified Color on.
  2. OFF(Streetname, Color) Turns the light on Streetname with a specified Color off.
  3. WAIT(Seconds) Waits a specifed number of seconds.
  4. START Starts the program
  5. REPEAT Tells the computer to repeat a specified part of the program in a loop.

Comments are marked with a // on the left margin. Comments in a computer program do not affect the operation of the program. They are not evaluated by the computer. Assume the streetnames are Broadway and Main.

START 
//Let Broadway traffic go OFF(Broadway, Red) ON(Broadway, Green) WAIT(60 seconds) 
//Stop Broadway traffic OFF(Broadway, Green) ON(Broadway, Yellow) WAIT(3 seconds) OFF(Broadway, Yellow) ON(Broadway, Red) 
//Let Main traffic go OFF(Main, Red) ON(Main, Green) WAIT(60 seconds) 
//Stop Main traffic OFF(Main, Green) ON(Main, Yellow) WAIT(3 seconds) OFF(Main, Yellow) ON(Main, Red) 
//Tell computer to continuously repeat the program. REPEAT ALL 

With this set of instructions, the computer would cycle the light continually through red, green, yellow and back to red again on both streets.

However, suppose there is a simple on/off switch connected to the computer that is intended to be used to make the light flash red while some maintenance operation is being performed. The program might then instruct the computer to:

START 
IF Switch == OFF then: //Normal traffic signal operation { //Let Broadway traffic go OFF(Broadway, Red) ON(Broadway, Green) WAIT(60 seconds) 
//Stop Broadway traffic OFF(Broadway, Green) ON(Broadway, Yellow) WAIT(3 seconds) OFF(Broadway, Yellow) ON(Broadway, Red) 
//Let Main traffic go OFF(Main, Red) ON(Main, Green) WAIT(60 seconds) 
//Stop Main traffic OFF(Main, Green) ON(Main, Yellow) WAIT(3 seconds) OFF(Main, Yellow) ON(Main, Red) 
//Tell the computer to repeat this section continuously. REPEAT THIS SECTION } 
IF Switch == ON THEN: //Maintenance Mode { //Turn the red lights on and wait 1 second. ON(Broadway, Red) ON(Main, Red) WAIT(1 second) 
//Turn the red lights off and wait 1 second. OFF(Broadway, Red) OFF(Main, Red) WAIT(1 second) 
//Tell the computer to repeat the statements in this section. REPEAT THIS SECTION } 

In this manner, the traffic signal will run a flash-red program when the switch is on, and will run the normal program when the switch is off. Both of these program examples show the basic layout of a computer program in a simple, familiar context of a traffic signal. Any experienced programmer can spot many software bugs in the program, for instance, not making sure that the green light is off when the switch is set to flash red. However, to remove all possible bugs would make this program much longer and more complicated, and would be confusing to nontechnical readers: the aim of this example is a simple demonstration of how computer instructions are laid out.

No comments:

Post a Comment