CS99 Fall 2002 Prelim 1 Topics Problem Solving + programming is automating problem solving + process (brainstorm, research, outline, draft, polish, test) + algorithms for outlining - definition: sequence of steps to solve a problem - must be general enough to implement in most languages + programming converts human algorithm into computer code Languages + computer language needed for code + similar features of human languages - alphabet (keyboard characters) - words (tokens that use characters) - sentences (statements, which are instructions written with tokens) MATLAB language (MATrix LABoratory) + tokens: white space, token separators, punctuation, operators, numbers, strings, comments, identifiers, keywords, functions, arrays (actually, EVERYTHING in MATLAB is technically an array) + statements: - empty, expression, function, assignment, selection - repetition not on this test - punctuation: , (for same line but not suppress output) ; (suppress output) hitting RETURN Expression statement + expression combine values, operators, functions + operator precedence, associativity + MATLAB outputs result of expression unless you suppress output with semicolon (;) + use parentheses to clarify order of desired operations Assignment statement + must give a variable a value before using it + MATLAB is weakly typed -- does not require you to indicate a variable's type (everything is technically an array, anyway) + assignment syntax: name=expression + to clear an assignment use clear or clear name Function statement + name(parameters) causes MATLAB to - look for a function called name - do the commands written inside the function called name + so, functions are like separate mini-programs + a function that returns a value is replaced by that value in an expression ex) 1 + sqrt(4) is replaced by 1 + 2 which is evaluated to 3 Selection statement + need to make choices + use if-elseif-elseif...-else-end + only if and end are necessary + syntax: if c s end + c is a boolean expression - rem: 0 is false, and any other number (esp 1) is true - expression composed of logical values (0,1), logical operators & (and) | (or) ~ (not), and relations + s is a block of statements - each statement executes if the condition is true - when end reached, statement stops + can have as many elseifs as you want + only one else is allowed + shortcut notation: put everything on same line and separate with commas + nested ifs - can make choices within choices - makes a compound statement While loops + while(c), statements, end syntax + tracing variable values in a while loop + loops to produce repeated values Not on exam: + plotting + logical arrays