variable declaration

15
General Building Blocks & their use in BASIC Building blocks in general are common to most of the languages including C The objective is to introduce basic programming concepts such as program structure, variable declaration, conditional and looping constructs, and the code/compile or interpret/run style of programming This acts as an introduction to these concepts for students who have no prior programming experience.

Upload: wyanet

Post on 06-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

General Building Blocks & their use in BASIC Building blocks in general are common to most of the languages including C. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Variable Declaration

General Building Blocks & their use in BASIC

Building blocks in general are common to most of the languages including C

The objective is to introduce basic programming concepts such as program structure, variable declaration, conditional and looping constructs, and the code/compile or interpret/run style of programmingThis acts as an introduction to these concepts for students who have no prior programming experience.

Page 2: Variable Declaration

Variable Declaration Variables are place holders for data a program might use or

manipulate. Variables are given names so that we can assign values to them and refer to them later to read the values. Variables typically store values of a given type. Types generally include:

Integer - to store integer or "whole" numbers Real - to store real or fractional numbers (also called float to

indicate a floating point number) Character - A single character such as a letter of the alphabet or

punctuation. String - A collection of characters In order to use a variable within a program, the compiler needs

to know in advance the type of data that will be stored in it. For this reason, we declare the variables at the start of the program. Variable declaration consists of giving a new name and a data type for the variable. This is normally done at the very start of the program.

Page 3: Variable Declaration

ARITHMETIC OPERATORS These allow variables to be combined or assigned values. Assignment = Means let the variable be equal to. In assignment statements, the value on the right side of the = sign is copied into the variable on the left

side, thus the statement 10 LET X = 4 has the effect of assigning the value 4 to the variable X Addition + Means add two or more values together. 10 LET A = 1 20 LET B = 2 30 LET Y = A + B Subtraction - Means subtract one value from another 10 LET X = 1 20 LET C = 4 30 LET X = X - C x = ____________

Multiplication * Means multiply two values together 10 LET D = 2 20 LET Z = D * 3 Z = ___________

Division / Used to divide one value by another 10 LET E = 8 20 LET W = E / 8 W = ___________

Exponential ^ Means a number raised to the power of, ie, 102 means 10 * 10, and Y3 means Y * Y * Y 10 LET F = 2 20 LET V = F ^ 3 V = ___________

Page 4: Variable Declaration

OPERATOR PRECEDENCE The arithmetic operators are evaluated in a pre-determined order. This order may be over-ridden by the use of parenthesis (brackets). The rules of operator precedence are,

1 Exponential ^ 2 Multiplication and division * / 3 Addition and Subtraction + - 4 The operators are always evaluated left to right Parenthesis can be used to override the order of

precedence. Consider the following equation, Y + B X = ------- C

Page 5: Variable Declaration

In BASIC, we might be tempted to write this as, 10 LET X = Y + B / C HOW-EVER, this is wrong, as because of operator

precedence, division is always performed before addition. Thus, the value of B gets divided by the value of C first, and the result is then added to the variable Y.

Brackets can be placed around terms to instruct BASIC about how to perform calculations. BASIC always performs those calculations inside brackets first, so the above statement is actually written as

10 LET X = (Y + B) / C

Page 6: Variable Declaration

DECISION MAKING

Most programs need to make decisions. The relational operators used to compare two

values as part of the decision making process are,

= Equal to > Greater than < Less than <> Not equal to <= Less than or equal to >= Greater than or equal to

Page 7: Variable Declaration

Use in BASIC

There are several statements available in the BASIC language for this. The IF THEN statement is one of the them.

When the condition associated with the IF statement is true, the program statement following the THEN keyword is executed.

Examples:

10 IF A < 6 THEN PRINT Z 20 IF Z = M THEN LET X = M

Page 8: Variable Declaration

IF THEN ELSE STATEMENTS

This includes an ELSE statement, which specifies the block (statements) to be executed when the IF statement is not satisfied (ie, fails).

10 IF DEPT = 3 THEN LET RATE = 3.00 ELSE LET RATE = 1.00

Page 9: Variable Declaration

Back to calculations in BASIC

In BASIC, all calculations must be performed on the right hand side of the = sign. The statement

10 LET -C = Y is wrong, because you cannot do assignments ( - )

on the left side. The statement should be re-written as,

10 LET C = - ( Y ) This has the desired effect of negating the value of

Y.

Page 10: Variable Declaration

DATA NAMES Names given to variables which represent data can be a

maximum of 32 alphanumeric characters. Some BASIC versions only recognize the first two characters. The first letter of the data name must be ALPHABETIC and lowercase letters are treated as uppercase.

Examples of NUMERIC VARIABLES are, RATE.OF.PAY HOURS.WORKED B41 X y Home.score Data names should be meaningful and self explanatory. It is

pointless having cryptic variable names. CHARACTER STRINGS

Character strings are used to store non-numeric data, ie ABCD and ALFRED are both character strings. The rules for character strings are the same as those for numeric variables, except that the last character must be $ Examples,

NAME$ ADDRESS$ TEXT$ X$

Page 11: Variable Declaration

Assigning string variables,

10 LET A$ = "Mud in your eye" 20 LET X$ = A$ 30 LET N$ = "" 40 REM "" is a null string

THE PRINT STATEMENT

The PRINT statement in BASIC is provided to allow the displaying of text and the value which variables contain, on the screen. There are many variations to the PRINT statement.

10 PRINT D When executed, the value of D will be displayed at the current cursor location. 10 PRINT "D" This prints whatever is inside the quotes, ie, D 10 PRINT A,B The comma specifies that the value of B should be displayed at the next TAB position. These are every 14 characters. 10 PRINT "A";"B" The semi-colon specifies that the next item, B is is to displayed immediately after the previous item. 10 PRINT A;B In this case, a space will occur between the values of A and B. Referring back to the first program, the displayed result can be made easier by changing

line 50 as, 50 PRINT "The distance covered is ";Distance;" miles."

Page 12: Variable Declaration

Loops or Iterative Constructs

Virtually all programming languages have a facility to allow a section of code to be repeated (iterated). There are several variations to iterative or looping constructs. For the most part, they fall into two categories: FOR loops and WHILE/DO loops.

In each of the following examples, the code will output "Hello World" 10 times.

Programming

language Example

"C", C++ for (i = 1; i <= 10; i = i+1) {

printf("Hello World\n");

}

Basic For i = 1 To 10

Print "Hello World"

Next i

Page 13: Variable Declaration

While loops

Programming Language While constructs

"C", C++, Java, Perl while (condition) {

statements

}

Basic While condition

statements

Wend

Page 14: Variable Declaration

Program Structure

Virtually all structured programs share a similar overall structure:

Statements to establish the start of the program

Variable declaration Program statements (blocks of

code) The following is a simple

example of a program written in several different programming languages. We call this the "Hello World" example since all the program does is print "Hello World" on the computer screen.

Language Example program

Basic print "Hello World"

"C" #include void main() { printf("Hello World"); }

Page 15: Variable Declaration

Continued in the next part……