0% found this document useful (0 votes)
4 views

Elements of Programming

The document provides an overview of fundamental concepts in programming languages, including identifiers, variables, literals, constants, and data types. It explains the importance of naming objects and memory locations, as well as the distinction between literals and constants. Additionally, it covers expressions, statements, operator precedence, and associativity, which are essential for writing effective code.

Uploaded by

aitajprince100
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Elements of Programming

The document provides an overview of fundamental concepts in programming languages, including identifiers, variables, literals, constants, and data types. It explains the importance of naming objects and memory locations, as well as the distinction between literals and constants. Additionally, it covers expressions, statements, operator precedence, and associativity, which are essential for writing effective code.

Uploaded by

aitajprince100
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

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

• Variables are names for memory


locations.
• Each memory location in a computer has
an address.
• Although the addresses are used by the
c o m p u t e r i n t e r n a l l y, i t i s v e r y
inconvenient for the programmer to use
addresses.

Variables

• A programmer can use a variable, such as score, to


store the integer value of a score received in a test.
Since a variable holds a data item, it has a type.

“In computer programming, a variable is a storage


location and an associated symbolic name which
contains some known or unknown quantity or
information, a value.”
Literals
A literal is a predetermined value used in a program. For
example, if we need to calculate the area of circle when the
value of the radius is stored in the variable r, we can use the
expression 3.14 × r2, in which the approximate value of π (pi)
is used as a literal. In most programming languages we can
have integer, real, character and Boolean literals. In most
languages, we can also have string literals.

To distinguish the character and string literals from the names


of variables and other objects, most languages require that the
character literals be enclosed in single quotes, such as 'A', and
strings to be enclosed in double quotes, such as "Anne".
Constants
The use of literals is not considered good programming practice
unless we are sure that the value of the literal will not change
with time (such as the value of π in geometry). However, most
literals may change value with time.

For this reason, most programming languages define


constants. A constant, like a variable, is a named location that
can store a value, but the value cannot be changed after it has
been defined at the beginning of the program. However, if we
want to use the program later, we can change just one line at the
beginning of the program, the value of the constant.
Inputs and Outputs
Almost every program needs to read and/or write data. These
operations can be quite complex, especially when we read and
write large files. Most programming languages use a predefined
function for input and output.

Data is input by either a statement or a predefined function


such as scanf in the C language.

Data is output by either a statement or a predefined function


such as printf in the C language.
Expressions
An expression is a sequence of operands and operators that
reduces to a single value. For example, the following is an
expression with a value of 13:

An operator is a language-specific token that requires an action


to be taken. The most familiar operators are drawn from
mathematics.
Statements
A statement causes an action to be performed by the program. It
translates directly into one or more executable computer
instructions. For example, C, C++ and Java define many types
of statements.

An assignment statement assigns a value to a variable. In other


words, it stores the value in the variable, which has already been
created in the declaration section.

A compound statement is a unit of code consisting of zero or


more statements. It is also known as a block. A compound
statement allows a group of statements to be treated as a single
entity.
Data types
■ A data type defines a set of values and a set of
operations that can be applied to those values. The
set of values for each type is known as the domain for
the type. Most languages define two categories of
data types: simple types and composite types.
■ A simple type is a data type that cannot be
broken into smaller data types.
■ A composite type is a set of elements in which
each element is a simple type or a composite
type.
Data types in Java
Data Types and Their Data Sets

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

You might also like