Professional Documents
Culture Documents
Introduction to
Computer Science I
Topic 1
Introduction to
Programming
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-1
Introduction to Programming
PROGRAMMING
PROCESS
EXAMPLE
Driving a Car
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-2
Introduction to Programming
Programming Process Example
Types of Contol
Sequential: Step-by-step
Selection: One of choices
Repetition: Repeated step
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-3
Sequential
One Step at a Time
Begin
Compute
Compute
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
Compute
Compute
End
JAR 1-4
Selection
One of Choices
Begin
Condition
Compute
End
Compute
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-5
Repetition
Repeated Step
Begin
Initialize
Compute
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
Update
Condition
End
JAR 1-6
Composition
Replace any Sequential Step
with
Two or more Sequential Steps,
a Selection, or a Repetition
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-7
Composition
Example
Guess a Number Between 0 and 100
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-8
Example
Guess a Number Between 0 and 100
Start
Set
Set
G to 50
C to 25
G = N?
G < N?
Subtract
C from G
Done:
Add
Divide
G is N
C to G
C by 2
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-9
Introduction to Programming
Programming Steps
1.
2.
3.
4.
5.
6.
Specification:
Description of the Task
Analysis:
Understanding the Task
Test Data:
Complete Set of Tests
Design:
Data and Algorithms
Implement:
Write the Program
Test and Debug: Make It Work
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-10
Algorithm Design
Tools
Flow Charts
Pseudocode
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-11
Algorithm Design
Flow Charts
Graphical Symbols
Easy to Visualize Flow
Difficult to Draw in Comments
Examples: Flow of Control Diagrams
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-12
Algorithm Design
Set
G to 50
C to 25
G = N?
G < N?
Subtract
C from G
Done:
Add
Divide
G is N
C to G
C by 2
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-13
Algorithm Design
Pseudocode
Structured Natural Language
Easy to Understand
Easy to Use in Comments
Example: Following Program Comments
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-14
Algorithm Design
Pseudocode Example
Guess a Number Between 0 and 100
1.
2.
3.
4.
JAR 1-15
Algorithm Design
Pseudocode Example
// Get the input values
// If the input values are not valid
// Display an error message
// Else
// For each input value
// Calculate the result
// Display the results
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-16
Programming Basics
Programming Practices
Design before implementing
Use comments liberally
Develop and test incrementally
Reuse functions
Start early
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-17
Programming Basics
Learning to Program
Program
Program
Program
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-18
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-19
Machine Language
JAR 1-20
Assembly Language
JAR 1-21
High-Level Language
Mathematical Language
Consists of Mathematical Statements
Machine Independent (Mostly)
Highest Level (Except Natural)
Limited Capability: Not All Operations
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-22
Uses
Machine Language:
Assembly Language:
High-Level Language:
Natural Language:
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
Debugging Only
Critical Tasks
Most Common
Not Yet
JAR 1-23
Assembly:
LOAD
ADD
STORE
Machine:
210A = 0010000100001010
410B = 0010000100001011
310C = 0011000100001100
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
First
Second
Sum
JAR 1-24
Compilation Process
High-Level Machine
1.
2.
3.
4.
5.
6.
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-25
Compilation Process
C++
Files
Obj/Asm
Files
Compiler
Obj
Asm
Source
Files
Assembler
Object
Files
Library
Files
Linker
Execute
File
Loader
Main
Memory
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-26
Compilation Process
User Steps
1.
2.
3.
4.
5.
6.
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-27
High-Level Languages
Many Types
FORTRAN:
First, Mathematical
COBOL:
Early, English-Like
LISP,SNOBOL: Special-Purpose
ADA:
DOD Standard
C:
Operating System Implementation
C++: Object-Oriented Programming
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-28
High-Level Languages
C/C++
C:
C++: Enhancement of C
Object-Oriented Programming
Language for this Course
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-29
Function
A single unit of computation
Accomplishes one thing
May use other functions
May produce a result
Sequence of statements
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-30
Program
Implementation of a complete solution
Solves a specified problem
Includes input and output
May use functions
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-31
C++ Basics
Keywords/Identifiers
Requirements
Conventions
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-32
C++ Basics
Keywords/Identifiers
Identifiers:
Keywords:
Examples:
Invalid:
Names of data
Identifiers reserved for
specific language use
x, data1, myName, ab123c,
big_variable, v1234
3z, XY 3, #a3
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-33
C++ Basics
Data Types
Hardware
Integer:
Real:
int
float Limited precision
double Extended precision
Character: char One character only
Software
Logical:
bool
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
True or False
JAR 1-34
C++ Basics
Integer Data
Discrete values
Counting numbers
Positive, negative, or zero
Values specified by decimal digits
No decimal point
Examples: 24, -80, 0, 16
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-35
C++ Basics
Real Data
Continuous values
Positive, negative, or zero
Values specified by decimal digits
Optional decimal point
Optional scientific notation
Examples: 3.14, -25.8, 0, 123, -3.45e8
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-36
C++ Basics
Boolean Data
JAR 1-37
C++ Basics
Character Data
JAR 1-38
C++ Basics
String Data
JAR 1-39
C++ Basics
JAR 1-40
C++ Basics
JAR 1-41
C++ Basics
Literals
JAR 1-42
C++ Basics
Literal Examples
Integer:
Float:
Double:
Long Dbl:
Character:
String:
80
8.42F, 8.42f, 8.42E2F, 8.42e2f
16.24, 1.624E1, 1.624e1
8.42L, 8.42l, 8.42E2L, 8.42e2l
'*'
"String Literal"
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-43
C++ Basics
Escape Sequences
Characters with special meaning
Specified in C++ with preceding backslash
Limited set
\n
\'
\"
\\
\0
Newline
Single Quote
Double Quote
Backslash
Character String (Null) Terminator
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-44
C++ Basics
Data Declaration
Must be declared before use
Give compiler information about the data
Name
Type
Size (Not for elementary data types)
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-45
C++ Basics
int
int
float
double
bool
char
i;
x1, x2, abc;
f1, volts, power;
weight, v3, inp;
state;
ch, letter;
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-46
C++ Basics
Data Initialization
Must be initialized before first use
Two ways to initialize
Declaration
Assignment
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-47
C++ Basics
int
int
float
double
bool
char
i = 0;
x1 = 3, x2 = -5, abc = 3 * 5 + 9;
f1 = 0, volts = 120.5, power = 0.1;
weight = 100, v3 = 0.03, inp = -2;
state = false;
ch = '*', letter = 'X';
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-48
C++ Basics
Arithmetic Operators
Unary syntax
operator Operand: -value
Binary syntax
Operand operator Operand: a + 5
Parentheses
(number)
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-49
C++ Basics
Arithmetic Operators
+
*
/
%
Addition
Subtraction
Multiplication
Division
Modulus, Remainder
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-50
C++ Basics
Division
JAR 1-51
C++ Basics
Data Hierarchy
int/char
float
double
long double
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-52
C++ Basics
++ Preincrement/Postincrement
Preincrement: Increment before use
Postincrement: Increment after use
--
Predecrement/Postdecrement
JAR 1-53
C++ Basics
Expressions
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-54
C++ Basics
Expression Examples
var1
item1 * 5
a * (b + c)
0.5 * base * height
x * (p * q / (m n) + t)
var = (num / div) * div
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-55
C++ Basics
JAR 1-56
C++ Basics
C++ Statements
Terminated by semicolon
One action
Assignment
Input/Output
Control
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-57
C++ Basics
Assignment Statement
JAR 1-58
C++ Basics
Assignment Statement
JAR 1-59
C++ Basics
Assignment Examples
JAR 1-60
C++ Basics
JAR 1-61
C++ Basics
Console Input/Output
JAR 1-62
C++ Basics
JAR 1-63
C++ Basics
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-64
C++ Basics
JAR 1-65
C++ Basics
JAR 1-66
C++ Basics
JAR 1-67
C++ Basics
JAR 1-68
C++ Basics
JAR 1-69
C++ Basics
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-70
C++ Basics
JAR 1-71
C++ Basics
JAR 1-72
C++ Basics
JAR 1-73
C++ Basics
String Input
getline(cin, string);
Reads one complete line
Stops on first character of next line
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-74
C++ Basics
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-75
C++ Basics
Libraries
JAR 1-76
C++ Basics
Namespaces
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-77
C++ Basics
Comments
JAR 1-78
C++ Basics
Comment Examples
JAR 1-79
C++ Basics
Programming Style
JAR 1-80
UPDATES
Constant & Variable Examples
More Example Programs
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
JAR 1-81