Elements of Programming
Elements of Programming
LANGUAGES
Lines of Code in Example Products
2
Programming languages
!3
Overview
• Constants
• operators
• Variables
• hierarchy of operators
• datatypes
• equations
• functions
• expressions
Identifiers
■ One feature present in all procedural languages, as
well as in other languages, is the identifier—that is,
the name of objects. Identifiers allow us to name
objects in the program.
■ For example, each piece of data in a computer is
stored at a unique address.
Identifiers
■ If there were no identifiers to represent data locations
symbolically, we would have to know and use data
addresses to manipulate them.
■ Instead, we simply give data names and let the
compiler keep track of where they are physically
located
Variables
0-10
Examples of Data Types
0-11
Examples of Data Types
0-12
Operators and Their Computer Symbols
0-17
Operators and Their Computer Symbols
0-18
Definitions of the Logical Operators
0-19
Definitions of the Logical Operators
0-20
Definitions of the Logical Operators
0-21
Hierarchy of operators
• Precedence order.
★ When two operators share an operand the
operator with the higher precedence goes first.
★ For example, 1 + 2 * 3 is treated as 1 + (2 * 3),
whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since
multiplication has a higher precedence than
addition.
Associativity
• Associativity. When an expression has two operators
with the same precedence, the expression is evaluated
according to its associativity.
• For example x = y = z = 17 is treated as x = (y = (z = 17)),
leaving all three variables with the value 17, since the =
operator has right-to-left associativity (and an assignment
statement evaluates to the value on the right hand side).
• On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3
since the / operator has left-to-right associativity.
• Some operators are not associative: for example, the
expressions (x <= y <= z) and x++-- are invalid.
Operator Precedence
Hierarchy of Operations
0-22
Hierarchy of Operations
0-23
Operator Precedence
Operator Precedence
Expressions and Equations
0-24
Evaluating a Mathematical Expression
0-25
Evaluating
Expression
0-26
Developing a Table of All Possible
Resultants of a Logical Expression
• Two unknowns
—A and B.
• Four
combinations: B
can be either
True or False
for each value
of A..
0-30
Developing a Table of All Possible
Resultants of a Logical
Expression
• Three
unknowns—A,
B, and C.
• Eight
combinations.
0-31
Developing a Table of All Possible
Resultants of a Logical Expression
0-32