05-Control-Flow-4UP

05-Control-Flow-4UP - for loops while loops Branching with if Branching with switch Control Flow Dhavide Aruliah UOIT MATH 2070U c D Aruliah(UOIT

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 I for , while statements for iteration I 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 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...
View Full Document

This note was uploaded on 02/23/2010 for the course MATH 2070 taught by Professor Aruliahdhavidhe during the Spring '10 term at UOIT.

Page1 / 5

05-Control-Flow-4UP - for loops while loops Branching with if Branching with switch Control Flow Dhavide Aruliah UOIT MATH 2070U c D Aruliah(UOIT

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online