Programming in BASIC MS
Programming in BASIC MS
e computer language BASIC to develop three simple programs. They download a free application for the PC or Mac that installs in a few seconds. The students then progress through three challenges set at easy, medium and hard. The aim is to introduce students to computer programming through BASIC a simple, intuitive programming language. Learning Objectives: Students will be able to: Write a short computer program in BASIC Distinguish different features in program architecture Debug a simple computer program Explain that there are several programming languages Articulate the roles of software versus hardware in computing devices Academic Standards: National Science Education Standards (SCES) Science as Inquiry: CONTENT STANDARD A Abilities Necessary to Do Scientific Inquiry 1. Design and conduct a scientific investigation 2. Use appropriate tools and techniques to gather, analyze, and interpret data. 3. Develop description, explanation, prediction, and models using evidence. Science and Technology: CONTENT STANDARD E Understandings about science and technology Science in Personal and Social Perspectives: CONTENT STANDARD F Science and technology in local, national, and global challenges Time Frame: This lesson requires two 45-minute sessions to complete. During the first session, students are introduced to elements of computer programming and will write a simple routine. During the second session, students are challenged to write two routines.
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
Background for the Teacher: For most people, it is difficult to spend a day without using a computer or similar electronic device, such as a smart phone. Today, we take computers for granted. They are in our homes and offices. They run everything from our home entertainment and motorcars to the national grid and national defense. Its easy to forget about the underlying technology, which is one reason computers have become so widely adopted. The technology behind the keyboard and screen, and a browser or spreadsheet application is hidden beneath layers of computer programming. However, fewer and fewer people grasp the principles of programming. Yet, the demand for good programmers continues to increase. Students will benefit from having a basic grasp of the technology that drives our computing devices. Devices are run by a combination of hardware and software. By software we usually mean applications such as Microsoft Word, or a browser such as Firefox. But the essence of software is a series of instructions, or code, written in a computer language. The hardware uses these software routines to translate the instructions into output we can perceive and understand. This activity emphasizes to students that sophisticated technology drives computing devices but the principles are simple. Students will better understand the roles of software versus hardware in computing devices, as well become aware that programming languages have evolved to meet new technological needs and challenges. Students will learn that programs have a basic architecture and the simple rule of GIGO (garbage in, garbage out). Students will test their computer program and if necessary debug their programs. Vocabulary* Architecture the overall structure of the program including beginning, end and internal loops. BASIC one of the simplest programming languages, an acronym for Beginner's All-purpose Symbolic Instruction Code Command a program term that tells the computer to perform a specific task Computer program series of instructions, also called software, that manages computer hardware to provide specific output Conditional statement part of a programming language that performs alternative computations or actions depending on whether a condition is evaluated as true or false Debug to find errors in a malfunctioning computer program and correct them Factorial A mathematical function by which a number is a product of an integer and all the integers less than the value of that integer. Function an attribute or command that performs a transformation or other manipulation of a variable or string Loop a set of instructions that are repeated, usually subject to conditions Output The outcome of a program, producing data or information
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
Routine synonymous with program, often referring to parts of a program or subroutines, such as a sequence of instructions for performing a task that is accomplished by a program or a part of a program Statement a single line of code String a series of characters Syntax the structure or organization of computer code. All programming languages incorporate specific syntax. Incorrect syntax will cause a program to fail. Variable a value that may change within the program or according to user input
Classroom Activities:
Materials for the teacher: Computer with Internet access Materials for each group of students: Personal computer with BASIC software installed Engage 1. Ask students how they think a computer works. When they use an application such as Microsoft Word how does information they type treated to create what they see onscreen? 2. Few students understand the amount of programming behind the complex software we see on our desktop computers. They will be astonished to learn that millions of lines of code are needed just to run the operating system of their computer. For example, Microsoft Office uses 30 million lines of code, and Windows Vista requires 50 million lines of code. If each line of code was one line on a piece of 8 x 11 paper, with 50 lines per page, that would be a million pages, or a stack of paper about 315 feet high, ten feet more than the Statue of Liberty. So where does a program start? Tell students that a computer program is a series of instructions that tell the computer to perform specific tasks. A program is the software that tells the hardware what to do. 3. In this lesson the student will learn the simple principles of computer programming. Explore 1. Introduce students to the core concepts of programming: 2. A computer program is series of instructions, also called software, that manages computer hardware to provide specific output.
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
3. BASIC is one of the simplest programming languages, an acronym for Beginner's All-purpose Symbolic Instruction Code. Other programming languages provide different features. 4. A routine or simple program often refers to parts of a program. Depending on the complexity of the program, it may complete a calculation, execute a procedure or define variables. 5. Architecture refers to the overall structure of the program including conditional statements, beginning, end and internal loops. 6. Discuss with students how they might write a program. Explain to students they will be presented with three programming challenges: easy, medium and hard. INSTALLING THE BASIC SOFTWARE For PC computers: 1. Visit the JustBASIC website <http://www.justbasic.com/> to download the free application. 2. Click the link Download your copy now! The file size is 2.4 MB. 3. Install the program on the PC. (If necessary use a USB memory stick to transfer the program onto the students computers.) 4. To run the program, click on jbwin101.exe in the download file. Follow the on-screen instructions to install the program. (Ignore warning messages.) 5. Read the welcome screen to become familiar with the program features. Read the Help section and complete the Tutorial. 6. If using Windows 7 or Vista, follow the additional instructions: <http://justbasic.wikispaces.com/Installing_on_Vista> For Mac computers: 1. Visit the Chipmunk Basic website <http://www.nicholson.com/rhn/basic/>to download the free application. 2. Click the link Download the current version of Chipmunk Basic for Mac OS X v3.6.4(b8) 3. Find the file (called cbas364b8MacOSX.dmg.gz) in the Download folder. (For convenience, drag the file to the desktop. Click the file to unzip it. Click the volume icon (dmg file). In the window that opens, click the Chipmunk Basic icon. This launches the BASIC window. 4. Click the websites onscreen links for tutorials and manuals. 7. Present students with the first programming challenge. In this challenge, they have to write a routine that will print their name on the screen. 8. Ask students for suggestions on how to do that. Then have them complete Challenge 1.
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
CHALLENGE 1 INSTRUCTIONS 1. In the Just BASIC v1.01 window, click File in the top left of the menu bar. 2. Click New BASIC Source File 3. In the window type the highlighted phrase: PRINT Hello world! 4. A single line of code is a statement. Most programs include hundreds or more of such statements. 5. Indicate to students that the program word PRINT instructs the computer to put the phrase in brackets on the screen. Instructions such as PRINT are called commands, which tell the computer to perform a specific task. Ask how they think they can make the computer do that. i. They need to RUN the program to execute the PRINT command: ii. Click the blue Play triangle (in Just Basic). Alternatively, Run in the menu bar and select Run. (These are interchangeable for convenience.) iii. A new window will open with the words Hello world! in the window, and without instructions. (In Chipmunk Basic, the instructions are completed in the same window.) iv. This is the simplest computer program or routine. The phrase inside the quote marks is called a string, which can be any combination of characters that are output or input. 6. Now challenge students to write a program to write their name as a string so they print their name in the Run window. 7. The solution is for the students simply to replace Hello world! with their own name: PRINT STUDENT NAME 8. The popup box will display the string STUDENT NAME without the command. (Note for clarity we use uppercase to indicate code, but the BASIC applications are not case-sensitive.) Heres what it should look like:
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
9. To save a routine, click the disk icon. Alternatively, click File > Save As and type the file name. The file extension is *.bas (* is the name of the file). 10. The next part of Challenge 1 is to have the program print the student name multiple times. Again, ask students how to do that. The simplest way is simply to repeat the instructions in the routine, so to print the name three times, the student will type: PRINT STUDENT NAME PRINT STUDENT NAME PRINT STUDENT NAME 11. Is this efficient? If not, why not? So is there an easier way to do this? The second challenge is for students to write a program that will print a string a specified number of times.
CHALLENGE 2 INSTRUCTIONS 1. For Challenge 2, students will write a program that will print a string a specified number of times. The program instructs the computer to print the string a set number of times. 2. Like any language, computer languages must be learned in order to process information. 3. In this case, students will learn to create a loop that tells the computer to go back to execute a routine again. A loop is a set of instructions that are repeated, usually subject to conditions. The loop continues to be executed until instructed to stop. 4. For Challenge 2, introduce additional programming terms: FOR NEXT loop: specifies an instruction that is looped back to the beginning once it reaches the end, usually until a certain condition has been met. VAR: specifies a variable, a value that may change due to functions in the program or according to user input TO: specifies a range 5. Given this information, how might we construct a routine that will print out the string 10 times? One way to start is to imagine what someone would say in real words, breaking it down to its simplest components. For example, one could say Print the string once then, go back and do it again, for a total of ten times. It could do this twenty or thirty times. In this case, the variable is the number of times the PRINT command is executed. We want it to execute the command ten times, so the range is 1 to 10. The beginning of the loop is FOR and the end of the loop is NEXT. SO now we can say. FOR the VARiable from 1 to 10, PRINT Hello World and then execute the NEXT command. The lines of code are therefore: FOR VAR = 1 TO 10 PRINT HELLO WORLD NEXT
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
6. The popup box will display the string HELLO WORLD! ten times. 7. By design, BASIC reflects human language in its syntax, which is the structure of the statement. For example, reversing the commands or order or the lines would be incorrect syntax, and the program would not work. 8. Be sure to introduce good programming practice. The first good practice is to annotate code. Use REM (short for REMark) to make a note. The computer ignores anything on the same line as REM. Why is it important to include notes with code? Complex programs might include hundreds or more lines of code. Notes help keep track of which parts of the code perform which functions. Its easier to modify or debug well annotated code. Another good practice is to indent lines of code inside of loops to make the program easier to read. This is especially useful for BASIC applications that dont need numbered lines and with nested loops and numerous lines of code. Its a good idea to introduce best practices for programming even if specific items arent used in the lesson. 9. Numbering lines is also useful since the program ignores numbers at the beginning of lines. With numbers a loop can refer back to specific lines using the GOTO command, for example, GOTO 10 10. So well-written code for the above routine would be: REM print a string ten times 10 FOR VAR = 1 TO 10 20 PRINT HELLO WORLD 30 NEXT
11. Have the students experiment with changing the code. Try to have them predict what will happen. For example, what do they predict will happen if VAR = 1 TO 20? Note for Mac users: If using the Chipmunk program, program lines must be numbered. Type in the code immediately above. To run a program select Control>List from the menu bar. Then Select Control>Run. If the List command
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
produces unwanted code (since it lists all code typed) close the console window and start from the beginning. CHALLENGE 3 INSTRUCTIONS 1. Of course, computers can do so much more. In particular, as their name implies, computers are particular good at number-crunching. 2. For the next challenge, students will be asked to create a simple computational program. The program will ask for input, and then output results according to the input. 3. In this program, the computer will output the factorial of an entered number. For example, 3! = 1 x 2 x 3 = 6 4. After the factorial is output, the user is asked if they are finished. If not, the program will ask for input again. If so, the program prints a goodbye message. As with Challenge 2, the program uses a FOR-NEXT loop. It also uses three new functions: INPUT a command that requests a value, such as a number from the user IF-THEN loop a conditional function that executes commands only if conditions are met STEP a count function for a value, so that a function is counted in increments. 5. So using a common language approach, how can we structure the program? 6. First the program must get INPUT, which is a variable. The factorial is counted up from 1 a STEP at a time and then multiplied by each successive number. PRINT provides the result. To find out if the user want to continue, the program asks for new INPUT and assigns a new variable. This time the variable is a letter, so it is a string. Therefore we add a dollar sign ($) after the variable. Note that unlike numeric variables, text strings are always enclosed in double quotes. 7. We use the conditional loop to continue or end the program, so that IF the new variable equals Y, the program go back to the beginning. If it equals N, the program prints a goodbye message. 8. What if the user enters a lower case Y or N. Although BASIC commands and functions are not case-sensitive, strings are. Therefore, we need to introduce another function: OR a function that allows an alternative input or outcome Using OR we can specify a command whether the user inputs Y or N 9. Have students brainstorm ways to write the program. The solution: REM calculate the factorial of an input variable 10 INPUT "ENTER A NUMBER"; N 20 NFACTORIAL=1 30 FOR I=1 TO N STEP 1 40 NFACTORIAL=NFACTORIAL*I 50 NEXT I 60 PRINT N,NFACTORIAL
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
70 PRINT 80 INPUT DO YOU WANT TO CONTINUE (Y OR N?); ANS$ 90 IF ANS$ = Y OR ANS$ = y THEN GOTO 10 100 IF ANS$ = N OR ANS$ = n THEN PRINT "THANKS FOR PLAYING!" 110 END NOTE: ANS$ is simply a variable assigned to a string. Any appropriately declared variable will work such as RES$ (for response) or XYZ$. ANS$ is a shorthand. Using ANS makes it easier to remember the purpose of the variable.
10. Did the program work? If not, the students will have to debug to find errors and correct them. Since the programming language is intolerant of errors, any number of mistakes could cause a bug. Spelling errors in commands are common, or use of single instead of double quotes. Also variables must be declared before they are used. For example, if line 10 omitted N, line 20 would produce an error. Or if line 80 omitted ANS$, line 90 would produce an error. 11. Have students experiment with the program. What happens when they change the variables for instance? Explain 1. Basic is a programming language that offers an easy introduction to the world of programming. It allows the programmer to create simple routines with relative ease. For the hands on activity, the student will create a routine based on an easy, medium or hard challenge. 2. Compare BASIC with other computer languages. For example, to print Hello world! in C++ a programmer would have to write:
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
#include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; }
Compare these with the same function in BASIC. Ask students if they would prefer these languages to BASIC. If not why not? Other computer languages may be preferred according to their application. For example, Java may be applicable in more environments (such as robotic devices) than BASIC or C++. On the other hand C++ may be executed faster than either Java or BASIC, depending on the operating system. Programmers prioritize bug tolerance, device portability, user-friendliness and efficiency when choosing a program for a particular application or operating system. 3. Remember that computer languages are very precise. Just one mistake in a program could cause it not to work at all. Therefore simple programming languages are easier to use than more complex languages. Likewise, short routines are preferable to long ones. But inevitably, mistakes creep in. When a program doesnt work, the programmer must debug the program. A bug is a mistake that causes the program to work incorrectly or at all. Extend 1. Emphasize that there is not just one version of BASIC, but like any other language, it has evolved and changed, and there are several different versions, like dialects of a language. 2. Discuss the history of the language explaining that in the early days of home computing, users and hobbyists generally did their own programming, and that was mostly in BASIC. Try experimenting with programs called emulators, which mimic the behavior of BASIC in these early home computers. 3. BASIC is used in numerous educational and commercial applications. For example, BASIC is used to program microcontrollers, which run anything that has a remote control or digital display. 4. What other programming languages are there? Introduce students to programming languages from machine code to Fortran, Cobol and C++ to
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
modern descendants such as Perl, Java, PHP and SQL. Explain the difference between higher and lower level languages. BASIC is a high level language. 5. Encourage students to write more complex programs. For example, programs can be written to produce graphics or create games. 6. Challenge the students to debug programs by introducing deliberate errors into longer programs (e.g., removing quote marks from strings). Use a library of computer games written in BASIC to make this more interesting. Evaluate 1. Why is BASIC a useful computer language? 2. Write a short computer program that will multiply two numbers provided by the user. Scoring key for evaluation 1. BASIC is a useful computer language because it uses everyday language with logical syntax that can be readily understood by non-experts. 2. There are several possible solutions. A simple one is: 10 REM MULTIPLY TWO NUMBERS 20 INPUT "ENTER THE FIRST NUMBER"; X 30 INPUT "ENTER THE SECOND NUMBER"; Y 40 Z=X*Y 50 PRINT X, "TIMES", Y, "EQUALS", Z Web resources Chipmunk Basic for Mac OS http://www.nicholson.com/rhn/basic/ How Microcontrollers Work http://electronics.howstuffworks.com/microcontroller3.htm What is C programming? http://curiosity.discovery.com/question/c-programming How is a variable used in computer programming? http://curiosity.discovery.com/question/variable-used-in-computer-programming What is computer science? http://curiosity.discovery.com/question/what-is-computer-science Further activities TRS 80 BASIC Emulator http://www.vavasour.ca/jeff/level1/simulator.html 101 BASIC Computer Games http://www.atariarchives.org/basicgames/
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
Architecture the overall structure of the program including beginning, end and internal loops. BASIC one of the simplest programming languages, an acronym for Beginner's All-purpose Symbolic Instruction Code Command a program term that tells the computer to perform a task Computer program series of instructions, also called software, that manages computer hardware to provide specific output Conditional statement part of a programming language that performs alternative computations or actions depending on whether a condition is evaluated as true or false Debug to find errors in a malfunctioning computer program and correct them Factorial A mathematical function by which a number is a product of an integer and all the integers less than the value of that integer. Function an attribute or command that performs a transformation or other manipulation of a variable or string Loop a set of instructions that are repeated, usually subject to conditions Output The outcome of a program, producing data or information Routine synonymous with program, often referring to parts of a program or subroutines, such as a sequence of instructions for performing a task that is accomplished by a program or a part of a program Statement a single line of code String a series of characters Syntax the structure or organization of computer code. All programming languages incorporate specific syntax. Incorrect syntax will cause a program to fail. Variable a value that may change within the program or according to user input
What is BASIC? BASIC is an acronym for Beginner's All-purpose Symbolic Instruction Code. It is a high-level programming language invented in the 1960s. It allows users to use everyday language in logical steps to program a computer. Before BASIC, computer programs needed complex detailed instructions with some knowledge of how the hardware worked. What are the key features of a programming language? A computer language such as BASIC includes three main features: Commands words that carry out a specific operation, usually on strings or variables Functions operations that transform a string or variable, such as a mathematical function
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.
Routines or subroutines sets of self-contained parts of the program that execute commands and functions Many programs also use: Loops a repeated operation initiated with a command Variables a value that can be changed by user input or a function Strings a series or set of characters that may be provided by the user or manipulated by commands or functions Conditionals commands that are executed only if specified conditions are met
What are the key features of a computer program? All computer programs share common features including: Architecture the overall structure of a program Syntax the specific order of commands, functions and statements Statements Lines of code that comprise the program Who uses BASIC? BASIC is used in numerous educational and commercial applications, such as Visual Basic for Applications (VBA) which is used for Microsoft Office. An IBM operating system uses a version called CBASIC in point of sale systems (such as card swipe machines). Beginner computer programmers use it because it is easy to learn. Why is BASIC important? Computer programs run the computing applications that we encounter everyday word processors, spreadsheets, browsers and computer games, for example. Some programming languages are highly complex, but BASIC is the simplest. Understanding BASIC helps people learn how more complex computer languages work. BASIC still has practical use in a number of commercial applications. Web resources Chipmunk Basic for Mac OS http://www.nicholson.com/rhn/basic/ How Microcontrollers Work http://electronics.howstuffworks.com/microcontroller3.htm What is C programming? http://curiosity.discovery.com/question/c-programming How is a variable used in computer programming? http://curiosity.discovery.com/question/variable-used-in-computer-programming What is computer science? http://curiosity.discovery.com/question/what-is-computer-science
Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of Discovery Communications, LLC.