CS208 Software programming
10/3/2015
1
Introduction
Software
Instructions that tell the computer what to do
(ie. a program)

10/3/2015
2
Languages

Machine Language –
zeros and ones

•
•
•
Assembly Languages Use mnemonics to operations and sometimes memory
locations
Each is specific to a particular hardware architecture
Translated by an Assembler
10/3/2015
3
Languages

High Level Languages -
•
Use English-like statements to instruct the
computer what to do.
•
Are more portable (will run on different kinds of
hardware)
Translated by a compiler (produces object code)
•
•
Linked via linker to other code and library
routines (produces executable code)
10/3/2015
4
Languages
High Level Languages –
Examples:
- FORTRAN
- scientific/engineering, math
- COBOL
- business
- BASIC
- education and PCs
- Pascal
- education
- C
- Application development
C++
- Application development
Java
- Application development

10/3/2015
5
Structured Programming




Program is subdivided into modules (one logical
thought, approx. one page long)
Each page is organized into recognizable
paragraphs, using indentation to show nesting and
subordinate clauses.
Embedded comments describe the function of
each variable and purpose of each module.
Straightforward, easily readable code is preferred
over slightly more efficient, but obtuse, code.
10/3/2015
6
Structured Programming



Final program is created via top-down
design.
Main code with empty modules, slowly
refined to lowest level
Keeps programmer from being
overwhelmed by job, and allows easy
division of work (modules assigned to
different programmers)
10/3/2015
7
Benefits of Structured
Programming:
•
•
•
•
More readable
Easier to maintain
More likely to be correct on first run
Easier to "prove" by systematic program
verification
10/3/2015
8
Eight-Statement Pascal








program
const
var
readln
writeln
assignment (:=)
if-then-else
while-do
10/3/2015
to name the program
to define constants
to declare variables
to read input
to write output
to perform calculations
to make decisions
to loop
9
What a Pascal program looks
like:
program Warning (output);
{header - required}
begin
{required}
writeln ('DANGER!!')
end.
10/3/2015
{required}
10
Declarations
(Constants/Variables)
Primitive (Basic) Data Types
Real, Integer, and Char



10/3/2015
Integer - whole numbers
{No commas in numbers}
Real - numbers with fractional parts
{Must have digit before & after decimal pt}
Char - a single ASCII character
{enclosed in single quotes}
11
Constants
Value known prior to start of program AND
value doesn't change
Format:
10/3/2015
const
constant-name = value;
12
Constants
Examples:
const
Pi = 3.14;
TaxRate = 0.28;
MaxItems = 50;
BestGrade = 'A';
10/3/2015
13
Variables
allocates memory cell, types it, gives it a name
Format:
var variable-list: type;
Examples:
var
Initial: char;
I, Count: integer;
GPA: real;
10/3/2015
14
Variables

NOT case-sensitive.

good identifiers make code "SelfDocumenting"

must begin with a Letter, followed by
letters/digits
10/3/2015
15
Executable Statements

Between "begin" and "end" (separated by
semicolons ;)

Statements can take up more than one line.
Extra blanks ignored - use for readability.
Use comments {}


10/3/2015
16
Assignment Statements



assigns a value to an identifier
id := 5; (id "gets" 5)
types must be compatible
Operators: + - * / DIV MOD translate
several math formulas. (/ for real nums
only, DIV & MOD for integers)
10/3/2015
17
Assignment Statements
Swap Example:
temp := a;
a := b;
b := temp;
Increment Example:
10/3/2015
a := a + 1;
18
I/O Procedure statements
Writeln (output list);
o
procedure call to write output to standard output
device (w/CR)
o Writeln; writes a blank line with no output on it.
Examples: Writeln ('Hello');
Writeln ('Please enter a number:');
Writeln ('The answer is', sum);
10/3/2015
19
I/O Procedure statements
Readln (input list);
procedure call to get input from standard input
device
Examples:
10/3/2015
Readln (Pay);
Readln (Num1, Num2);
20
Semi-Colons (;)


