690 likes | 705 Views
Learn about variables in Pascal, how they represent values, change during program execution, and how to work with them effectively. Get insights into identifiers, commands, and assignment operators in Pascal programming.
E N D
CPSC 110 - Pascal Chapter 2 – part b Brent M. DingleTexas A&M University
Back to the programming- Variables in Pascal • As in math variables are ‘abstract’ representations of values. • Think of a variable as a name. • For example consider your name, it represents you. • So instead of listing out all of your traits to identify you, we can talk about you by name – and the traits are known.
Variables (cont) • Unlike in math the value of a variable may change – as the program runs. • Also variables in a program may be one or more characters in length. • Some example variable names are: • X Y Sum N1 N2 Rate time ANS
Values • Values may be numbers or other types of data. • Variables are said to contain values.
Working up to understanding variables and their values • Shortly we will look at a sample program to demonstrate what variables are and how they contain values. • You may or may not be able to immediately tell what it does – don’t worry it will be explained.
Sample Program 1Variables and their values PROGRAM AddTwo VAR n1, n2, sum : integer; BEGIN writeln(‘Enter two numbers’); readln(n1, n2); sum := n1 + n2; writeln(n1, ‘ plus ‘, n2, ‘ = ‘, sum); END.
Statements • Between the BEGIN and END lines are 4 instructions to be performed by the computer. Each instruction is called a statement. • Every statement ends with a semi-colon (;)There are some exceptions to the ending semi-colon, but don’t worry too much about that now.
Some commands • We will be returning to that program – but before that we need to learn some Pascal commands.
writeln • The writeln (pronounced write line) command tells the computer to write a line of text. • Specifically the computer is to output to the screen whatever is in the parentheses after the word writeln. • Anything in single quotes is output “as-is” • Anything not in single quotes is assumed to be a variable and the variable’s value is output.
writeln - example 1 • So the statement:writeln(‘Enter two numbers’);will cause the computer to print:Enter two numbersto the computer screen.
writeln – example 2 • The statement:writeln(n1);would cause the computer to output the VALUE of n1. • Assuming that n1 holds the value 42, then the output of the above statement would be:42
writeln – example 3 • The statement:writeln(‘The value of n1 is’, n1);would cause the computer to output:The value of n1 is 42(again assuming n1 contained the value 42)
readln • The readln command causes the computer to read data from the keyboard (or other input device – such as a file) and place it into any variables specified between the parentheses following the readln command. • If no variables are specified (i.e. there are no parentheses following the readln command) then the computer will simply wait for the user to press the [enter] key.
readln – example 1 • So the statement:readln(n1);would read ONE number (of 1 or more digits) from the keyboard and place the value of that number into the variable n1. • So if the user typed: 31and pressed enter, the value of n1 would become 31.
readln – example 2 • So the statement:readln(n1, n2);would read TWO numbers (of 1 or more digits) from the keyboard. • So if the user typed: 53 27and pressed enter, the value of n1 would be 53 and the value of n2 would be 27.
Assignment Operator := • The assignment operator in Pascal is the colon equals, := • So the statement:n1 := 5;would cause the value of n1 to become 5 • Important note: we call the number 5 a constant because it will not change in value.
Assignment Op – continued • The statement:sum := n1 + n2;would cause the value of sum to become whatever the value of n1 plus the value of n2 is. • So if n1 contained the value 55 and n2 contained the value 33, then the above statement would set the value of sum to be 88.
Expression • Just for reference an assignment statement is always made up of a variable on the left hand side of the := and an expression on the right hand side. • IN THIS CONTEXT, an expression may be a variable, a number, or a more complicated expression made up of variables, numbers and arithmetic operators (like +, -, / or *).
Going back to Program 1You should now know what most of it does. PROGRAM AddTwo VAR n1, n2, sum : integer; BEGIN writeln(‘Enter two numbers’); readln(n1, n2); sum := n1 + n2; writeln(n1, ‘ plus ‘, n2, ‘ = ‘, sum); END.
Identifiers • In the sample program notice the name of the program is AddTwo – this is an identifier. • The variable names: n1, n2, and sum are also identifiers. • In general a name used in a Pascal program is called an identifier. • Identifiers are defined to be any string made up of letters, digits and the underscore symbol.
Identifiers – rules • ALL identifiers MUST start with a letter or an underscore. • Any two identifiers or numbers must be separated by one or more spaces, by a line break, by a punctuation symbol (comma, semi-colon, colon, etc) or by a combination of two or more of these separators.
Identifiers – Examples • All the below are valid Pascal identifiers:X x1 x_1 Sum ABC1234q7_abc RATE count DaTa2 Hi_There
NOT Identifiers • None of the below are identifiers:21 3x 4Y %change Data.1Bad-Data FIRST.PAS • Do you see why?
NOT Identifiers – explanation • 21, 3x, 4Y and %change are NOT identifiers because they do not start with a letter or underscore. • Data.1, Bad-Data and FIRST.PASare NOT identifiers because they contain characters that are not letters or the underscore.
Identifiers – length • Technically identifiers in Pascal may be of any length, however Pascal limits you to 63 characters – which should be more than you need. (Who wants to type a variable name that is that long anyway?)
Case (In)Sensitivity • In Turbo Pascal there is NO distinction between upper and lower case letters when it comes to identifiers. • For example: Sum, sum and SUMare all the same identifier as far as Turbo Pascal is concerned.
Reserved Words • So getting back to our sample program, you should be wondering about the words:program, var, begin and end. • These are all reserved words in Pascal. • A reserved word has a predefined meaning in Pascal.
Standard identifiers • You may wonder why words such as readln, writeln, and integer are NOT reserved words… • This is because their meaning, while predefined may be changed – these type of identifiers are called standard identifiers.
General Program Layout • Program Heading • Variable Declarations (global) • Program Body
Program Heading • This is just the first line of most programs. • For example:PROGRAM AddTwo;
Variable Declarations • This is where the (global) variables are declared. • For example:VAR n1, n2, sum : integer; r1 : real;
Program Body • The program body is the list of statements to be executed. • This in general are all statements between the BEGIN and END lines. • Side note: the last END is followed by a period.
Data Types • A data type is a type or category of data. • All variables are of some data type. • When you declare a variable you also declare its type.
Variable Declaration • An example of variable dclaration is:var n1, n2, sum : integer; • This declares 3 variables of type integer.
Numbers in Pascal • There are two major number data types in Pascal: • Integer (e.g. –1, 53, 1002, etc) • Real (e.g. –4.3256, 1.045, 120.29801, etc)
Integers • Integers are stored as exact values. • Because a computer has finite storage, there does exist a minimum and maximum integer value that Pascal will understand. • Can you write a program to determine these minimum and maximum values?Hint: maxint
Reals • Type reals are stored only in an approximate form – again due to the finiteness of a computer. • Type reals may be written using a form of scientific notation: • 5E27 = 5 * 1027 or 6.5E-3 = 6.5*10-3 • Note though the below are NOT valid numbers: • .5E12 because there is no number before the decimal point • 3.78E11.4 because there is a decimal point in the exponent • Reals also have a maximum and minimum value in Pascal.
Type Compatibility • Notice that the number 3 and 3.0 are NOT the same type. • 3 is type integer • 3.0 is type real
Type Compatibility (cont) • Say you have the following variables:var n1, n2 : integer; r, q : real; • You may assign an integer value to a variable of type integer OR type real. • So the statements:n1 := 3; and r = 3; and q := n1;would all be valid.
Type Compatibility (cont 2) • However you may assign a real value ONLY to type reals. • So the statements:r := 4.05 and r := q;are valid. • But the statements:n1 := 4.05; and n1 := q;are NOT valid.
Arithmetic Expressions • Arithmetic expressions also have a type associated with them. • For example in:x := 5 * (n + (m/9) + 3*y);the expression is the right hand side of the statement (i.e. 5 * (n + (m/9) + 3*y) ). • The expression returns a type real (because it contains division).
Determining Type of Expressions • Combining something of type real with something of type integer or real always yields something of type real.real + real => real real + integer => real
Determining Type of Expressions • Combining anything with a division sign always yields something of type real.real / real => realinteger / integer => realreal / integer => realinteger / real => real
Determining Type of Expressions • Combining two things of type integer with an addition sign, a subtraction sign, or a multiplication sign yields something of type integer.integer + integer => integerinteger – integer => integerinteger * integer => integer
Determining Type of Expressions • Placing a minus sign in front of an arithmetic expression does NOT change its type.- integer => still an integer- real => still a real
MOD and DIV • Pascal has two built in arithmetic operators that work ONLY on integer (ordinal) types. • Mod returns the remainder of an integer divided by an integer. • So, 5 mod 3 returns 2. • Div returns the quotient of an integer divided by an integer. • So 5 div 3 returns 1.
Char and string data types • Pascal has two types that deal with characters – a character is generally anything that can be typed from the keyboard. • Type char can hold ONE character. • Type string may hold more than ONE character, based on how it is declared.
Strings • Variables of type string are declared:var name : string[40]; • This declares a string that can hold up to 40 characters. • To assign a string a value you use a statement like:name := ‘Robert Frost’;
Problems with strings • Make sure your strings are declared long enough.Programming as follows:var name : string[4]; : : name := ‘Bob Hope’;will cause errors to develop !(Notice ‘Bob Hope’ is longer than 4 characters)
String Trick • To place a single quote inside a string you must type it twice. • For example to set message to have the value Bob’s rock you would say:message := ‘Bob’’s rock’;And that is 2 single quotes between the b and the s, NOT a double quote.