You are on page 1of 39

Programming Logic and

Design
Programming Logic and Design Seventh Edition, Comprehensive
Understanding Unnamed, Literal
Constants and their Data Types, and
Variables.
Objectives
• Declaring and using variables and constants
• Performing arithmetic operations
• The advantages of modularization
• Modularizing a program
• Hierarchy charts
• Features of good program design
Introduction
• Data items include all the text, numbers, and other information that
are processed by a computer.
• When you input data items into a computer, they are stored in
variables in memory where they can be processed and converted to
information that is output.
• When you write programs, you work with data in three different
forms: literals (or unnamed constants), variables, and named
constants.
Understanding Unnamed, Literal Constants
and their Data Types
• All programming languages support two broad data types:
• Numeric - describes data that consists of numbers
• String - describes data that is nonnumeric
• Most programming languages support several additional data types,
including multiple types for numeric values of different sizes and with
and without decimal places.
• Integer - whole number
• floating-point – fractional, numeric variables that contain a decimal point.
This is also called as real numbers **
Understanding Unnamed, Literal Constants
and their Data Types
• Numeric Constant – or literal numeric constant. Writing specific digits
and no quotation marks needed.
• String Constant - or literal string constant. String constants, unlike
numeric constants, appear within quotation marks in computer
programs. String values are also called alphanumeric values because
they can contain alphabetic characters as well as numbers and other
characters. **
Declaring and using variables and constants
Working with Variables
• Variables are named memory
locations whose contents can vary or
differ over time. **
• At any moment in time, a variable
holds just one value.
• The ability of memory variables to
change in value is what makes
computers and programming
worthwhile. Because one memory
location can be used repeatedly with
different values, you can write
program instructions once and then
use them for thousands of separate
calculations.
Working with Variables
• Declaration - a statement that provides a data type and an identifier
for a variable.
• Initializing the variable - Declaring a starting value
• Identifier - a program component’s name.
• Data Type - is a classification that describes the following:
• What values can be held by the item
• How the item is stored in computer memory
• What operations can be performed on the data item
Working with Variables
• Variables must be declared before
they are used in a program for the
first time.
• Some languages require all
variables to be declared at the
beginning of the program; others
allow variables to be declared
anywhere if they are declared
before their first use.
Naming Variables
• As a programmer, you choose reasonable and descriptive names for your variables.
• The language interpreter then associates the names you choose with specific
memory addresses.
• Every computer programming language has its own set of rules for creating
identifiers.
• Most languages allow letters and digits within identifiers. Some languages allow
hyphens in variable names.
• Each programming language has a few (perhaps 100 to 200) reserved keywords
that are not allowed as variable names because they are part of the language’s
syntax.
• Programmers use multiple conventions for naming variables, often depending on
the programming language or standards implemented by their employers.
Naming Variables
• Common conventions include the following:
• Camel casing is the convention in which the variable starts with a lowercase
letter and any subsequent word begins with an uppercase letter.
• Pascal casing is a convention in which the first letter of a variable name is
uppercase.
• Hungarian notation is a convention in which a variable’s data type is part of
the identifier.
• The variable names usually used follow only three rules:
• Variable names must be one word
• Variable names must start with a letter.
• Variable names should have some appropriate meaning.
Assigning Values to Variables
• Assignment Statement - incorporates two actions.
• Example: set myAnswer = myNumber * 2
• Assignment Operator – the equal sign in the statement.
The assignment operator always operates from right to
left. This means that the value of the expression to the
right of the assignment operator is evaluated first, and
then the result is assigned to the operand on the left. It
is called as right-associativity or right-to-left associativity.
Understanding the Data Types of Variables
• Computers handle string data differently from the way they handle
numeric data.
• Every programming language requires that you specify the correct
type for each variable, and that you use each type appropriately.
• Numeric Variable - one that can hold digits and have mathematical
operations performed on it.
• String Variable - can hold text, such as letters of the alphabet, and
other special characters, such as punctuation marks.
Performing Arithmetic Operations
Performing Arithmetic Operations
• Most programming languages use the following standard arithmetic
operators:
• + (plus sign)—addition
• – (minus sign)—subtraction
• * (asterisk)—multiplication
• / (slash)—division
• In programming languages, you can combine arithmetic statements.
When you do, every operator follows rules of precedence (also called
the order of operations) that dictate the order in which operations in
the same statement are carried out.
Performing Arithmetic Operations
• The rules of precedence for the basic arithmetic statements are as
follows:
• Expressions within parentheses are evaluated first. If there are multiple sets
of parentheses, the expression within the innermost parentheses is evaluated
first.
• Multiplication and division are evaluated next, from left to right.
• Addition and subtraction are evaluated next, from left to right.
• Example:
• firstAnswer = 2 + 3 * 4
• secondAnswer = (2 + 3) * 4
Summary of precedence and associativity of the five most
frequently used operators
Understanding the Advantages of
Modularization
Understanding the Advantages of
Modularization
• Programmers seldom write programs as one long series of
steps.
• They break down their programming problems into smaller
units and tackle one cohesive task at a time.
• These smaller units are modules.
• Programmers also refer to them as subroutines, procedures,
functions, or methods. **
• A main program executes a module by calling it.
Understanding the Advantages of
Modularization
• To call a module is to use its name to invoke the module,
causing it to execute.
• The process of breaking down a large program into modules
is modularization.
• There are at least three reasons for doing modularization:
• Modularization provides abstraction.
• Modularization allows multiple programmers to work on a
problem.
• Modularization allows you to reuse work more easily.
Modularizing a Program
• Most programs consist of a main program, which contains the
basic steps, or the mainline logic, of the program.
• The main program then accesses modules that provide more
refined details.
• When you create a module, you include the following:
• A header - The module header includes the module identifier and possibly
other necessary identifying information.
• A body - The module body contains all the statements in the module.
• A return statement - The module return statement marks the end of the
module and identifies the point at which control returns to the program
or module that called the module.
Modularizing a Program
• Naming a module is similar to naming a variable.
• General rules used for variable identifiers
• Module names must be one word and start with a letter
• Module names should have some meaning
• Some module names are followed by a set of parentheses.
• When a main program wants to use a module, it calls the module. A
module can call another module, and the called module can call
another.
• The number of chained calls is limited only by the amount of memory
available on your computer.
Simple Program using Modularization
Creating Hierarchy Charts
SAMPLE ORGANIZATIONAL HIERARCHY CHART
Hierarchy Charts
• Operates to show the overall picture of how modules are related to one
another.
• When a program has several modules calling other modules,
programmers often use a program hierarchy chart.
• Hierarchy charts does not tell what tasks are to be performed within a
module, when the modules are called, how a module executes, or why
they are called.**
• Hierarchy chart tells only which modules exist within a program and
which modules call others.
• Because program modules are reusable, a specific module may be called
from several locations within a program.
SAMPLE BILLING HIERARCHY CHART
Introduction to Looping, List and Array
Looping
• Looping makes computer programming both efficient and
worthwhile.
• When you use a loop, one set of instructions can operate on multiple,
separate sets of data.
• In looping we have usually use while loop and for loop.
Lists
• A list is a collection which is ordered and changeable. In Python lists
are written with square brackets.
thislist = ["apple", "banana", "cherry"]
print(thislist)
• You access the list items by referring to the index number.
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Array
• Array is a collection of elements of the same type.
• Arrays are used to store multiple values in one single variable.
• An array is a special variable, which can hold more than one value at a time.
• When people talk about Python arrays, more often than not, they are talking
about Python lists.
• We can treat lists as arrays. However, we cannot constrain the type of
elements stored in a list.
cars = ["Ford", "Volvo", "BMW"]
x = cars[0]
print(x)
If…else statements
If…else statements
• Python supports the usual logical conditions from mathematics:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• These conditions can be used in several ways, most commonly in "if
statements" and loops.
• An "if statement" is written by using the if keyword.
If…else statements
a = 33
b = 200
if b > a:
   print("b is greater than a")
• Elif - The elif keyword is pythons way of saying "if the previous conditions were
not true, then try this condition".
a = 33
b = 33
if b > a:
   print("b is greater than a")
elif a == b:
   print("a and b are equal")
• Else - The else keyword catches anything which isn't caught by the
preceding conditions.
a = 200
b = 33
if b > a:
   print("b is greater than a")
elif a == b:
   print("a and b are equal")
else:
   print("a is greater than b")
Indentation
• Indentation (whitespace at the beginning of a line) use to define
scope in the code. Other programming languages often use curly-
brackets for this purpose.
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Looping: While Loop, For Loop
While Loop
• In while loop we can execute a set of statements as long as a
condition is true.
i=1
while i < 6:
print(i)
i=i+1

You might also like