Branching instructions

To make programs more useful and responsive, they need to accept and react to outside data. When you play a video game and a monster appears on-screen, the game needs to react differently if you shoot and kill the monster or if the monster eats and kills you. If you shoot and kill the monster, the game lets you keep playing. If the monster kills and eats you, the game displays the words GAME OVER on-screen.

Branching instructions simply tell the computer to run different instructions depending on the value of certain data. When you delete a file, many programs ask, Do you really want to delete the file?. If you click the Yes button, the program follows the instructions to delete the file. If you click the No button, the program follows a different set of instructions

The following Liberty BASIC program shows how a simple branching instruction can work. If the user types the letter Y, the program displays the message Deleting file. If the user types any other letter, the program displays the message File NOT deleted.


PROMPT “Do you want to delete the file? (Y or N)”; answer$

IF answer$ = “Y” THEN

  NOTICE “Deleting file”

ELSE

  NOTICE “File NOT deleted”

END IF

END

Remember Branching instructions let a program respond to data. Branching instructions typically contain two or more alternative instructions, but the computer can follow only one of these sets of instructions at a time.


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply



eXTReMe Tracker