Chapter 1 Logic
Chapter 1 Logic
Chapter 1 Logic
1. Hardware and software: the two major components of any computer system
Hardware: equipment, or devices
Software: programs that contain instructions for the computer
2. Input devices: allow data to enter the computer
Mouse, keyboard, scanner
Processing: working on the data; such as:
Organizing data
Checking data for accuracy
Mathematical or other manipulations on data
Central Processing Unit (CPU): hardware that performs the tasks
3. Output devices: provide data to the user
Printer, monitor, speakers
Programming language: special language containing instructions for the computer
Visual Basic, Java, C#, C++, COBOL
Syntax: the rules governing word usage and punctuation in the language
Machine language: a language that controls the computers on/off circuitry
Compiler or interpreter: software that translates programming languages to machine language
4. A program must be free of syntax errors to be run, or executed, on a computer
To function properly, the logic must be correct
5. Logic errors, or semantic errors, are more difficult to locate than syntax errors
6. Two storage categories: internal and external
Internal storage:
Main memory, random access memory (RAM)
Located inside the computer system
Volatile: contents are lost when power goes down
External storage:
Persistent: contents are relatively permanent
Floppy drive, hard drive, flash media, magnetic tape
Located outside the computer system
12.
Flowlines:
Connect the steps
Show the sequence of statements
Have arrows to show the direction
Terminal symbol (start/stop symbol):
Shows the start and end points of the statements
Lozenge shape
Variable: a memory location whose contents can vary; also called an identifier
Each programming language has it own rules for naming identifiers, including:
Legal characters
Maximum length
Use of upper or lower case
Variable name must be a single word, but can be formed from several words
rate, interestRate, interest_rate
Choose meaningful names for variables
Improves the readability and maintainability of code
15.
16.
18.
Assignment statement:
Assigns a value to a variable
Variable must appear on the left side, value on the right side of the assignment operator
Right side may be an expression that will be evaluated before storing the value in the variable
Assignment operator: the equal sign (=) in most languages
Variable:
Memory location: has an address and a value
Value (contents) is used for various operations
19.
20.
Programming began in the 1940s, using memory addresses and machine code directly
Higher level languages were developed to allow English-like instructions
Older programs were monolithic, and ran from beginning to end
Newer programs contain modules that can be combined to form programs
Two major programming techniques:
Procedural programming
Object-oriented programming
Procedural programming: focuses on the procedures that programmers create
Object-oriented programming: focuses on objects that represent real-world things and their attributes
and behaviors
Both techniques employ reusable program modules
21
Translator program
Compiler or interpreter
Changes the programmers English-like high-level programming language into the low-level
machine language
Syntax error
Misuse of a languages grammar rules
Programmer corrects listed syntax errors
Might need to recompile the code several times
Logical error
Use a syntactically correct statement but use the wrong one for the current context
Programs should be tested with many sets of data
22. Conversion
Entire set of actions an organization must take to switch over to using a new program or set of programs
23. Maintaining the Program
Maintenance
Making changes after program is put into production
Common first programming job
Maintaining previously written programs
Make changes to existing programs
Repeat the development cycle
24. Two Truths & A Lie
Hardware is the equipment, or the devices, associated with a computer. Software is computer
instructions. (T)
The grammar rules of a computer programming language are its syntax. (T)
You write programs using machine language, and translation software converts the statements to a
programming language. (F)
25. Drawing Flowcharts
Create a flowchart
Draw geometric shapes that contain the individual statements
Connect shapes with arrows
Input symbol
Indicates input operation
Parallelogram
Processing symbol
Processing statements such as arithmetic
Rectangle
Output symbol
Represents output statements
Parallelogram
Flowlines
Arrows that connect steps
Terminal symbols
Start/stop symbols
Shaped like a racetrack
Also called lozenge
26. Two Truths and A Lie
A program with syntax errors can execute but might produce incorrect results.
Although the syntax of programming languages differs, the same program logic can be expressed
in different languages.
Most simple computer programs include steps that perform input, processing, and output.
27. Syntax
Compilers dont always know exactly what you mean, nor do they know what the proper correction
should be, but they do know when something is wrong with your syntax.
When writing a program, a programmer might need to recompile the code several times.
Remember, an executable program is created only when the code is free of syntax errors.
28. Two Truths and A Lie
Understanding the problem that must be solved can be one of the most difficult aspects of programming.
The two most commonly used logic-planning tools are flowcharts and pseudocode.
Flowcharting a program is a very different process if you use an older programming language instead of
a newer one.
29. Two Truths and A Lie
When you draw a flowchart, you use a parallelogram to represent an input operation.
When you draw a flowchart, you use a parallelogram to represent a processing operation.
When you draw a flowchart, you use a parallelogram to represent an output operation.
30. Pseudocode is Fairly Flexible
Instead of start and stop begin and end
Instead of writing input myNumber get or read myNumber
Instead of writing set myAnswer calculate or compute myAnswer = myNumber * 2
Instead of writing output myAnswer display, print or write myAnswer
31. After the flowchart or pseudocode has been developed, the programmer only needs to:
Buy a computer
Buy a language compiler
Learn a programming language
Code the program
Attempt to compile it
Fix the syntax errors
Compile it again
Test it with several sets of data
Put it into production
32. Loop: repetition of a series of steps
Infinite loop: repeating flow of logic with no end
33. Two Truths and A Lie
A program that contains an infinite loop is one that never ends.
A preselected value that stops the execution of a program is often called a dummy value or sentinel
value.
Many programming languages use the term fe (for file end) to refer to a marker that automatically acts
as a sentinel.
34. Use a keyboard to type program statements into an editor
Plain text editor
Similar to a word processor but without as many features
Text editor that is part of an integrated development environment (IDE)
Software package that provides an editor, compiler, and other programming tools
35.
Command line
Location on your computer screen at which you type text entries to communicate with the
computers operating system
Graphical user interface (GUI)
Allows users to interact with a program in a graphical environment
43.
44.
Numeric variable
Holds digits
Can perform mathematical operations on it
String variable
Can hold text
Letters of the alphabet
Special characters such as punctuation marks
Assign data to a variable
Only if it is the correct type
45.
Named constant
Similar to a variable
Can be assigned a value only once
Assign a useful name to a value that will never be changed during a programs execution
43 and Amanda
Magic number
Unnamed constant like 0.06
Purpose is not immediately apparent
Avoid this (use num salesTax = 0.06 instead)
46.
Assignment statement
set myAnswer = myNumber * 2
Assignment operator
Equal sign
Always operates from right to left
Valid
set someNumber = 2
set someNumber = someOtherNumber
Not valid
set 2 + 4 = someNumber
The process of breaking down a large program into modules is called modularization.
You are not required to modularize a large program in order to make it run but,
Modularization provides abstraction
Modularization allows multiple programmers to work on a problem.
Modularization allows you to reuse your work more easily.
52.
Abstraction
Paying attention to important properties while ignoring nonessential details
Selective ignorance
Newer high-level programming languages
Use English-like vocabulary
One broad statement corresponds to dozens of machine instructions
Modules provide another way to achieve abstraction
With Abstraction
Do laundry
Call Aunt Nan
Start term paper
Without Abstraction
Pick up laundry basket
Put laundry basket in car
Drive to laundry mat
57.
Hierarchy chart
Shows the overall picture of how modules are related to one another
Tells you which modules exist within a program and which modules call others
Specific module may be called from several locations within a program
Planning tool
Develop the overall relationship of program modules before you write them
Documentation tool
Program comments
Written explanations
Not part of the program logic
Serve as documentation for readers of the program
Syntax used differs among programming languages
Flowchart
Use an annotation symbol to hold information that expands on what is stored within another
flowchart symbol
Prompt
Message displayed on a monitor to ask the user for a response
Used both in command-line and GUI interactive programs
Echoing input
Repeating input back to a user either in a subsequent prompt or in output