used to separate statements
do not use AFTER Begin or BEFORE End
10/3/2015
21
Example
Write a program that computes the age of the user.
10/3/2015
22
10/3/2015
23
10/3/2015
24
Example
program CalcAge (input, output);
{Computes and prints age}
const
CurYr = 2001;
var
BirthYr, Age: integer;
begin
writeln ('Enter year of birth: ');
readln (BirthYr);
Age := CurYr - BirthYr;
writeln ('Your age after this year''s birthday is ', Age)
end.
10/3/2015
25
Boolean Expressions
A relational operator tests the relationship
between two expressions and returns a
Boolean (TRUE or FALSE) result.

Syntax: (<expr> <relop> <expr>)
10/3/2015
26
Boolean Expressions
operator | example | result
-----------------------------------------=
A=B
TRUE if A is equal to B
<>
A <> B TRUE if A is not equal to B
<
A<B
TRUE if A is less than B
<=
A <= B TRUE if A is less than or equal to B
>
A>B
TRUE if A is greater than B
>=
A >= B TRUE if A is greater than or equal to B
10/3/2015
27
If Selection Structure
Syntax:
One Alternative
if condition then statementT
{do statement if condition is TRUE}
Two Alternatives
if condition
then statementT
{do if TRUE}
else statementF
{do if FALSE}
10/3/2015
28
If Selection Structure
Examples:
if Age > 18 then
writeln ('Can Vote');
if Divisor <> 0.0 then
Answer := Num / Divisor;
if MorD = 'M' then
writeln ('Hi Mom');
10/3/2015
if MorD = 'M'
then writeln ('Hi Mom')
else writeln ('Hi Dad');
29
If Selection Structure
if Num >= 0
then writeln ('Positive')
else writeln ('Negative');
if Temp >= 60
then writeln ('Tennis Anyone?')
else writeln ('Too cold for tennis.');
10/3/2015
30
If Selection Structure
program CalcAge;
{Computes and prints age}
{$Apptype Console}
const
CurYr = 2001; LastMon = 1;
var
BirthYr, BirthMon, Age: integer;
begin
writeln ('Enter year of birth: '); readln (BirthYr);
writeln ('Enter month of birth: '); readln (BirthMon);
Age := CurYr - BirthYr;
if BirthMon > LastMon then
Age := Age - 1;
{Subtract 1 if birthday has not yet occurred this year}
writeln ('Your age is ', Age);
writeln(‘Push return to exit’);
readln;
end.
10/3/2015
31
If Selection Structure
Exercise:
Write a program that reads in 3 integers and outputs
the smallest.
example:
13
8
12
The smallest number is 8
10/3/2015
32
If Selection Structure
program SmallNum;
{$Apptype Console}
var
Num1, Num2, Num3, Smallest: integer;
begin
Writeln ('Enter three numbers: '); Readln (Num1, Num2, Num3);
if Num1 < Num2 then
Smallest := Num1
else Smallest := Num2;
if Num3 < Smallest then
Smallest := Num3;
Writeln ('Smallest = ', Smallest:1);
Writeln(‘Press return to exit’); Readln;
end.
10/3/2015
33
Conditional Loop Structure
Conditional Loop Structure: the WHILE statement
Syntax:
WHILE (boolean-expression) DO
begin
statement1
:
statementN
end;
{NOTE: begin/end not needed if only one statement}
10/3/2015
34
Conditional Loop Structure
Example #1: Display multiples of 5, from 5 to 50.
Count := 1;
WHILE Count <= 10 DO
begin
Mult := 5 * Count;
Writeln (Mult);
Count := Count + 1
end;
10/3/2015
35
Conditional Loop Structure
Example #2:
Error check input, to be sure a positive number is entered.
Writeln ('Enter a positive number:');
Readln (Num);
WHILE Num < 0 DO
begin
Writeln ('Invalid entry. Try Again.');
Readln (Num)
end;
10/3/2015
36
Conditional Loop Structure
Student Exercise:
Write a WHILE loop that will add a series of positive
integers as they are entered by the user. Stop adding and
display the total when the user enters a negative number.
Example:
10
20
-1
Total is 30
10/3/2015
37
Conditional Loop Structure
Sum := 0;
Num := 0;
WHILE Num >= 0 DO
begin
Sum := Sum + Num;
Writeln ('Enter a positive number (negative to exit):');
Readln (Num)
end;
Writeln ('The total is ', sum);
10/3/2015
38
Descargar

Document