Step by step set of instructions to accomplish a task or solve a problem
Algorithm to sum a list of numbers: Start a Sum at 0 For each number in the list: Add the current sum to the next number Make the result the new Sum The result is the final value of Sum
Example: sum Let Sum = 0 Add 8 + 7 = 15 Add 0 + 5 = 5 Let sum = 15 Let sum = 5 Add 15 + 2 = 17 Add 5 + 3 = 8 Let sum = 17 Let sum = 8 Result:
Step by step set of instructions to accomplish a task or solve a problem
a word already defined in the language. Includes known symbols: These may NOT be re-defined by the programmer. etc.
a word defined by the programmer - must begin with a letter - remainder may be made up of letters, numbers or the underscore - NOTE: no spaces! - these are usually the Names of things
a word that IS (literally) a data value 3-7 12.5 Go Big Blue!! (character data between Single quotes)
anything on a line after a percent sign - Comments have NO EFFECT on a program and are not seen by the User. - Purpose: to add explanatory notes by programmers, for programmers.
for writing statements, defined by the language. Failure to follow these rules results in a and the program will not execute (run).
the of a statement What does it do? What does it command the computer to do?
for writing a statement in a program. - Failure to follow these rules has NO EFFECT on the program or what it does. - These rules make the program clearer to a programmer. - Examples: using comments, using meaningful identifier names, indentation and spacing.
for data in a program A Variable has a: - an identifier - a description of the data it contains (ex: number, string, matrix) - : which can change during execution.
% my first Matlab program width = 20; height = 30; area = width * height; disp(area); % the semicolons suppress Matlab clutter % when RUN (executed), this program % prints
Syntax: = must be a single variable, nothing else can be: - a literal - another variable (including ) - a function call - an expression made up of any of the above combined with operators ( etc.)
Semantics: = 1. is evaluated to one value. 2. The Value of is changed to the evaluated result of. Read as The value of is changed to Style: usually, all on one line.
Examples: x = 1 y = ((2+1)*(3/5))^2 x = x + 1 % think: Add 1 to x y = x * 2 z = sqrt(16) % sqrt() is the % Square Root function 2 + 3 = x % Syntax Error!
Standard: Power: 3^2 is 9 Modulus: mod(9,4) is 2 Remainder: rem(9,4) is 1 Negation: x = 2 y = -x y becomes -2 Round Off: round(2.5) is 3 Square Root: sqrt(16) is 4
Syntax: disp( ) Semantics: evaluates and displays the result on the command window. Then advances the cursor to the next line. Style: usually all on one line
Examples: disp(3) prints x = 6 disp(x*2) prints y = 'Hello World' disp(y) prints without the quotes!
The number must be converted to string, then appended to the string portion x = 6 disp( 'The answer is ' x ) Prints:
= input( ) Syntax: use the input() function on the Right Hand Side of an Assignment. is the question to display to the user, called a will hold the value entered by the user.
= input( ) Semantics: 1. The is printed in the command window. 2. Execution is suspended; it waits for the user to respond. 3. The user enters a value and presses. 4. The value entered is evaluated, and the result is assigned to the
Example: n = input( Enter a number: ) m = 2 * n disp([num2str(n), doubled is, num2str(m)]) If the user enters 3, it prints:
String Input is slightly different. When the user is expected to enter a string value, a second argument of 's' must be used in input: name = input('enter name: ' ) disp('hello ',name,'! How are you?')
It is usually a good idea to start all scripts by clearing the screen and workspace: clc; % clears the screen clear; % drops all variables
Term Algorithm Program Keyword Reserved Word Identifier Literal Comment Syntax Semantics Style Variable Data Type String Definition a step by step set of instructions to accomplish a task a step by step set of instructions to accomplish a task, written in a computer language word that has an already-defined meaning in a computer language. word that has an already-defined meaning in a computer language. word defined by a programmer, usually the Name of something. something that IS a value. Ex: 8 12.5 Hello anything on a line after a %. It has NO EFFECT on the program rules for writing statements in a computer language that must be followed for the computer to understand/interpret/execute the command. meaning of a statement in a program; what it causes the computer to do rules for writing statements in a computer language that have NO EFFECT on the program, but make the program clearer for programmers. a container for data, which has a Name, Data Type, and Value which can change. a Description of the data held by a variable. ex: numeric, string, matrix Data Type that describes a sequence of any characters. Literals are between single quotes.