Variables, literals, constants - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Variables, literals, constants

Description:

Operators are symbols that can be used to perform certain calculations. ... This operator has two meanings: subtraction operator (binary) negation operator (unary) ... – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 21
Provided by: vdou8
Category:

less

Transcript and Presenter's Notes

Title: Variables, literals, constants


1
Variables, literals, constants
  • Variable named memory location that holds a
    changeable value
  • Literal fixed value written into a program
  • Constant named memory location that holds a
    non-changeable value

2
Literals
  • Literal fixed value written into a program
  • Not declared, no memory location assigned
  • Not re-usable just written directly into each
    statement
  • This makes it easy to make a mistake
  • Solution use define directive.

define PI 3.14 int main () double area,
radius radius 12.1 area PI radius
radius return 0
Directs preprocessor to replace all occurrences
of PI with 3.14
3
define
  • Syntax

define NAME value
The name is usually capitalized
Preprocessor directives are not statements.
There should be no semicolon at the end.
4
define
  • Advantage if you need to change the value, you
    only have to do it once

If I want to change the value of PI to 3.14159, I
only have to do it here and the preprocessor will
take care of the rest.
define PI 3.14 int main () double area,
radius double circumference radius
12.6 area PI radius radius circumference
2 PI radius return 0
5
Constants
  • Constant named memory location that holds a
    non-changeable value
  • Declared
  • Re-usable
  • MUST be initialized
  • Can not be modified after initialization

int main () const double pi 3.14 double
area, radius radius 12 area pi radius
radius return 0
6
Elements of a program
  • Literals ? fixed data written into a program
  • Variables constants ? placeholders (in memory)
    for pieces of data
  • Types ? sets of possible values for data
  • Expressions ? combinations of operands (such as
    variables or even "smaller" expressions) and
    operators. They compute new values from old ones.
  • Assignments ? used to store values into variables
  • Statements ? "instructions". In C, any
    expression followed by a semicolon is a statement

7
Elements of a program
  • Control-flow constructs ? constructs that allow
    statements or groups of statements to be executed
    only when certain conditions hold or to be
    executed more than once.
  • Functions ? named blocks of statements that
    perform a well-defined operation.
  • Libraries ? collections of functions.

8
A hierarchy of program elements
?
Literals variables
Operators
Expressions
Statements
Functions
Libraries
Programs
Systems
9
Expressions
  • An expression is anything that has a value.
  • An expression may consist of
  • variables
  • operators
  • literals
  • constants
  • function calls
  • parentheses

10
Operators
  • Operators are symbols that can be used to perform
    certain calculations. They are always inside
    expressions.
  • Operators can be classified according to
  • The number of their operands
  • Unary (one operand)
  • Binary (two operands)
  • The type of their operands and of their output
  • Arithmetic
  • Relational
  • Logical
  • Bitwise

11
Assignment operator
  • Binary operator used to assign a value to a
    variable.
  • Its left operand is the destination variable
  • Its right operand is an expression.

int var var 10
COPY
12
Arithmetic operators
  • They operate on numbers and the result is a
    number.
  • The type of the result depends on the types of
    the operands.
  • If the types of the operands differ, one is
    "promoted" to other.
  • The "smaller" type is promoted to the "larger"
    one. char ? int ? float ? double

13
Arithmetic operators ,
  • is the addition operator
  • is the multiplication operator
  • They are both binary

14
Arithmetic operator ?
  • This operator has two meanings
  • subtraction operator (binary)
  • negation operator (unary)

e.g. 31 - 2
e.g. -10
15
Arithmetic operator /
  • Division operator
  • CAREFUL! The result of integer division is an
    integer

e.g. 5 / 2 is 2, not 2.5
16
Arithmetic operator
  • The modulus (remainder) operator.
  • It computes the remainder after the first operand
    is divided by the second
  • It is useful for making cycles of numbers
  • For an int variable x if x is 0 1 2
    3 4 5 6 7 8 x3 is 0
    1 2 0 1 2 0 1 2

e.g. 5 2 is 1, 6 2 is 0
17
Relational operators
  • These perform comparisons and the result is what
    is called a Boolean a value TRUE or FALSE
  • FALSE is represented by 0 anything else is TRUE
  • The relational operators are
  • lt (less than)
  • lt (less than or equal to)
  • gt (greater than)
  • gt (greater than or equal to)
  • (equal to)
  • ! (not equal to)

18
Logical operators
  • These have boolean operands and the result is
    also a boolean.
  • The basic boolean operators are
  • (logical AND)
  • (logical OR)
  • ! (logical NOT) -- unary

19
Special assignment operators
  • write a b instead of a a b
  • write a - b instead of a a - b
  • write a b instead of a a b
  • write a / b instead of a a / b
  • write a b instead of a a b

20
Special assignment operators
  • Increment, decrement operators , --
  • Instead of a a 1 you can write a or a
  • Instead of a a - 1 you can write a-- or --a
  • What is the difference?

pre-increment
post-increment
num 10 ans num
num 10 ans num
First increment num, then assign num to ans. In
the end, num is 11 ans is 11
First assign num to ans, then increment num. In
the end, num is 11 ans is 10
Write a Comment
User Comments (0)
About PowerShow.com