Variables and DeBugging

13
VARIABLES AND DEBUGGING Beginning Programming

description

Variables and DeBugging. Beginning Programming. Assignment Statements. Used to hold values in a variable Calculates a result and stores it in a variable Variable: used to hold values in a program Should describe what it holds Starts with a letter Cannot use spaces - PowerPoint PPT Presentation

Transcript of Variables and DeBugging

Page 1: Variables and  DeBugging

VARIABLES AND DEBUGGING

Beginning Programming

Page 2: Variables and  DeBugging

Assignment Statements

Used to hold values in a variable Calculates a result and stores it in a variable

Variable: used to hold values in a program Should describe what it holds Starts with a letter Cannot use spaces Can only use _ , $ , and . Cannot use reserved words (such as RUN, NEW,

PRINT, etc.) Should end a string (a sequence of characters

enclosed in quotation marks) with $

Page 3: Variables and  DeBugging

Variable Naming

FRUIT$ POOL.LENGTH LAST_NAME$ BIRTHDATE$ AGE

1FRUIT$ F$ FRUIT TYPE$ FRUIT*$ NEWFRUIT$ FRUIT

Good Naming Examples Poor Naming Examples

Page 4: Variables and  DeBugging

Practice

Complete the Creating Descriptive Variable names worksheet

Complete the Storage Location Worksheet

Page 5: Variables and  DeBugging

Commenting Variables in REM Statements

You need to list every variable that you use in the program in your REM Statements

You will put there in the Variable Declaration section of the REM Template

You should list the variable name and then describe what the variable stands for

Example: REM POOL.LENGTH holds the length of the pool

in feet REM TEACHER_NAME$ holds the teachers first

and last name

Page 6: Variables and  DeBugging

Assignment

Complete REM and Variable Practice Worksheet

Page 7: Variables and  DeBugging

Using Variables in the Main Program

To set the value of a variable, use the command LET Examples:

LET Stage.Length = 5 LET Subject_Name$ = “Algebra”

You can also set the value of the variable without the word LET Examples:

Stage.Length = 5 Subject_Name$ = “Algebra”

Page 8: Variables and  DeBugging

Using Variables in Processes

You will want to use variable names when solving equations Example #1:

LET Length = 5 LET Width = 4 LET Height = 3 LET Volume = Length * Width * Height PRINT “The volume is “; Volume; “.”

Page 9: Variables and  DeBugging

Using Variables in Processes

You will want to use variable names when solving equations Example #2:

LET First.Name$ = “Bob” LET Last.Name$ = “Smith” Customer.Name$ = First.Name$ + “ “ +

Last.Name PRINT “The customer’s name is “;

Customer.Name$

Page 10: Variables and  DeBugging

Some important facts

Try to use variables whenever you can and have the computer solve mathematical equations for you, rather than solving them yourself. If you solve them yourself and just enter the answer, you will have to do it all over again if you want to run the program with different input numbers.

In PRINT statements, remember to separate the parts in quotations with the variables not in quotations with a ;

Page 11: Variables and  DeBugging

Assignment

Program #4A

Page 12: Variables and  DeBugging

Debugging

Debugging: Figuring out the errors in a program There are 2 types:

Syntax Errors Most common error Programmer violates the grammatical rules of

Qbasic Error message will be displayed at bottom of screen

Logical Errors Program may stop executing prematurely Program may execute completely, but produce

incorrect results Mathematical errors Formatting errors

Page 13: Variables and  DeBugging

Debugging Practice

1. Debugging exercise #1 – complete and we will go over as a class

2. Debugging exercise #2 – complete and turn in for a grade