{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-Control-Flow-4UP

05-Control-Flow-4UP - for loops while loops Branching with...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
for loops while loops Branching with if Branching with switch Control Flow Dhavide Aruliah UOIT MATH 2070U c D. Aruliah (UOIT) Control Flow MATH 2070U 1 / 24 for loops while loops Branching with if Branching with switch Control Flow 1 Iteration with for loops 2 Iteration with while loops 3 Branching with if statements 4 Branching with switch statements c D. Aruliah (UOIT) Control Flow MATH 2070U 2 / 24 for loops while loops Branching with if Branching with switch Control flow Control flow refers to changing the order in which statements (or blocks of statements) in a program will be executed Straight-line program : executes statements strictly in sequence Iteration : executing a block of statements repeatedly Branching : choosing between alternative statements to execute Iteration and branching in Matlab for , while statements for iteration if , switch statements for branching Control flow determined by logical expressions (tests) need to understand relational & logical operators well c D. Aruliah (UOIT) Control Flow MATH 2070U 3 / 24 for loops while loops Branching with if Branching with switch for loops Basic syntax of for loops for n = array commands end n is loop index variable commands execute with n taking each value in array in sequence At end , return to top, increments n and repeats commands If array empty (i.e., array==[] ), commands do not execute at all Customary to use indentation to emphasise commands Number of iterations fixed by array c D. Aruliah (UOIT) Control Flow MATH 2070U 5 / 24
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
for loops while loops Branching with if Branching with switch Using for loops Example ( for example.m ) x = zeros(1,5) ; for n = 1:5 x(n) = cos(n*pi/5) ; disp(n) , disp(x) , pause end What Happens Initialise array x to all zeros n is assigned the value 1 n is 1 : x(1) is assigned the value cos ( 1 · π /5 ) n is 1 : n and x are displayed n is 1 : end of loop, return to top n is assigned the value 2 n is 2 : x(2) is assigned the value cos ( 2 · π /5 ) n is 2 : n and x are displayed n is 2 : end of loop, return to top n is assigned the value 3 n is 3 : x(3) is assigned the value cos ( 3 · π /5 ) n is 3 : n and x are displayed n is 3 : end of loop, return to top n is assigned the value 4 n is 4 : x(4)
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern