Microsoft® Small Basic

Slides:



Advertisements
Similar presentations
What Was I Thinking??. Key Terms 1. Control 1. Control 2. Design Mode 2. Design Mode 3. Event 3. Event 4. Form 4. Form 5. Interface 5. Interface 6. Properties.
Advertisements

Microsoft® Small Basic
Microsoft® Small Basic
Microsoft® Small Basic Statements, Properties, and Operations Estimated time to complete this lesson: 1 hour.
Microsoft® Small Basic
Introduction to Visual Basic.NET Uploaded By: M.Sheraz anjum.
CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1 based on lecture notes by D. Schneider.
Microsoft® Small Basic
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
Microsoft® Small Basic
Microsoft® Small Basic Debugging Aids Estimated time to complete this lesson: 1 hour.
Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
The Weather It´s sunny It´s cloudy It´s rainy It´s windy It´s stormy
Word Lesson 16 Working with Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
Create Forms Lesson 5. Software Orientation Creating Forms A form is a database object –enter, edit, or display data from a table or query Providing.
Microsoft® Small Basic The Controls Object Estimated time to complete this lesson: 1 hour.
CHAPTER 6 Loop Structures.
CHAPTER SIX.
Microsoft® Small Basic
An Introduction to Textual Programming
1 Workshop 4 (B): Visual Basic Test Project Mahidol University June 13, 2008 Paul Evenson University of Delaware Bartol Research Institute.
Bug Session Four. Session description Objectives Session activities summary Resources Prior knowledge of sequencing instructions using Bug Bug website.
Step 1: Launch Audacity from your computer by clicking on the START menu, and then clicking on Audacity*. *If you cannot locate it in your list of programs,
Pasewark & Pasewark 1 Access Lesson 5 Creating and Modifying Reports Microsoft Office 2007: Introductory.
Database Systems Microsoft Access Practical #3 Queries Nos 215.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Chapter Two Creating a First Project in Visual Basic.
Microsoft® Small Basic Collision Detection Estimated time to complete this lesson: 1 hour.
Workflows II: Collect feedback for a file How to collect feedback Collecting feedback for a file can be challenging. However, if you save a file to a document.
Standard Grade Programming using VB 1 Programming Visual Basic.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Report Writer – Comment Bank is an add in for Microsoft Word. Installation is straightforward, just follow the on screen instructions. The PC does not.
Pasewark & Pasewark 1 Access Lesson 5 Creating and Modifying Reports Microsoft Office 2007: Introductory.
Select (drop-down list) Inputs. Insert/Form/List Menu.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Seasons.
Programming and Languages Dept. of Computer and Information Science IUPUI.
Microsoft® Small Basic Exploring Shapes Estimated time to complete this lesson: 1 hour.
CRE Programming Club - Class 5 Robert Eckstein and Robert Heard.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Debug in Visual Studio Windows Development Fundamentals LESSON 2.5A.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Introduction to TouchDevelop Lesson 3 – Comments & Lists Created by S. Johnson
Microsoft® Small Basic Flickr, ImageList, and Network Objects Estimated time to complete this lesson: 1 hour.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
21/03/ Working with Controls Text and List Boxes.
Programming with Visual Basic.NET. Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons.
Visual Basic.NET BASICS Lesson 9 Nested If Statements and Radio Buttons.
Chapter 6: Loops.
Scratch: iteration / repetition / loops
Lesson 22. My Favourite Season!
Microsoft® Small Basic
Starter Write a program that asks the user if it is raining today.
FLOW OF CONTROL.
elppa legoog erawdrah erawtfos margorp Cisab llams Apple Google
Items, Group Boxes, Check Boxes & Radio Buttons
Tonga Institute of Higher Education
Introduction to TouchDevelop
Come Rain or Shine.
CSCI N207 Data Analysis Using Spreadsheet
Welcome! Advanced Dynamic Documentation Advanced Dynamic Documentation
Welcome! Advanced Dynamic Documentation Advanced Dynamic Documentation
Welcome! Advanced Dynamic Documentation Advanced Dynamic Documentation
Welcome! Advanced Dynamic Documentation Advanced Dynamic Documentation
Welcome! Advanced Dynamic Documentation Advanced Dynamic Documentation
Presentation transcript:

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: Branch your code by using Goto statements. Create subroutines by using Sub and EndSub statements.

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. Code: j = 1 lineQ: TextWindow.WriteLine(j) j = j + 1 If j < 30 Then Goto lineQ EndIf You can instruct the computer to process a line of code out of sequence if you use the Goto statement.

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 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 = 1 lineQ: TextWindow.WriteLine(j) j = j + 1 If j < 10 Then Goto lineQ EndIf

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. Warning: If you use Goto statements a lot, your code will be difficult to understand and to correct. Although these statements are useful sometimes, you should try to structure your programs so that you rarely use Goto statements. Code: start: TextWindow.WriteLine("How many members are in your family?") number = TextWindow.ReadNumber() remainder = Math.Remainder(number, 2) If remainder = 0 Then TextWindow.WriteLine("Your family has an even number of members.") Else TextWindow.WriteLine("Your family has an odd number of members.") EndIf Goto start

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 to run (or “call”) the subroutine Divide from any location within the program. If you use subroutines, your programs will be easier to read and understand than if you use Goto statements. In this program, you write the Divide subroutine once, but you can run it from anywhere in the program. When you instruct the computer to run a subroutine, you use a statement that contains the name of the subroutine followed by a set of open and close parentheses. When you use this type of statement, you are calling the subroutine. Code: While i < 5 TextWindow.WriteLine("Enter Dividend: ") Dividend = TextWindow.Read() TextWindow.WriteLine("Enter Divisor: ") Divisor = TextWindow.Read() Divide() TextWindow.WriteLine("Your answer is: " + Answer) i = i + 1 EndWhile Sub Divide Answer = Dividend / Divisor EndSub 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: Asks the user for the name, temperature, rain status, and wind status of 10 cities. Uses branching and subroutines to determine and display the total number of: Cold Cities Cool Cities Warm Cities Hot Cities Rainy Cities Windy Cities Solution: up: If i < 10 then TextWindow.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 subroutines subtempCount() subrainyCount() subwindyCount() i = i + 1 If i = 10 Then subOutput() EndIf   Goto up Sub subtempCount If temp <= 5 Then ColdCount = ColdCount + 1 ElseIf temp <= 15 Then CoolCount = CoolCount + 1 ElseIf temp <= 25 Then WarmCount = WarmCount + 1 Else HotCount = HotCount + 1 EndSub Sub subRainyCount If Rainy = "y" Or Rainy = "Y" Then RainyCount = RainyCount + 1