Variables - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Variables

Description:

Data types are used to separate data into whole numbers, real numbers, and ... float myPay=3.1512345; double bigNumber=1.123456789101112; char firstInitial= J' ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 14
Provided by: jameso9
Category:
Tags: mypay | variables

less

Transcript and Presenter's Notes

Title: Variables


1
Variables
2
Storing data
  • Data is stored in our code by using variables and
    constants. We say that we assign a value to a
    variable or a constant.
  • int total 3
  • final price 100.25
  • Assignment statements always assign from right to
    left, never left to right.

3
Data type
  • Data types are used to separate data into whole
    numbers, real numbers, and character data.
  • int and long refer to whole numbers. Use int for
    smaller whole numbers and long for really big
    numbers. We will look at this again when we get
    to number systems.
  • float and double refer to real numbers ( numbers
    with a decimal point). The difference in these
    two is the precision of the number. Float is good
    to around 7 decimal places and double to
    around15.
  • char is used for single characters. Strings are
    used for multiple characters. We will look at
    strings in more detail later.

4
Declaration statements
  • int total3
  • long amount123456789
  • float myPay3.1512345
  • double bigNumber1.123456789101112
  • char firstInitialJ
  • char lastInitialR

5
Naming variables
  • While it is not a requirement for the program to
    run, it is important for clarity of reading and
    your grade in this class that you use descriptive
    names for your variables.
  • int x 25 ?
  • int totalSum 25 ?
  • Notice that all words but the first are
    captitalized. Please do this in your work.

6
Note that the data types are drawn as different
sizes. This is because each data type is given a
predetermined size of memory (in bytes) by the
system depending on the data type. This is why we
have to declare out variables.
7
  • Lets say an integer takes up four bytes of
    memory in our system.
  • int total 0
  • This declaration statement tells the system to
    reserve 4 bytes of memory to store this value and
    to give it an initial value of 0. The system has
    many memory locations referred to by some number
    like 0012FED4. Rather that having to remember
    this big hex number we are allowed to refer to
    the memory location by the identifier, in this
    case total. If we want to change the value of
    total to depend on the values of other variables
    we could write
  • totalnumberOnHand-numberSold
  • This assignment statement would change the value
    of total from 0 to whatever value
    numberOnHand-numberSold is equal to at this point
    in execution of the code. To change the value of
    total again we would have to write another
    assignment statement.
  • We can use any math operation with little problem
    using variables as long as we pay attention to
    the data type and operator precedence.

8
Operator precedence
  • Operator precedence basically means that any
    operators we use in our code have an order that
    they are read in when more than one of them
    appear on a single line. For our purposes.
  • 1st parenthesis ( )
  • 2nd multiply, divide, modulus , /,
  • 3rd add ,subtract ,-
  • The statement is read from left to right so for
    instance if we have and on the same line do
    the problem from right to left. If we have
    parenthesis on any line do this first. If we have
    for instance multiplication and addition on the
    same line do the multiplication first and then
    the addition.
  • (48)/4 would be equal to 3.
  • 48/4 would be equal to 6.
  • 482 would be equal to 20.
  • 482 would be equal to 34.

9
(No Transcript)
10
Operator precedence
11
To display and input data
  • cout
  • cintotal
  • cout
  • cout and cin are keywords used for input/output
    in C programming. cout allows you to display to
    the screen and cin allows you to store keyboard
    input to a variable. The endl implies that any
    statement following will appear on the next
    line.

12
Compiler
  • Any high level language has to be compiled or
    changed from the code we write to something the
    machine understands. In C the code is compiled
    to machine language. Machine language is 0s and
    1s or binary numbering.

13
Running the code
  • When we run a console application we should see
    the DOS shell with whatever we told the system to
    write.
Write a Comment
User Comments (0)
About PowerShow.com