Docsity

Introduction to Computing and Algorithms: Built-in Data Types and Operators, Study notes of Computer Science

Partial preview of the text

Download Introduction to Computing and Algorithms: Built-in Data Types and Operators and more Study notes Computer Science in PDF only on Docsity!

Introduction to Computing

and Algorithms

Basic Data, Operations

and Decisions

Chapter 3

Basic Tools for

Building Algorithms

  • Atomic Data
    • Numbers
    • Characters
    • Booleans
    • Pointers
  • Operators
    • Assignment
    • Arithmetic
    • Input / Output
      • Relational
    • Boolean

Built-in Data Types

Variables are data structures whose value may be changed by the algorithm.

  • Each variable must be declared to
    • have an identifier (i.e., a name )
    • be of some data type :

For example: to declare a variable named “some number” that can hold a numeric value:

some_number isoftype Num

  • Each variable must be assigned a value of the appropriate type:

For example: to assign the value 4 to the variable some_number :

some_number <- 4

Built-in Data Types

- 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

Built-in Data Types (cont.)

- 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

Built-in Data Types (cont.)

- 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

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)

Assignment Operator (cont.)

For pointers, assignment means one of

three things, depending on how used ...

  1. Creating a new variable. Given: this_ptr isoftype ptr toa Num, that_ptr isoftype ptr toa Num , other_ptr isoftype ptr toa Num then: this_ptr <- new(Num) that_ptr <- new(Num)

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

Arithmetic Operators allow us to express mathematical operations:

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /

Using the Arithmetic Operators

  • To add the values found in variables num_one and num_two , and store the result in variable num_three :
  • To add thevalues found in num_one and num_two , multiply the result by two, and store that result in variable

num_three :

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)

  • Don’t worry about the exact details of the format of input or about how to format output for printing.
  • We intentionally leave that vague for now. Assume that it works by magic.
  • Such things are unimportant details covered in later programming courses.

Input and Output Operators

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

A Simple Example

Decisions Statements

(Conditionals)

Conditionals can have three parts:

  • A conditional expression
  • A then clause
  • An (optional) else clause

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

Relational Operators

• Greater than >

• Greater or equal >=

• Equal to =

• Less than or equal <=

• Less than <

• Not equal to < >

Conditionals produce boolean ( TRUE/ FALSE ) results based on comparisons specified by the relational operators: