lesson 4 variables and constant

28

Upload: christopher-olaya

Post on 07-Aug-2015

159 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Lesson 4   variables and constant
Page 2: Lesson 4   variables and constant

“Programming must look DEEPER to find out all possible

solutions to a problem.”

Page 3: Lesson 4   variables and constant
Page 4: Lesson 4   variables and constant

Sample program

clslet name$=“Piolo Pascual ”let school$=“Pililla National High School”print “Hello World!!!”print “I am “;name$print “I am enrolled at “;school$print “Thank your for Reading”end

Page 5: Lesson 4   variables and constant

Program Output

Hello World!!!I am Piolo PascualI am enrolled at Pililla National High SchoolThank you for reading.

Page 6: Lesson 4   variables and constant

Variables May be simply defined as a name

or label that may contain a value. When variables are used in a

computer program, Basic will store your variables in different locations on the hard drive. With this reason, the programmer must tell the computer what type of variable is going to be stored in a particular location on the hardisk.

Page 7: Lesson 4   variables and constant

Examples of Variables

Name=“kevin”X = y + z

Page 8: Lesson 4   variables and constant

Two Types of Variables1. String Variable - contain a word,

letter or a group of numbers that act as labesl or simple text within the program.(ex. Name$=kevin)

2. Numeric Variable – on the other hand are used to store positive or negative numbers. These numbers can be used for computation later on the program.

Page 9: Lesson 4   variables and constant

Symbol Classification Range Precision

$ String Variable A variable declared as a string can contain a maximum of 255

characters.

% Short Integer Variable types of this kind must only contain whole

numbers. Its range is from -32,768 to 32, 768.

& Long Integer Variables of this types has a range of -2,147,483,648 to

2,147,483,647

! Single Precision Variable of this type can contain up to a maximum of 7

decimal places. Its range is from -3.402823E+38 to

+ 3.402823E+38

# Double Precision Have a maximum of 14 decimal places from

1.79769313486321D+308

Page 10: Lesson 4   variables and constant

Rules in giving a Variable Name The first character of a variable

name must be a letter. Variable names cannot contain an

underscore(_), use the decimal point instead to separate word.

Variable names cannot start with the letter “FN” as Basic will assume that these names are functions.

Page 11: Lesson 4   variables and constant

Rules in giving a Variable Name Variable names may not be the same

as any of the words Basic uses to describe operations and functions. These names/words are sometimes called reserved words or keywords like PRINT, LET, DATA, READ, SQR.

Page 12: Lesson 4   variables and constant

TIPS IN GIVING VARIABLE NAME It would be best if the variable name

is descriptive and meaningful of the data it will stand for.

example= mathgrade, averagegrade,

This will enhance the clarity of the program for others who would like to read the program and it makes it easier to remember which variable is storing what value.

Page 13: Lesson 4   variables and constant

Sample Program:clsREM This program will compute for the area of the circlelet r=2.0let pi=3.1416print “Compute the area of a circle.”print “The Radius is equal to: “;rprint “Pi is equal to: “;piarea=pi*r*rprint “The area of the circle is equal to: “;areaprint “End of the Program”

Page 14: Lesson 4   variables and constant

Output Program:

Compute the area of a circle.The Radius is equal to: 1Pi is equal to: 3.1416The area of the circle is equal to: 3.1416End of the Program

Page 15: Lesson 4   variables and constant

Constant Are values that do not change

while the program is running. They are very effective when, for

example, you have a number that you want to use over and over again, you can simply use the constant name instead of typing the numbers every time you need it

Page 16: Lesson 4   variables and constant

Example

Dollar=45.50Pi=3.1416Name=“Piolo”School=“PilillaNHS”

Page 17: Lesson 4   variables and constant

Mathematical Operators Exponentiation- use the caret (^)

symbol to work with numbers and their exponents.

Multiplication-uses Asterisk (*) Division- uses forward slash(/) Integer Division- uses the back

slash (\) to get the whole number answer for a division operation.

Page 18: Lesson 4   variables and constant

Mathematical Operators Modulo Arithmetic – Uses MOD

operator to supply the remainder after any division operation.

Addition – Uses cross (+) Subtraction – Uses the dash (-)

Page 19: Lesson 4   variables and constant

Example

Exponentiation: 2^4 = 16 Multiplication: 2*4 = 8 Division: 4/2 = 2 Integer Division: 10\3 = 3 Modulo Arithmetic: 10 MOD 3 =

1 Addition: 5+41 = 46 Subtraction: 25-16 = 9

Page 20: Lesson 4   variables and constant

CLS Command

CLS – simply means Clear Screen. This command will remove all other contents of the execution window so that the program that your will activate/use/run has a clear screen to use.

Page 21: Lesson 4   variables and constant

Print CommandPRINT – The print command tells

the computer to make an output on the screen. The syntax or the way the command is used is:

Print [variable name] and/or[string constants]

Page 22: Lesson 4   variables and constant

Print CommandExample:print name$print “Hello”print “The average is :”;average

Page 23: Lesson 4   variables and constant

Let CommandThe Let statement or command is

what we usually call an assignment statement. It is used to define the content of variable and constants. It is used in much the same manner as when it is used in math when we write the variable being used in word problem.

Page 24: Lesson 4   variables and constant

Let CommandLike for example: Let x = y +

zThe syntax for LET is:LET [variable name] =

[variable value]

Page 25: Lesson 4   variables and constant

INPUT COMMAND

Used to ask for data from the user. The syntax for input command is: Input [string constant] or [string

message]; variable name

The variable name is important since this command is an assignment command that takes a variable’s value from the user’s input.

Page 26: Lesson 4   variables and constant

Sample program code:

clsrem This program will input and print out your

name and age.name$=“ ”age=0input “Please type in your name: “;name$input “Please enter your age: “;ageprint “Hello “;name$; “Welcome to Basic

Programming”print “I see that you are “;age; “years old.”end

Page 27: Lesson 4   variables and constant

Activity

Problem: Create a program that will input

three numbers and will compute for the average of the three numbers. Print out the Average number.

Page 28: Lesson 4   variables and constant

Hands on Activity