Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine.

Similar presentations


Presentation on theme: "Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine."— Presentation transcript:

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. 8d4c240483e4f0ff 71fc5589e55183ec 10c745f830000000 8345f8078b45f883 c410595d8d61fcc3 A computer can understand this: People used to program this way!!

2 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 8d4c240483e4f0ff 71fc5589e55183ec 10c745f830000000 8345f8078b45f883 c410595d8d61fcc3 i=48 i=i+7 return i A programming language must have:

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. 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')

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. >> [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]

10 Relational Operators Relational operators compare types and give a true or false 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.

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 => 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)

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] -> [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 & l2 ans = 0 0 1 0 1 0 Example in MATLAB:

13 “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.

14 Expressions Values and operators combine to make expressions. The meat of a program is the expressions it contains. >> pi*10^2 ans = 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

15 Variables Variables have names, types, and values. >> radius = 10; >> pi*radius^2 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. Variables are assigned using the assignment operator, =. A variable of a given type can be assigned any value that has that same type.


Download ppt "Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine."

Similar presentations


Ads by Google