

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


























Chapter 3
Variables are data structures whose value may be changed by the algorithm.
For example: to declare a variable named “some number” that can hold a numeric value:
some_number isoftype Num
For example: to assign the value 4 to the variable some_number :
some_number <- 4
- Numbers , declared as:
my_age isoftype Num
my_age can now store one number of any magnitude
For example :
my_age <- 1 // this is OK my_age <- 65 // so is this my_age <- 14.5 // and so is this
my_age <- “old” // an ERROR
- Booleans , declared as:
this_test isoftype Boolean
this_test can now hold one boolean ( TRUE or FALSE ) value
For example :
this_test <- TRUE // this is OK this_test <- FALSE // so is this
this_test <- “true” // an ERROR
- Pointers , declared as:
this_num_ptr isoftype ptr toa Num
this_num_ptr can now “know” where a Num variable “lives” in memory
( will discuss pointers later )
The Assignment Operator stores a particular value in a variable.
my_age <- 43 (any integer or real number)
your_grade <- ‘A’ (quote marks needed to distinguish Chars from variable names)
this_test <- TRUE ( TRUE and FALSE are each single boolean values)
For pointers, assignment means one of
three things, depending on how used ...
creates a new Number variable for each pointer and makes the specified pointer variable refer to it, e.g.:
this_ptr that_ptr
Arithmetic Operators allow us to express mathematical operations:
num_three <- num_one + num_two
num_three <- (num_one + num_two) * 2
Use parentheses to alter the standard order of operations:
read and print may take any number of arguments of type Num, Boolean, Char, or String.
For example , read(this_var) read(that_var) is equivalent to: read (this_var, that_var)
algorighm Simple_Example
// declare the variables num_one, num_two, num_three isoftype Num // get the numbers from “outside” read (num_one, num_two) // perform addition and show result num_three <- num_one + num_two print(“The addition result is: ”, num_three) //perform multiplication and show result num_three <- num_one * num_two print(“This multiplication results is: ”, num_three)
endalgorithm // Simple_Example
Conditionals can have three parts:
For example :
if (num_one < num_two) then highest <- num_two else highest <- num_one endif
The “else” clause is optional :
if ( salary > MONEY_NEEDED ) then save some of it give some to charity endif
Conditionals produce boolean ( TRUE/ FALSE ) results based on comparisons specified by the relational operators: