Programming in BASIC MS

Embed Size (px)

Citation preview

  • 7/31/2019 Programming in BASIC MS

    1/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    Title: Programming in BASIC

    Subject: Science

    Grades: 6 8

    Category: Computers

    Lesson Overview:

    In this activity, students use the computer language BASIC to develop three simpleprograms. They download a free application for the PC or Mac that installs in a fewseconds. The students then progress through three challenges set at easy, mediumand hard. The aim is to introduce students to computer programming throughBASIC 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 Inquiry1. Design and conduct a scientific investigation2. 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 asimple routine. During the second session, students are challenged to write tworoutines.

  • 7/31/2019 Programming in BASIC MS

    2/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    Background for the Teacher:

    For most people, it is difficult to spend a day without using a computer or similarelectronic 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 toforget about the underlying technology, which is one reason computers havebecome so widely adopted. The technology behind the keyboard and screen, and abrowser or spreadsheet application is hidden beneath layers of computerprogramming. However, fewer and fewer people grasp the principles ofprogramming. Yet, the demand for good programmers continues to increase.Students will benefit from having a basic grasp of the technology that drives ourcomputing devices. Devices are run by a combination of hardware and software. Bysoftware we usually mean applications such as Microsoft Word, or a browser suchas Firefox. But the essence of software is a series of instructions, or code, written ina computer language. The hardware uses these software routines to translate the

    instructions into output we can perceive and understand. This activity emphasizesto students that sophisticated technology drives computing devices but theprinciples are simple. Students will better understand the roles of software versushardware in computing devices, as well become aware that programming languageshave evolved to meet new technological needs and challenges. Students will learnthat programs have a basic architecture and the simple rule of GIGO (garbage in,garbage out). Students will test their computer program and if necessary debugtheir programs.

    Vocabulary*

    Architecture the overall structure of the program including beginning, endand internal loops.

    BASIC one of the simplest programming languages, an acronym forBeginner's All-purpose Symbolic Instruction Code

    Command a program term that tells the computer to perform a specifictask

    Computer program series of instructions, also called software, thatmanages computer hardware to provide specific output

    Conditional statement part of a programming language that performsalternative computations or actions depending on whether a condition isevaluated as true or false

    Debug to find errors in a malfunctioning computer program and correctthem

    Factorial A mathematical function by which a number is a product of aninteger and all the integers less than the value of that integer.

    Function an attribute or command that performs a transformation or othermanipulation 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

  • 7/31/2019 Programming in BASIC MS

    3/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    Routine synonymous with program, often referring to parts of a programor subroutines, such as a sequence of instructions for performing a task thatis 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 programminglanguages 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

    *First occurrences of vocabulary items are highlighted in the text.

    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 anapplication such as Microsoft Word how does information they type treatedto create what they see onscreen?

    2. Few students understand the amount of programming behind the complexsoftware we see on our desktop computers. They will be astonished to learnthat millions of lines of code are needed just to run the operating system oftheir 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 wasone line on a piece of 8 x 11 paper, with 50 lines per page, that would be amillion pages, or a stack of paper about 315 feet high, ten feet more than theStatue of Liberty. So where does a program start? Tell students that acomputer program is a series of instructions that tell the computer toperform specific tasks. A program is the software that tells the hardwarewhat to do.

    3.

    In this lesson the student will learn the simple principles of computerprogramming.

    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.

  • 7/31/2019 Programming in BASIC MS

    4/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    3. BASIC is one of the simplest programming languages, an acronym forBeginner's All-purpose Symbolic Instruction Code. Other programminglanguages provide different features.

    4. A routine or simple program often refers to parts of a program. Dependingon 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, mediumand hard.

    INSTALLING THE BASIC SOFTWARE

    For PC computers:

    1. Visit the JustBASIC website todownload 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 totransfer the program onto the students computers.)

    4. To run the program, click onjbwin101.exe in the download file. Followthe on-screen instructions to install the program. (Ignore warningmessages.)

    5. Read the welcome screen to become familiar with the programfeatures. Read the Help section and complete the Tutorial.

    6. If using Windows 7 or Vista, follow the additional instructions:

    For Mac computers:1. Visit the Chipmunk Basic website

    to download the freeapplication.

    2. Click the link Download the current version of Chipmunk Basic forMac 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 theChipmunk 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.

  • 7/31/2019 Programming in BASIC MS

    5/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    CHALLENGE 1 INSTRUCTIONS1. In the Just BASIC v1.01 window, click File in the top left of the menu bar.2. Click New BASIC Source File3. 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 arecalled commands, which tell the computer to perform a specific task. Askhow 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 forconvenience.)

    iii. A new window will open with the words Hello world! in the window,and without instructions. (In Chipmunk Basic, the instructions arecompleted in the same window.)

    iv. This is the simplest computer program or routine. The phrase insidethe quote marks is called a string, which can be any combination ofcharacters that are output or input.

    6. Now challenge students to write a program to write their name as a string sothey print their name in the Run window.

    7. The solution is for the students simply to replace Hello world! with theirown name:

    PRINT STUDENT NAME

    8. The popup box will display the string STUDENT NAME without thecommand. (Note for clarity we use uppercase to indicate code, but the BASICapplications are not case-sensitive.) Heres what it should look like:

  • 7/31/2019 Programming in BASIC MS

    6/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    9. To save a routine, click the disk icon. Alternatively, click File > Save As andtype 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 namemultiple times. Again, ask students how to do that. The simplest way issimply to repeat the instructions in the routine, so to print the name three

    times, the student will type:PRINT STUDENT NAMEPRINT STUDENT NAMEPRINT STUDENT NAME

    11.Is this efficient? If not, why not? So is there an easier way to do this? Thesecond challenge is for students to write a program that will print a string aspecified number of times.

    CHALLENGE 2 INSTRUCTIONS

    1. For Challenge 2, students will write a program that will print a string aspecified number of times. The program instructs the computer to print thestring a set number of times.

    2. Like any language, computer languages must be learned in order to processinformation.

    3. In this case, students will learn to create a loop that tells the computer to goback to execute a routine again. A loop is a set of instructions that arerepeated, usually subject to conditions. The loop continues to be executeduntil instructed to stop.

    4. For Challenge 2, introduce additional programming terms:FOR NEXTloop: specifies an instruction that is looped back to the

    beginning once it reaches the end, usually until a certain condition hasbeen met.VAR: specifies a variable, a value that may change due to functions inthe program or according to user inputTO: specifies a range

    5. Given this information, how might we construct a routine that will print outthe string 10 times? One way to start is to imagine what someone would sayin real words, breaking it down to its simplest components. For example, onecould say Print the string once then, go back and do it again, for a total of tentimes. It could do this twenty or thirty times. In this case, the variable is thenumber 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 FORand the end of the loop is NEXT. SO now we can say. FOR the VARiable from1 to 10, PRINT Hello World and then execute the NEXT command. Thelines of code are therefore:

    FOR VAR = 1 TO 10PRINT HELLO WORLDNEXT

  • 7/31/2019 Programming in BASIC MS

    7/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, 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 orthe lines would be incorrect syntax, and the program would not work.

    8. Be sure to introduce good programming practice. The first good practice is toannotate code. Use REM (short for REMark) to make a note. The computerignores anything on the same line as REM. Why is it important to includenotes with code? Complex programs might include hundreds or more lines ofcode. Notes help keep track of which parts of the code perform whichfunctions. Its easier to modify or debug well annotated code. Another goodpractice is to indent lines of code inside of loops to make the program easierto read. This is especially useful for BASIC applications that dont neednumbered lines and with nested loops and numerous lines of code. Its a goodidea to introduce best practices for programming even if specific items arentused in the lesson.

    9. Numbering lines is also useful since the program ignores numbers at thebeginning of lines. With numbers a loop can refer back to specific lines usingthe GOTO command, for example,

    GOTO 1010.So well-written code for the above routine would be:

    REM print a string ten times10 FOR VAR = 1 TO 1020 PRINT HELLO WORLD30 NEXT

    11.Have the students experiment with changing the code. Try to have thempredict what will happen. For example, what do they predict will happen ifVAR = 1 TO 20?

    Note for Mac users: If using the Chipmunk program, program lines must benumbered. Type in the code immediately above. To run a program selectControl>List from the menu bar. Then Select Control>Run. If the List command

  • 7/31/2019 Programming in BASIC MS

    8/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    produces unwanted code (since it lists all code typed) close the console window andstart from the beginning.

    CHALLENGE 3 INSTRUCTIONS1. 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 outputresults 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, theprogram 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 threenew functions:

    INPUT a command that requests a value, such as a number from the

    userIF-THENloop a conditional function that executes commands only ifconditions are metSTEP a count function for a value, so that a function is counted inincrements.

    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 successivenumber. 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 thevariable 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 alwaysenclosed in double quotes.

    7. We use the conditional loop to continue or end the program, so that IF thenew 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 andfunctions are not case-sensitive, strings are. Therefore, we need to introduceanother function:

    OR a function that allows an alternative input or outcomeUsing 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 variable10 INPUT "ENTER A NUMBER"; N20 NFACTORIAL=130 FOR I=1 TO N STEP 140 NFACTORIAL=NFACTORIAL*I50 NEXT I60 PRINT N,NFACTORIAL

  • 7/31/2019 Programming in BASIC MS

    9/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    70 PRINT80 INPUT DO YOU WANT TO CONTINUE (Y OR N?); ANS$90 IF ANS$ = Y OR ANS$ = y THEN GOTO 10100 IF ANS$ = N OR ANS$ = n THEN PRINT "THANKS FORPLAYING!"

    110 END

    NOTE: ANS$ is simply a variable assigned to a string. Anyappropriately declared variable will work such as RES$ (forresponse) or XYZ$. ANS$ is a shorthand. Using ANS makes it easierto remember the purpose of the variable.

    10.Did the program work? If not, the students will have to debug to find errorsand 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 bedeclared before they are used. For example, if line 10 omitted N, line 20would produce an error. Or if line 80 omitted ANS$, line 90 would producean error.

    11.Have students experiment with the program. What happens when theychange the variables for instance?

    Explain

    1. Basic is a programming language that offers an easy introduction to theworld of programming. It allows the programmer to create simple routineswith relative ease. For the hands on activity, the student will create a routinebased on an easy, medium or hard challenge.

    2. Compare BASIC with other computer languages. For example, to print Helloworld! in C++ a programmer would have to write:

  • 7/31/2019 Programming in BASIC MS

    10/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    #include

    int main()

    {

    std::cout

  • 7/31/2019 Programming in BASIC MS

    11/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    modern descendants such as Perl, Java, PHP and SQL. Explain the differencebetween 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 errorsinto longer programs (e.g., removing quote marks from strings). Use a libraryof 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 withlogical syntax that can be readily understood by non-experts.

    2. There are several possible solutions. A simple one is:10 REM MULTIPLY TWO NUMBERS20 INPUT "ENTER THE FIRST NUMBER"; X30 INPUT "ENTER THE SECOND NUMBER"; Y40 Z=X*Y50 PRINT X, "TIMES", Y, "EQUALS", Z

    Web resources

    Chipmunk Basic for Mac OShttp://www.nicholson.com/rhn/basic/

    How Microcontrollers Workhttp://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 Emulatorhttp://www.vavasour.ca/jeff/level1/simulator.html

    101 BASIC Computer Gameshttp://www.atariarchives.org/basicgames/

  • 7/31/2019 Programming in BASIC MS

    12/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    STUDENT TAKEAWAY Programming in BASIC

    Architecture the overall structure of the program including beginning, endand internal loops.

    BASIC one of the simplest programming languages, an acronym forBeginner'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 isevaluated as true or false

    Debug to find errors in a malfunctioning computer program and correctthem

    Factorial A mathematical function by which a number is a product of aninteger and all the integers less than the value of that integer.

    Function an attribute or command that performs a transformation or othermanipulation 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 thatis 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 programto fail.

    Variable a value that may change within the program or according to userinput

    What is BASIC?

    BASIC is an acronym for Beginner's All-purpose Symbolic Instruction Code. Itis a high-level programming language invented in the 1960s. It allows usersto use everyday language in logical steps to program a computer. BeforeBASIC, computer programs needed complex detailed instructions with someknowledge 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

  • 7/31/2019 Programming in BASIC MS

    13/13

    Copyright 2011 Discovery Education. All rights reserved. Discovery Education is a subsidiary of DiscoveryCommunications, LLC.

    Routines or subroutines sets of self-contained parts of the programthat 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 useror 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 asVisual Basic for Applications (VBA) which is used for Microsoft Office. An IBMoperating system uses a version called CBASIC in point of sale systems (suchas card swipe machines). Beginner computer programmers use it because itis easy to learn.

    Why is BASIC important?

    Computer programs run the computing applications that we encountereveryday word processors, spreadsheets, browsers and computer games,for example. Some programming languages are highly complex, but BASIC isthe simplest. Understanding BASIC helps people learn how more complex

    computer languages work. BASIC still has practical use in a number ofcommercial applications.

    Web resources

    Chipmunk Basic for Mac OShttp://www.nicholson.com/rhn/basic/

    How Microcontrollers Workhttp://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