Starting Out with Programming Logic and Design (2nd Edition) |
- Designing a Program
Programmers, when given an assignment will very rarely just start writing the program. They will work on the design of the program. They will then start to write the program in the language desired, being careful not to break the rules (or syntax) of the programming language. Any syntax errors will cause the program not to compile to an executable program or be interpreted through an interpreter. Even if the rules are not broken, there may be logic errors (mistakes in the results) that need to be fixed. For either type of error, the programmer must remove the bugs, called debugging the code. These are what is done in the Program Development Cycle as follows:
A) Design the program
B) Write the code
C) Correct syntax errors
E) Test the executable code
F) Debug the code
G) go back to A and start over again till it is working.
This book focuses on design. With a bad design, a program will be like a house with a bad foundation. It will fall apart a lot.
Designing a Program
Understanding the task that the program is to perform
Talk to the end user (customer) and find out what it is that they want the program to do. This may take several interviews. Gather the information and make a list of the requirements needed. Verify this with customer and then on to the next step.
Determine the steps that must be taken to perform the task
You must now break it down into steps, called algorithms, that are a logical way for the program to progress.
PseudoCode
Since syntax problems can cause problems, programmers will often write down the program steps in computer like languages. Doing this lets them think the problem through without worrying about misspelled words or missed punctuation. Once done, the code can then be translated to real programming code.
Flowcharts
Another method for designing the program is to draw it out with a flowchart. There are three basic shapes (with others used for other things): ovals to indicate the start and end, parallelograms that indicate input or output, and rectangles that indicate a process being done. Lines with arrows connect the boxes to indicate the flow of the program.
- Output, Input and Variables Displaying Screen Output
The most common thing in a program is to display output to the user. We will use the term DISPLAY for this books usage. A simple program would be to display the name, address and city/state/zip of a person.
Sequence Structure
When done this would flow from beginning to end and this type of layout would be a sequence structure. There are three basic structures, we will learn others later.
Strings and Literals
Data is represented in many ways. If it is a series of alpha-numeric characters it is often referred to as a string. If it is hard coded into the program it is a string literal.
Variables and Input
When a program needs to store information it will use a variable, a location in memory that is given an easy to remember name. We will be using INPUT for this in pseudocode with the understanding that when the user types it in and hits enter the information will be stored in the variable name provided.
Variable Names
Though rules may vary from language to language, basic rules for variable names are:
One word – no spaces
No punctuation characters – only alphanumeric
Usually 1st character cannot be a number
In general use variable names that will make sense to you later (pay v. p). It is also a good idea to break names like grosspay up into gross_pay or grossPay to make them easier to read by humans.
Displaying Multiple Items With One Display Statement
Most programming languages will allow you to display several pieces of information with only one line. For out purposes we will pseudocode this with a comma but it works differently in each programming language.
String Input
To code input from a source we will use INPUT followed by a variable name.
Prompting the User
Rather than make a user guess what they are to do, you will tell them what to do with a prompt (DISPLAY) and then follow it up with an INPUT. This makes it user friendly. - Variable Assignments and Calculations
Variable Assignments
Often we will need to put information into a variable without user input. For this we will use the SET statement. (SET dollars = 2.75) This is known as an assignment statement. We call them variables because they can contain different information as we go along. In fact we can reassign them as needed.
Performing Calculations
The following are types of mathematic operations done in programming:
Symbol Operator Description + addition add two numbers - subtraction subtract one number from the other * multiplication multiply two numbers / division divide one number by another and give the whole number result MOD Modulus Divide one number by another and give the remainder ^ Exponent Raise a number to a power
The Order of Operations
All math in programming is done in the following order and in left to right order:
Parenthesis
Exponential
Multiplication, division and modulus
Additions and subtractions
Grouping With Parenthesis
If there is any question, use parenthesis to make it clear what is to be done in what order.
Advanced Arithmetic Operators: Exponent and Modulus
Exponentials allow you to raise a power (square would be a power of 2, cube would be 3, etc) and is shown by the ^ symbol. So to cube a variable bob it would be bob^3. Modulus is doing a division problem and just taking the remainder. Simply use is if a number is odd or even. If I do bob MOD 2 and get 0 it is an even number. If I get a 1 it is an odd number.
Converting Math formulas to Programming Statements
Unlike math class 2XY would be written 2 * X * Y . The layout would be different for division as well. - Variable Declarations and Data Types
In order to use a variable in most programming languages you need to define what it is or DECLARE. If you declare it as a number and then try to put a string in it, then you will get an error. The basic types of variables (and they do vary from language to language are:
Integer A whole number with no fractions Real A whole number or one with a fractional part String A series of alphanumeric characters.
We will use for pseudocode something like DECLARE String name
Declaring Variables Before Using Them
In order for you to use a variable to store information in, it must be declared before it is used. If not, you will get an error. If there are many variables it often is done at the beginning of the program as a group. This is a programming convention (or style) and does not necessarily have to be done.
Variable Initialization
It is possible when Declaring a variable to also assign a value to it in most programming languages.
Uninitialized Variables
If you declare a variable and then do not use it, the variable is uninitialized. Because there is no way of knowing what is in that memory location, we will get random results, usually resulting in an error.
Numeric Literals and Data Type Compatibility
Similar to the string literal is a numeric literal. It is a number hard coded into the program, not one that is asked for from the user. One must be careful in declaring and then putting information in. For example, declaring a variable eggs to be a integer you could not put a value of 3.75 in. The real number has a fraction and would error out on the integer type.
Integer Division
One needs to be careful when dividing an integer by an integer. Often the programming language will throw the fractional part out. - Named Constants
To avoid errors it is a good idea to use a constant variable than a number in a program. If a number is used you are not sure what that number means. Also if it is used in several places you have to go in and change it at each one. So in place of 0.068 you might declare CONSTANT real INTEREST_RATE = 0.068 . This tells you what it is as well allows you to change it in one place only. All capitals for the name is a programming convention. - Hand Tracking a Program
Hand tracking (or Desk Checking) is done by creating a table with a row for each line and a column for each variable. As you go through the program you would change the variables. If at any time you run into something unclear, you need to go back to the code and look for the problem. This will help with uninitialized variables talked about earlier. - Documenting a Program
There are two types of documentation. External documentation would be the manuals that are created to explain the use of the program as well as its features. Internal documentation is comments that are written into the program code. They are intended to be read by humans looking at the code and will be ignored by a complier or an interpreter. The book uses // as a standard to display comments.
Block Comments and Line Comments
Block comments are a group of comments that will explain several lines or what the program is doing. Line comments are ones that are either before or after a line (usually before) or are part of the line itself.
It is also a good idea to use blank lines to separate different blocks of code.
No comments:
Post a Comment