Lab 2 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Lab 2

Description:

Work Areas on the main computer disk. Lab1/ mkdir Lab1. cd Lab1 ... Illegality 'Compiler' Errors some languages and compilers allow mis-designations of variables ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 26
Provided by: cseBu
Category:
Tags: illegality | lab

less

Transcript and Presenter's Notes

Title: Lab 2


1
Lab 2
  • posted, due 2/10
  • Lab 3 will be posted 2/5
  • Must be submitted via UB Learns according to the
    instructions on the web site
  • There are no emails involved in the submission

2
How do we use numbers?
  • Counting
  • Measuring
  • Non-numerical designations
  • Finally, they can designate a state unrelated
    to the number (1 if by land, 2 if by sea)

3
So computers
  • Must deal with each type of number in its own
    way
  • Must store those numbers in their own way
  • Must make those numbers available, though,
    through a common interface
  • That interface, is the VARIABLE

4
Variables
  • x, y, z
  • i, j, k
  • TotalCountOfNewEmployees
  • Geekish_Designations_0123
  • They Stand for Something, can be named anything
    except reserved C words

5
Variable Types (primitive, common)
  • Integers meant to be a whole number (used for
    counting) C int
  • Floating Pt. meant to be a detailed number
    (used for measuring) called a double
  • Character has no meaning other than to be read
    by a human C char
  • Strings of Characters have no meaning other
    than to be read by a human. Not native to C,
    but libraries exist to help
  • Boolean reflects a state of true or false
    C bool

6
x 14
  • Could mean 14 (not likely)
  • Could mean 13.999995
  • Could mean 14.000001
  • Could mean 20 (honestly), might not be base-10,
    more later
  • Could mean Fourteen, which has no meaning other
    than to be read by humans

7
Typing a variable clarifies what it does and
its precision
  • int x
  • x 14
  • Therefore, 14 means 14
  • double x
  • x 14.0000
  • Therefore, x has meaning, 14 to 4 places

8
note
  • Can be initialized at the declaration
  • int x 14 // type name initValue
  • Can only be typed once
  • Value can be reset (thats why its called a
    variable).

9
floating point?
  • 14.6567
  • 128.1231
  • 154367.001423
  • 3.14159
  • 987987465986543.
  • adequate precision
  • cout truncates for space

10
chars and strings
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • char Id 'm' // chars are single-quoted
  • string name "mikeb" // strings are double
  • int main()cout ltlt Id ltlt endlcout ltlt name ltlt
    endl

11
New Types
  • Boolean true or false (really means presence or
    absence), used to signify a condition, or
    state of being.
  • the bool type can have values true, false, 1,
    or 0
  • bool X,Y, Z
  • X true (same as 1, means X is present)
  • Y false (same as 0, means Y is NOT
    present)
  • Z X OR Y (what is Z?, i.e. does Z signify
    some presence?)
  • Z X AND Y (what is Z?)

12
A smoke, water, burglar alarm
  • Boolean variables W, B, S, A
  • W is true if water
  • B is true if burglar
  • S is true if smoke
  • A means sound the alarm
  • A W and B and S
  • Does W and B and S make sense?
  • A W or B or S

13
example code
  • include ltiostreamgtinclude ltstringgtusing
    namespace stdchar Id 'm'string Name
    "mikeb"bool Flag falseint x,y,zdouble
    a14.0123,b,c
  • int main( )x true // lets try 1,
    1, 1cout ltlt x ltlt endlcout ltlt Id ltlt
    endlcout ltlt name ltlt endlcout ltlt flag ltlt
    endlcout ltlt a ltlt endl

14
once a variable is designed
  • you can get it from the operator (if you want)
  • int x
  • x0
  • cout ltlt enter x
  • cin gtgt x

15
The "if..else" statement
  • if ( a b )
  • // do this
  • else
  • // do that

comparison
16
"logical" comparisons
  • if ( a b )
  • if ( a ! b )
  • if ( a lt b )
  • if ( a gt b )
  • if ( a lt b )
  • if ( a gt b )

17
demo
18
Legality
  • Variable value and variable type must be the
    same.
  • Deterministic behavior - All computers yield the
    same result with the same variables

19
Computers DO NOT completely check our programs
  • Called Compilation
  • Must be successful before a program can run
  • But not all errors are caught in compilation

20
Compilation
will it work?

21
Illegality
  • Compiler Errors however, some languages and
    compilers allow mis-designations of variables
  • Indeterminate results
  • Run-time Errors

22
Illegal 1 mis-defined variables
  • int y
  • y 14.0001
  • char q
  • q abcdef
  • double z
  • z hello

23
Why Type Variables
  • Deterministic behavior of variables and the
    numbers they represent.
  • New definition of Legality We will never
    perform mathematical or logical operations on
    variables of different types.

24
Illegal 2 mixing variables in same equation
  • int x
  • double y
  • char z
  • x 12
  • y 16.001
  • z x y

25
Illegal 3 - Operation wrong for variable type
  • char Q, R, S
  • Q R / S (/ divide)
Write a Comment
User Comments (0)
About PowerShow.com