Professional Documents
Culture Documents
1.5 - Branching and Subroutines
1.5 - Branching and Subroutines
Branching
As you know, the computer runs a program by reading and processing the statements line by line, one at a time.
Sometimes, you may want the computer to break the flow and jump to another line of code while the program is running.
You can instruct the computer to process a line of code out of sequence if you use the Goto statement.
Lets see how Goto statements work by adding one to a familiar program.
This program will continue to run until someone clicks the Close (X) button in the top-right corner of the text window.
By using a subroutine, you can run one or more statements with a single instruction. To create a subroutine, you use the Sub keyword, and then you give the subroutine a specific name. You end the subroutine by using the EndSub keyword.
Look at the following subroutine named PrintHour, which opens a text window and displays the current hour.
In this program, we use the Divide( ) statement to run (or call) the subroutine Divide from any location within the program.
Lets Summarize
Cool Cities
Warm Cities Hot Cities Rainy Cities Windy Cities