Intro to Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Intro to Programming

Description:

Problem 2: Computers only understand 'Machine language' ... is translated every time it is run ... In many deep ways, programming is about abstraction. ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 16
Provided by: atis3
Category:

less

Transcript and Presenter's Notes

Title: Intro to Programming


1
Intro to Programming
Problem 1 Computers have to be told exactly what
do to.
Problem 2 Computers only understand Machine
language.
Problem 3 Machine language is pretty much
impossible to use.
A computer can understand this
8d4c240483e4f0ff 71fc5589e55183ec 10c745f830000000
8345f8078b45f883 c410595d8d61fcc3
People used to program this way!!
2
Intro to Programming
Programming languages are the solution.
A programming language must have
  • Syntax that we can understand
  • A program for translating this to machine
    language
  • Compiler if the program is translated once
  • Interpreter if the program is translated every
    time it is run

8d4c240483e4f0ff 71fc5589e55183ec 10c745f830000000
8345f8078b45f883 c410595d8d61fcc3
i48 ii7 return i
3
Intro to Programming
The Tao gave birth to machine language. Machine
language gave birth to the assembler. The
assembler gave birth to the compiler. Now there
are ten thousand languages. -- The Tao of
Programming
Some common languages
  • Fortran
  • C/C
  • Java
  • Perl

But we will focus on MATLAB, with examples from
others.
4
Vocabulary
In order to talk about programs, we need to know
some vocabualry. These are some common words
that have specific meanings in the programming
world.
  • Types
  • Constants
  • Operators
  • Expressions
  • Variables

A specific language can be almost completely
defined just by knowing how it treats these 5
concepts.
5
Types
Programs deal with quantities, but not all
quantities are the same. Inherently different
quantities are assigned different Types.
The difference between a 1 and an 'a' is their
type.
Compound Types
Simple/Primitive Types
  • Integer 1
  • Float 1.2
  • Character 'a'
  • Boolean true or false
  • String abc (sometimes)
  • Array 1,2,3
  • String abc (sometimes)
  • Tuple (1,'a')

6
Constants
Unchangeable values are called Constants. Boring
but useful.
7 and 'h' are anonymous constants, usually
called literals.
You can guess what named constants are... In
MATLAB, pi is a named constant. Named constants
are usually just called constants.
7
Operators
We learn about operators in elementary school ,
-, , .
Operators transform one or more values into some
other value. If it operates on one value, the
operator is unary. Two values, then binary.
There are different flavors or operators
  • Arithmetic
  • Relational
  • Logical

8
Arithmetic Operators
Arithmetic operators do some mathematical
computation. Types are typically preserved
throughout. (Integer) (Integer) (Integer)
  • - (unary) Negation
  • Addition
  • - (binary) Subtraction
  • Multiplication
  • / Division
  • Modulus
  • Exponentiation

9
Arithmetic Ops. in MATLAB
Since everything in MATLAB is a matrix, it has a
few extra operators. Operators preceded by a '.'
do element by element operations.
Matrix multiplication
gtgt 1,2,3,42222 ans 20
12 22 32 42
Elelement-by-element multiplication
gtgt 1,2,3,4.2,2,2,2 ans 2 4 6
8
12, 22, 32, 42
10
Relational Operators
Relational operators compare types and give a
true or false answer. 3 gt 9 false
  • gt Greater than
  • gt Greather than or equal
  • lt Less than
  • ltLess than or equal
  • Equal (most languages use 2 equal signs)
  • !,ltgt, Not equal
  • Most languages use the first form, some the
    second, MATLAB uses the third.

11
Logical Operators
Logical operators combine boolean values and
return another boolean. Most languages follow
the C paradigm, so we will look at that, as well
as how MATLAB is different.
  • C has both strictly logical and bitwise
    operators
  • logical
  • and (true false gt false)
  • or (true false gt true)
  • ! not (!true gt false)
  • bitwise operate on binary representations
  • and (1110 1011 -gt 1010)
  • or (1110 1011 -gt 1111)
  • xor (1110 1011 -gt 0101)
  • not (1110 -gt 0001)

12
Logical Operators
Matlab has element-wise logical operators with
the same syntax as C's bitwise operators. e.g.
1,1,1,0 1,0,1,1 -gt 1,0,1,0 Also, MATLAB
uses for negation, never !.
Example in MATLAB
gtgt x 3,1,4,1,5,9 x 3 1 4 1 5 9 gtgt l1
(xgt3) l1 0 0 1 0 1 1 gtgt l2 (xlt9) l2 1 1 1 1
1 0 gtgt l1 l2 ans 0 0 1 0 1 0
13
Short Circuit Operators
and are usually short circuit
operators. This means that an answer is returned
as soon as possible, without evaluating
everything.
e.g. true ? -gt true The value of ? doesn't
matter, the answer is always true.
This can be used to your advantage, the following
will not produce an error when x0
x 0 (5/x gt 2)
14
Expressions
Values and operators combine to make
expressions. The meat of a program is the
expressions it contains.
Example The area of a circle with radius 10
gtgt pi102 ans 314.1593
But, this is only for one size circle. To be
really useful, we need...
15
Variables
Variables have names, types, and values.
A variable of a given type can be assigned any
value that has that same type.
Variables are assigned using the assignment
operator, .
gtgt radius 10 gtgt piradius2 ans 314.1593
In this case the last expression is the area of
any circle. In many deep ways, programming is
about abstraction. We will see it is also about
modularity by introducing Functions.
Write a Comment
User Comments (0)
About PowerShow.com