A Very Brief Introduction to Programming - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

A Very Brief Introduction to Programming

Description:

1. Get out of bed. 2. Take off pajamas. 3. Take a shower. 4. Put on suit. 5. Eat breakfast ... 1. Eat breakfast. 2. Put on suit. 3. Get out of bed. 4. Take a ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 38
Provided by: robert365
Category:

less

Transcript and Presenter's Notes

Title: A Very Brief Introduction to Programming


1
A (Very) Brief Introduction to Programming
2
Programming with LeJOS
  • LeJOS (Java programming language
    specifically for Lego Robots)
  • Object Oriented Programming (OOP) language
  • Pieces of code others have written can easily be
    reused or modified
  • Supports floating-point (decimal) numbers, and
    arrays
  • Allows FULL control over the RCX brick (access
    to buttons, screen, etc)
  • Allows multi-threading simultaneous
    execution of various parts of a program

Do I have to use LeJOS? YES!
3
Algorithms
  • Before writing a program, develop an outline, or
    algorithm of what it should do
  • An algorithm includes

1. The ACTIONS to execute
2. The ORDER in which these actions execute
1. Get out of bed
1. Eat breakfast
2. Take off pajamas
2. Put on suit
3. Take a shower
3. Get out of bed
4. Put on suit
4. Take a shower
5. Eat breakfast
5. Drive to work
6. Drive to work
6. Take off pajamas
4
Algorithm Development
  • Top Down Design
  • Big picture description of problem solution in
    sequential steps
  • Can be used on simple problems

Example Calculate elapsed time
1. Read current time
2. Subtract initial time from current time
3. Print elapsed time
  • Divide and Conquer Strategy
  • Use for non-simple problems
  • Continuously break down problem into smaller
    pieces
  • Use pseudocode or flowcharts to refine outline

- There are usually MANY possible solutions that
will work
5
Pseudocode
  • Fake Code
  • English-like statements (not actual programming
    language)
  • Normally only describes executable statements

- Cause specific actions to take place -
input, output, calculation
Example Calculate and display a persons age
Prompt user to enter date of birth
Input date of birth
Prompt user to enter todays date
Input todays date
Calculate age, store result
Display their age
6
Flowchart Symbols
module name
Start
Processing Block
I/O to computer memory
Decision
end/stop/exit
End or Stop
7
Pseudocode/Flowchart
Operation
Pseudocode
Flowchart Symbol
start main
main
Beginning of algorithm
calculate age
computation
age date - DOB
print age
print age
output
Is age lt18?
if age lt 18, then
decision
yes
no
stop main
End of algorithm
8
Structured Programming
  • Use simple control structures
  • Sequence
  • Steps performed one after another
  • Selection
  • Two sets of steps to follow
  • - one if condition is true, another if it is false
  • Repetition
  • Set of steps that are repeated as long as
    condition is true

9
Structured Programming
Selection
Sequence
yes
no
Repetition
branches
no
yes
loops
10
Selection Statements
  • if
  • if else

11
The if statement (flowchart)
no
no
is condition true?
is condition true?
yes
yes
Statement 1
Statement 1
Statement 2
Statement n
12
The if statement (syntax)
if(expression) statement
single statement executed if expression is true
if(expression) statement1
statement2 statement n
statements inside are executed if expression
is true
13
The if statement (examples)
if (x gt 10) Sound.beep()
Causes beeping sound if x gt 10
if (x gt 10) Sound.beep()
Sound.buzz()
Causes beeping sound AND buzzing sound if x gt 10
if (x gt 10) if (y lt 5)
Sound.beep()
Causes beeping sound if x gt 10 AND y lt 5
14
The if-else statement (flowchart)
yes
no
is condition true?
Statement 1
Statement 2
15
The if-else statement (syntax)
if (expression) Statement1 else Statement2

Statement1 executed if expression is true.
Statement2 executed if expression is false.
if (expression) statement1
statement2 statement3 else
statementA statementB
statement1, statement2, and statement3 are
executed if expression is true
statementA and statementB are executed if
expression is false
16
The if-else statement (example)
if (x 10) Sound.beep() else
Sound.buzz()
Causes beeping sound if x 10
Causes beeping sound if x is not 10
if (x 10) Sound.beep() else if (x 5)
Sound.buzz() else if (x 2)
Sound.honk() else sound.tweet()
Can combine multiple if-else statements to select
from a list of possible values
What would this flowchart look like?
17
Multiple if-else statements (flowchart)
yes
no
does x 10
no
yes
BEEP
does x 5
yes
no
BUZZ
does x 2
TWEET
HONK
18
Repetition Statements
  • while
  • do/while
  • for

