Class Notes (807,343)
United States (312,496)
ENG 6 (40)
Lecture 6

ENG 06 – Lecture 6 notes.docx

5 Pages
64 Views

School
University of California - Davis
Department
Engineering
Course
ENG 6
Professor
A H M E D R A W Y
Semester
Spring

Description
ENG 06 – Lecture #6; 1/24/2013 Flow Control  Programming Concept: Code Block o Collection of MATLAB instructions identified if  You wish to execute them only under certain circumstances  You wish to repeat them a certain number of times o Use other key words to identify code blocks o Use indentations to identify code blocks/make them readable  Key command words o If, switch, while, for, case, otherwise, else, elseif o Identified with blue coloring o Not actually part of the code block, just instructions on what to do with code block If-elseif construct  Basic conditional execution  Requires two things o Logical expression o Code block  If expression is true, code block is executed  Otherwise, execution is resumed at instruction following code block  If-Elseif: Compound conditionals o By introducing elseif and else, allow for more tests if test returns false  General template for if-elseif statements: o If o o elseif o o … o else if o o else o o end  Only one option is ever chosen, the first that is true o Order of statements matters, put most exclusive first  Checking user input is important o Use MATLAB‟s isnumeric function and error functions to stop a program o Ex. if(~isnumeric(age)) %if age isn‟t a number  Error(„age should have been a number‟); o End  You can also use logical operators to check multiple conditions o AND o If(age>=17 & age<=25) o Disp(„you can drive, but can‟‟t rent a car‟); o End o Using OR operator o If(x>=1000 | x100 | x<=-100) o Disp(number has 3 digits o elseif(x>=10 | x<=‐ 10)  disp('number has 2 digits') o elseif(x==0)  disp('number is 0') o else o disp('number has 1 digit'); o end Iteration  Allows controlled repetition of a code block  Caveat for MATLAB o If you can perform a task without using loops, use arrays o Built in functions and matrix tasks generally run faster  Two Types of Iterative structure o For loops: designed to repeat code for a fixed number of times and automates process of managing iteration o While loops: more flexible in character  Code block can be repeated a variable number of times  More of a “do-it-yourself” iteration kit  FOR loop o Template:  For =   end o Automatically sets value of variable to next element of vector, executes code block with that value, moves to next vector element o Ex. for x=1:15 o Disp(„HELLO‟) o x o end  Composing a For Loop o First think about process you want to implement o Try writing code that would implement the first few iterations o Identify what changes from line to line, replace with a variable  For Loop Example: Counting #1 o Sum all 1, 2, and 3 digit prime numbers o Total = 0 o For k=1:999 o If(isprime(k)) o Total = total + k o End o End o Disp(total)
More Less

Related notes for ENG 6

OR

Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Join to view

OR

By registering, I agree to the Terms and Privacy Policies
Just a few more details

So we can recommend you notes for your school.

Get notes from the top students in your class.

Request Course
Submit