SlideShare a Scribd company logo
1 of 9
Microsoft® Small Basic Branches and Subroutines Estimated time to complete this lesson: 1 hour
Code Branches and Subroutines In this lesson, you will learn how to: Create subroutines by using Sub and EndSub statements. Branch your code by using Goto statements.
Branching You can instruct the computer to process a line of code out of sequence if you use the Gotostatement.  Sometimes, you may want the computer to break the flow and jump to another line of code while the program is running.  As you know, the computer runs a program by reading and processing the statements line by line, one at a time.
Branching in Small Basic Programs Let’s examine the Goto statement and its various parts by writing a program. 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. 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.  output
Branching in Small Basic Programs You can also use the Goto statement to make a program run forever.  Let’s 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.
Subroutines in Small Basic Programs 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.  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.
Subroutines in Small Basic Programs Let’s gain a better understanding of subroutines by writing another program… In this program, we use the Divide( ) statement torun (or “call”) the subroutine Divide from any location within the program. output
Let’s Summarize… Congratulations! Now you know how to:  Create a branch by using a Goto statement.  Create a subroutine by using a Sub..EndSub statement.
Show What You Know Write a program that opens a text window and then performs the following steps:  ,[object Object]

More Related Content

What's hot (12)

5.1 sharing code
5.1   sharing code5.1   sharing code
5.1 sharing code
 
3.6 debugging aids
3.6   debugging aids3.6   debugging aids
3.6 debugging aids
 
Small basic
Small basicSmall basic
Small basic
 
Programming language environments
Programming language environmentsProgramming language environments
Programming language environments
 
Fd223 cg223 evernote
Fd223 cg223 evernoteFd223 cg223 evernote
Fd223 cg223 evernote
 
Hey Siri, open my PowerApp
Hey Siri, open my PowerAppHey Siri, open my PowerApp
Hey Siri, open my PowerApp
 
Creating a Personalized ipad Wallpaper
Creating a Personalized ipad WallpaperCreating a Personalized ipad Wallpaper
Creating a Personalized ipad Wallpaper
 
Stepshot Intro
Stepshot IntroStepshot Intro
Stepshot Intro
 
2.6 flickr, image list, and network objects
2.6   flickr, image list, and network objects2.6   flickr, image list, and network objects
2.6 flickr, image list, and network objects
 
Small Basic - Branching and Loop
Small Basic - Branching and LoopSmall Basic - Branching and Loop
Small Basic - Branching and Loop
 
Java
JavaJava
Java
 
Html tutorial
Html tutorialHtml tutorial
Html tutorial
 

Viewers also liked (16)

2.5 clock, desktop, and dictionary objects
2.5   clock, desktop, and dictionary objects2.5   clock, desktop, and dictionary objects
2.5 clock, desktop, and dictionary objects
 
2.3 exploring shapes
2.3   exploring shapes2.3   exploring shapes
2.3 exploring shapes
 
2.1 graphics window
2.1   graphics window2.1   graphics window
2.1 graphics window
 
Basic logo-command
Basic logo-commandBasic logo-command
Basic logo-command
 
2.2 turtle graphics
2.2   turtle graphics2.2   turtle graphics
2.2 turtle graphics
 
In-bai tap logo_01 (hsinh)
  In-bai tap logo_01 (hsinh)  In-bai tap logo_01 (hsinh)
In-bai tap logo_01 (hsinh)
 
Msw logo
Msw logoMsw logo
Msw logo
 
Msw logo application user manual for teacher
Msw logo application user manual for teacherMsw logo application user manual for teacher
Msw logo application user manual for teacher
 
In-bai tap logo_02 -thu tuc (hs)
  In-bai tap logo_02 -thu tuc (hs)  In-bai tap logo_02 -thu tuc (hs)
In-bai tap logo_02 -thu tuc (hs)
 
Logo primitives
Logo primitivesLogo primitives
Logo primitives
 
Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3
 
Geometria de las Tortugas
Geometria de las  TortugasGeometria de las  Tortugas
Geometria de las Tortugas
 
In-bai tap logo_02 -thu tuc (gv)
  In-bai tap logo_02 -thu tuc (gv)  In-bai tap logo_02 -thu tuc (gv)
In-bai tap logo_02 -thu tuc (gv)
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notes
 
Logo
LogoLogo
Logo
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 

Similar to 1.5 branching and subroutines

LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docxLabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
DIPESH30
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01
Getachew Ganfur
 
Introducing small basic
Introducing small basicIntroducing small basic
Introducing small basic
An I
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
olracoatalub
 
Introducing small basic
Introducing small basicIntroducing small basic
Introducing small basic
som_nangia
 

Similar to 1.5 branching and subroutines (20)

