CSCI 150

Home | | Course Schedule | | Assignments | | Lecture Notes

Variables

Variables:
A variable is a named location in memory. It is used to store information.

Example:

	program songs;
	var
		favoriteSong: string;
	begin
		writeln('What is your favorite song?');
		readln(favoriteSong);
		writeln('I love ', favoriteSong, ' too!');
	end.

Naming variables:

Variable types:
The type of a variable tells the compiler what kind of information is stored in memory.
TypeKind of infoUses
string
 
text (string of letters
and/or numbers)
Write to monitor or
Read from keyboard
integer
 
integer number
(1, 17, -37, etc.)
Arithmetic operations
+, -, *, /
real
 
decimal number
(2.5, 0.7, -314.2, etc)
arithmetic operations
 

Assigning values to a variable:
Variables can be assigned values with readln( ) or using the assignment operator, :=

Example:

	program dogInformation;
	var
		ageOfDog: integer;
		nameOfDog: string;
	begin
		ageOfDog := 12;
		nameOfDog := 'Patches';
		writeln(nameOfDog, ' is ', ageOfDog, ' years old.');
	end.