19
The while statement (flowchart)
no
is condition true?
Note The statements that form a loop must
MODIFY objects that are used in the condition.
Otherwise, an infinite loop is created!
yes
Execute Statement1
Execute Statement2
20
The while statement (syntax)
while (condition) statement1
single statement executed while condition is true
while (condition) statement1
statement2 statement n
statements inside are executed while
condition is true
21
The while statement (example)
Causes beeping sound as long as bot.isBright() is
true
while (bot.isBright() ) Sound.beep()
int x 0 while (x ! 3 )
Sound.beep() x x 1
! means not equal to
How many beeps are there?
int x 0 while (x ! 0 )
Sound.beep() x x 1
How many beeps are there?
22
The do-while statement (flowchart)
Similar to while statement except condition is
tested at the END of the loop, instead of at the
beginning
Execute Statement
yes
  • ensures that the loop

is condition true?
will always be executed
at least once
no
23
The do-while statement (syntax)
do statement1 while (condition)
Statement1 executed first time through, then
while condition is true, it is executed again
do statement1 statement2
statement n while (condition)
statements inside are executed first time
through, then while condition is true, they are
executed again
24
The do-while statement (examples)
do Sound.beep() while
(bot.isBright() )
Beeps at least once, then causes beeping sound
as long as bot.isBright() is true
int x 0 do Sound.beep() x x
1 while (x ! 3 )
! means not equal to
How many beeps are there?
int x 0 do Sound.beep() x x
1 while (x !0)
How many beeps are there?
INFINITE LOOP!!!
25
The for statement
  • Use for loops that increment or decrement by the
    same amount each
  • time through the loop
  • Requires 3 expressions

1. Initialize the loop-control object
2. Specify the condition that must be true to
continue the loop repetition
3. Specify the modification to the loop-control
object that follows the execution of the
statement block
26
The for statement (flowchart)
initalize
test
increment/ decrement
true
statement(s)
statement(s)
27
The for statement (syntax example)
for (initialization test counter increment)
statements
for (int j 1 j lt 5 j) Sound.beep()
1. Initialize the loop-control object j is an
integer, and it is assigned the value 1
2. Specify the condition that must be true to
continue the loop repetition j must be less
than 5
3. Specify the modification to the loop-control
object that follows the execution of the
statement block j is incremented by 1 ()
28
Object Oriented Programming
29
Types of Programming
  • Unstructured Programming
  • Procedural Programming
  • Modular Programming
  • Object-Oriented Programming

30
Unstructured Programming
  • One Main Program
  • Global Data

Main Program
Data
  • Main Program operates directly on Global Data

31
Procedural Programming
  • Combine returning sequences of statements into a
    single place
  • Program can be a sequence of procedure calls

Main Program
Data
Procedure 1
Procedure 2
Procedure 3
32
Modular Programming
  • Procedure of common functionality grouped
    together in modules
  • Each module can have its OWN data (state)

Program
Main Program
data
module 2
module 1
data data2
data data1
Procedure 3
Procedure 2
Procedure 1
33
Object-Oriented Programming
  • Web of interacting objects, each with its own
    state (data AND operations)
  • Objects interact by sending messages to one
    another
  • Each object has its own discrete code

- Easy to modify without affecting functionality
of other objects
34
Object-Oriented Programming (Example)
  • Program a robot to

1. Navigate a course by following a line
2. Locate and grab a ball
Find/Grab Ball
Follow a Line
Sense the Line
Sense the Ball
Follow the Line
Power the Claw
Power the Wheels
  • Each object can be modified, without affecting
    the other

- But, to accomplish overall goal, they must work
together
35
Object-Oriented Programming (Classes)
  • Classes define objects using methods
    (functionality) and fields (data)

- classes are like engineering drawings
Class Automobile
method accelerate
method steer
36
Object-Oriented Programming (Objects)
  • Objects PERFORM what the class tasks describe

- realization of a class
Class Automobile
object
object
Ferrari
Model T
- many cars can be made from the same engineering
drawings
- many objects can be made from the same class
37
Object-Oriented Programming (Inheritance)
  • A class can inherit the functionality (methods,
    data) of an existing

class, plus add additional methods and/or data
  • This property of OOP is called type extensibility
Write a Comment
User Comments (0)
About PowerShow.com