intro to programming problem 1: computers have to be told exactly what do to. problem 2: computers...
TRANSCRIPT
![Page 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](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/1.jpg)
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.
8d4c240483e4f0ff71fc5589e55183ec10c745f8300000008345f8078b45f883c410595d8d61fcc3
A computer can understand this:
People used to program this way!!
![Page 2: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/2.jpg)
Intro to Programming
Programming languages are the solution.
● 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
8d4c240483e4f0ff71fc5589e55183ec10c745f8300000008345f8078b45f883c410595d8d61fcc3
i=48i=i+7return i
A programming language must have:
![Page 3: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/3.jpg)
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.
![Page 4: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/4.jpg)
Vocabulary
In order to talk about programs, we need to know somevocabualry.
These are some common words that have specific meaningsin the programming world.
●Types●Constants●Operators●Expressions●Variables
A specific language can be almost completely defined justby knowing how it treats these 5 concepts.
![Page 5: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/5.jpg)
Types
Programs deal with quantities, but not all quantities arethe same.
Inherently different quantities are assigned differentTypes.
The difference between a 1 and an 'a' is their type.
Simple/Primitive Types●Integer: 1●Float: 1.2●Character: 'a'●Boolean: true or false●String: “abc” (sometimes)
Compound Types●Array: [1,2,3]●String: “abc” (sometimes)●Tuple: (1,'a')
![Page 6: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/6.jpg)
Constants
Unchangeable values are called Constants.Boring but useful.
7 and 'h' are anonymous constants, usually calledliterals.
You can guess what named constants are...
In MATLAB, pi is a named constant.
Named constants are usually just called constants.
![Page 7: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/7.jpg)
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
![Page 8: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/8.jpg)
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
![Page 9: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/9.jpg)
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.
>> [1,2,3,4]*[2;2;2;2]ans = 20
>> [1,2,3,4].*[2,2,2,2]ans = 2 4 6 8
Matrix multiplication:
Elelement-by-element multiplication:
1*2 + 2*2 + 3*2 + 4*2
[1*2, 2*2, 3*2, 4*2]
![Page 10: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/10.jpg)
Relational Operators
Relational operators compare types and give a true orfalse answer.
3 > 9 = false
● >: Greater than● >=: Greather than or equal● <: Less than● <=:Less than or equal● ==: Equal (most languages use 2 equal signs)● !=,<>,~=: Not equal
● Most languages use the first form, some the second, MATLAB uses the third.
![Page 11: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/11.jpg)
Logical Operators
Logical operators combine boolean values and returnanother 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 => false)● ||: or (true || false => true)● !: not (!true => false)
● bitwise: operate on binary representations● &: and (1110 & 1011 -> 1010)● |: or (1110 | 1011 -> 1111)● ^: xor (1110 ^ 1011 -> 0101)● ~: not (~1110 -> 0001)
![Page 12: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/12.jpg)
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] -> [1,0,1,0]
Also, MATLAB uses ~ for negation, never !.
>> x = [3,1,4,1,5,9]x = 3 1 4 1 5 9>> l1 = (x>3)l1 = 0 0 1 0 1 1>> l2 = (x<9)l2 = 1 1 1 1 1 0>> l1 & l2ans = 0 0 1 0 1 0
Example in MATLAB:
![Page 13: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/13.jpg)
“Short Circuit” Operators
&& and || are usually “short circuit” operators.
This means that an answer is returned as soon as possible, without evaluating everything.
x ~= 0 && (5/x > 2)
This can be used to your advantage, the following will not produce an error when x=0:
e.g. true || ? -> true
The value of ? doesn't matter, the answer is always true.
![Page 14: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/14.jpg)
Expressions
Values and operators combine to make expressions.
The meat of a program is the expressions it contains.
>> pi*10^2ans = 314.1593
But, this is only for one size circle. To be really useful, we need...
Example: The area of a circle with radius 10
![Page 15: Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine](https://reader036.vdocuments.mx/reader036/viewer/2022080916/56649e4f5503460f94b46467/html5/thumbnails/15.jpg)
Variables
Variables have names, types, and values.
>> radius = 10;>> pi*radius^2ans = 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.
Variables are assigned using the assignment operator, =.
A variable of a given type can be assigned any value thathas that same type.