# COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review

Post on 31-Dec-2015

212 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

COMP 116 - Introduction to Scientific Programming

COMP 116: Introduction to Scientific ProgrammingLecture 28: Midterm #2 ReviewAnnouncementsMidterm in SN014

TopicsConditional LogicWriting Simple FunctionsVariables, workspaces, scopeLoopsStringsConditional LogicConditional Programming

When you want to run some code only if some test (condition) is satisfied

Making choices between 2 or more options

if-else-end

Relational OperatorsTests relationship between two objects or arraysWhat is the data type of the result of these operators?

NameOperatorsExamplesEquivalenceEqualityInequalityBinary OperatorsLess ThanLess Than or EqualGreater Than or EqualGreater ThanRelational OperatorsTests relationship between two objects or arraysResult is always a logical data type (or an array of logical data types)

NameOperatorsExamplesEquivalenceEquality== 5 == 5, x == yInequality~= 5 ~= 5, z == (x^2 + y^2)Binary OperatorsLess Than

10 > 7Logical OperatorsBoolean operatorsNameOperatorsExamplesUnary OperatorsLogical Negation (NOT)Binary OperatorsLogical And (AND)Short circuit (AND)Logical Or (OR)Short circuit (OR)Exclusive Or (XOR)Logical OperatorsBoolean operatorsNameOperatorsExamplesUnary OperatorsLogical Negation (NOT)~ (3 == 5) = 1 (true)Binary OperatorsLogical And (AND)Short circuit (AND)& or &&5 & 7 = 1 (true)Logical Or (OR)Short circuit (OR)| or ||0 | 1 = 1 (true)Exclusive Or (XOR)XORxor(8, 5) = 0 (false)Operates on logical data types, also returns a logical result.Other Logical ObjectsLogical Constants: true, falseLogical Functions: and(), or(), xor(), not()Predicate Logic: any(), all()Conversion Function: logical()Test functions (is* functions)Examples: isvarname(), iskeyword()String Comparison functions:strcmp(), strcmpi(), strncmp(), strncmpi()

Two options for logical indexingUse a logical vector x = rand(1, 100);x(x > 0.5); % returns only those elements in x which satisfy test

Use the find() functiony = find( x > 0. 5 ); x( y ) % returns elements in x whose indices are in y

Conditional ExecutionMultiple chained testsif commands1; % T1 trueelseif commands2; % T1 false T2 trueelseif commands3; % T1,T2 false T3 trueelse commands4; % all falseendText Outputdisp(msg)- displays an array or text stringdisp(Derp');

error(msg) - displays an error message and aborts current function or scripterror('Value Out of Range');Writing simple functionsfunction [o1, o2] = funcName( i1, i2 )% function comments % body (implementation)end Can have multiple inputs and multiple outputs

function [] = funcName()function o1 = funcName()function o1 = funcName( i1 )function o1 = funcName( i1, i2 )function [o1, o2] = funcName( i1, i2, i3)ScopeFunctions run in their own workspaces

MATLAB

sq.m x =4 x2 =16 foo =4 x2 =5 bar =16 Scripts vs. Functions

Loops: for loop statementthe counted loop solutionfor = :end

for = ::end

Loops: while loop statementthe conditional loop solutionwhile end

While loops are great if we dont know how many times we need to loop, but if we can write a test for when were done

For this to work properly, the test needs to evaluate to a logical value

The while loop will run as long as test evaluates to true

The while loop does not have a built-in counter like the for-loop (if you want to count something, you need to implement the counter yourself)Loops: Common pitfallsWhile-loops:Counters not initialized

While-loop never terminates or gets never executed

Counter does not count all the way to the desired value: e.g., x