C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it security
 
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docxLabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
LabsLab8.htmlLab 8 Im Thinking of a NumberBefore yo.docx
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training report
 
Using Python inside Programming Without Coding Technology (PWCT) Environment
Using Python inside Programming Without Coding Technology (PWCT) EnvironmentUsing Python inside Programming Without Coding Technology (PWCT) Environment
Using Python inside Programming Without Coding Technology (PWCT) Environment
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
C++ for hackers
C++ for hackersC++ for hackers
C++ for hackers
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01
 
Introducing small basic
Introducing small basicIntroducing small basic
Introducing small basic
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2
 
A tutorial on C++ Programming
A tutorial on C++ ProgrammingA tutorial on C++ Programming
A tutorial on C++ Programming
 
Introducing small basic
Introducing small basicIntroducing small basic
Introducing small basic
 
ID E's features
ID E's featuresID E's features
ID E's features
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
Introducing Small Basic.pdf
Introducing Small Basic.pdfIntroducing Small Basic.pdf
Introducing Small Basic.pdf
 
ProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdfProgFund_Lecture_7_Intro_C_Sequence.pdf
ProgFund_Lecture_7_Intro_C_Sequence.pdf
 
Introducing small basic
Introducing small basicIntroducing small basic
Introducing small basic
 
My final requirement
My final requirementMy final requirement
My final requirement
 
Chapter 2- Prog101.ppt
Chapter 2- Prog101.pptChapter 2- Prog101.ppt
Chapter 2- Prog101.ppt
 
Application development with Python - Desktop application
Application development with Python - Desktop applicationApplication development with Python - Desktop application
Application development with Python - Desktop application
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 

More from allenbailey

4.4 advanced games
4.4   advanced games4.4   advanced games
4.4 advanced games
allenbailey
 
4.3 collision detection
4.3   collision detection4.3   collision detection
4.3 collision detection
allenbailey
 
4.2 responding to events
4.2   responding to events4.2   responding to events
4.2 responding to events
allenbailey
 
4.1 playing with shapes
4.1   playing with shapes4.1   playing with shapes
4.1 playing with shapes
allenbailey
 
3.5 the controls object
3.5   the controls object3.5   the controls object
3.5 the controls object
allenbailey
 
3.3 the math object
3.3   the math object3.3   the math object
3.3 the math object
allenbailey
 
3.2 stacks and arrays
3.2   stacks and arrays3.2   stacks and arrays
3.2 stacks and arrays
allenbailey
 

More from allenbailey (7)

4.4 advanced games
4.4   advanced games4.4   advanced games
4.4 advanced games
 
4.3 collision detection
4.3   collision detection4.3   collision detection
4.3 collision detection
 
4.2 responding to events
4.2   responding to events4.2   responding to events
4.2 responding to events
 
4.1 playing with shapes
4.1   playing with shapes4.1   playing with shapes
4.1 playing with shapes
 
3.5 the controls object
3.5   the controls object3.5   the controls object
3.5 the controls object
 
3.3 the math object
3.3   the math object3.3   the math object
3.3 the math object
 
3.2 stacks and arrays
3.2   stacks and arrays3.2   stacks and arrays
3.2 stacks and arrays
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 

1.5 branching and subroutines

  • 1. Microsoft® Small Basic Branches and Subroutines Estimated time to complete this lesson: 1 hour
  • 2. Code Branches and Subroutines In this lesson, you will learn how to: Create subroutines by using Sub and EndSub statements. Branch your code by using Goto statements.
  • 3. Branching You can instruct the computer to process a line of code out of sequence if you use the Gotostatement. Sometimes, you may want the computer to break the flow and jump to another line of code while the program is running. As you know, the computer runs a program by reading and processing the statements line by line, one at a time.
  • 4. Branching in Small Basic Programs Let’s examine the Goto statement and its various parts by writing a program. 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. 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. output
  • 5. Branching in Small Basic Programs You can also use the Goto statement to make a program run forever. Let’s 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.
  • 6. Subroutines in Small Basic Programs 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. 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.
  • 7. Subroutines in Small Basic Programs Let’s gain a better understanding of subroutines by writing another program… In this program, we use the Divide( ) statement torun (or “call”) the subroutine Divide from any location within the program. output
  • 8. Let’s Summarize… Congratulations! Now you know how to: Create a branch by using a Goto statement. Create a subroutine by using a Sub..EndSub statement.
  • 9.
  • 10. Uses branching and subroutines to determine and display the total number of:

Editor's Notes

  1. Code:j = 1lineI:TextWindow.WriteLine(j)j = j + 1If j < 30 ThenGotolineQEndIf
  2. 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
  3. 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