1.5 branching and subroutines


Published on

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Code:j = 1lineI:TextWindow.WriteLine(j)j = j + 1If j < 30 ThenGotolineQEndIf
  • In the first line of this program, you create a variable that is named j, and you set its value to 1.Then you create a label that is named lineQ: with a colon (:) at the end.In the next line, you tell the computer to display the value of the j variable on the screen.Then you increase the value of the j variable by 1.In the fourth line, you determine whether the value of the j variable is smaller than 10.--If it is, you tell the computer to repeat the lines of code that follow the lineQ: label. In other words, you tell the computer to display the value of the j variable, increase its value by 1, and then determine whether that value is smaller than 10.--If the value of the j variable is not smaller than 10, you tell the computer to continue to the next part of the program (or to stop running the program if no more code exists).Code:j = 1lineQ:TextWindow.WriteLine(j)j = j + 1If j < 10 ThenGotolineQEndIf
  • Solution:up:If i < 10 thenTextWindow.WriteLine("")TextWindow.Write(“What is the name of a city? ") city = TextWindow.Read()TextWindow.Write(“How warm is that city (in degrees Celsius)? ") temp = TextWindow.Read()TextWindow.Write(“Is it rainy (Y/N)? ") rainy = TextWindow.Read()TextWindow.Write(“Is it windy (Y/N)? ") windy = TextWindow.Read()'Calling subroutinessubtempCount()subrainyCount() subwindyCount()i = i + 1 If i = 10 ThensubOutput()EndIf EndIf Goto up Sub subtempCountIf temp <= 5 ThenColdCount = ColdCount + 1ElseIftemp <= 15 ThenCoolCount = CoolCount + 1 ElseIftemp <= 25 ThenWarmCount = WarmCount + 1 ElseHotCount = HotCount + 1 EndIfEndSubSub subRainyCountIf Rainy = "y" Or Rainy = "Y" ThenRainyCount = RainyCount + 1EndIfEndSubSub subWindyCountIf Windy = "y" or Windy = "Y" thenWindyCount = WindyCount + 1EndIfEndSub Sub subOutputTextWindow.WriteLine("")TextWindow.WriteLine("Number of cold cities: " + ColdCount) TextWindow.WriteLine("Number of cool cities: " + CoolCount) TextWindow.WriteLine("Number of warm cities: " + WarmCount)TextWindow.WriteLine("Number of hot cities: " + HotCount)TextWindow.WriteLine("Number of rainy cities: " + RainyCount)TextWindow.WriteLine("Number of windy cities: " + WindyCount) EndSub
  • 1.5 branching and subroutines

    1. 1. Microsoft® Small Basic<br />Branches and Subroutines<br />Estimated time to complete this lesson: 1 hour<br />
    2. 2. Code Branches and Subroutines<br />In this lesson, you will learn how to:<br />Create subroutines by using Sub and EndSub statements.<br />Branch your code by using Goto statements.<br />
    3. 3. Branching<br />You can instruct the computer to process a line of code out of sequence if you use the Gotostatement. <br />Sometimes, you may want the computer to break the flow and jump to another line of code while the program is running. <br />As you know, the computer runs a program by reading and processing the statements line by line, one at a time. <br />
    4. 4. Branching in Small Basic Programs<br />Let’s examine the Goto statement and its various parts by writing a program.<br />In this program, the lineQ: statement is called a label, which is similar to a bookmark. You can add as many labels as you want and name them whatever you want, as long as you don’t use the same name more than once.<br />The Goto statement instructs the computer to run the statements after the lineQ: label again only if the condition in the If statement is true. <br />output<br />
    5. 5. Branching in Small Basic Programs<br />You can also use the Goto statement to make a program run forever. <br />Let’s see how Goto statements work by adding one to a familiar program.<br />This program will continue to run until someone clicks the Close (X) button in the top-right corner of the text window.<br />
    6. 6. Subroutines in Small Basic Programs<br />When we write programs, we often want the computer to run certain statements more than once. You can avoid writing the same statements over and over if you use subroutines in your programs. <br />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.<br />Look at the following subroutine named PrintHour, which opens a text window and displays the current hour. <br />
    7. 7. Subroutines in Small Basic Programs<br />Let’s gain a better understanding of subroutines by writing another program…<br />In this program, we use the Divide( ) statement torun (or “call”) the subroutine Divide from any location within the program.<br />output<br />
    8. 8. Let’s Summarize…<br />Congratulations! Now you know how to:<br /> Create a branch by using a Goto statement.<br /> Create a subroutine by using a Sub..EndSub statement.<br />
    9. 9. Show What You Know<br />Write a program that opens a text window and then performs the following steps: <br /><ul><li>Asks the user for the name, temperature, rain status, and wind status of 10 cities.
    10. 10. Uses branching and subroutines to determine and display the total number of:
    11. 11. Cold Cities
    12. 12. Cool Cities
    13. 13. Warm Cities
    14. 14. Hot Cities
    15. 15. Rainy Cities
    16. 16. Windy Cities</li>