if Statements A set of statements(the code block to be executed)is shown as a rectangle, a decision point is shown as a diamond condition and the flow of program control is indicated by arrows true The execution of a code block is based statements on some conditional test If the result of the test is true. the code block is executed. Otherwise the code block is omitted and the instructions after the A simple if statement end of that code block are executed
17 if Statements • A set of statements (the code block to be executed) is shown as a rectangle, a decision point is shown as a diamond, and the flow of program control is indicated by arrows. • The execution of a code block is based on some conditional test. If the result of the test is true, the code block is executed. Otherwise, the code block is omitted and the instructions after the end of that code block are executed. if condition statements true false A simple if statement
if Statements Examples: Suppose Test=2000;3000;4000;:0001 Type ifTest(4, 4>0 ifTest(1,1)>07est(2,4)==0 display (Conditionis true,) display true y end else display false y Matlab displays en ans Condition is true Matlab displays ans= TRUE
18 if Statements Examples: Suppose Test =[2 0 0 0 ; 3 0 0 0; 4 0 0 0; 0 0 0 1] Type if Test(4,4)>0 display(‘Condition is true’) end Matlab displays ans = Condition is true Type if Test(1,1)>0 | Test(2,4)==0 display(‘TRUE’) else display(‘FALSE’) end Matlab displays ans = TRUE
if Statements if false elseif false elseif fal condition condition condition else true true true statements(1) statements(2) statements(n) statements(n+1) A compound if statement(more than one conditional test) In the compound if statement, if the first logical test returns false,a second test is performed to determine whether the second code block should be executed. If that test returns false, as many further tests as necessary may be performed, each with the appropriate code block to be implemented when the result is true. Finally, if none of these tests returns true, the last code block (with the keyword else) is executed 19
19 if Statements • In the compound if statement, if the first logical test returns false, a second test is performed to determine whether the second code block should be executed. If that test returns false, as many further tests as necessary may be performed, each with the appropriate code block to be implemented when the result is true. Finally, if none of these tests returns true, the last code block (with the keyword else) is executed. if condition statements(1) true false A compound if statement (more than one conditional test) elseif condition statements(2) true false elseif condition statements(n) true false statements(n+1) … else
if Statements if false elseif false elseif fal condition condition condition else true true true statements(1) statements(2) statements(n) statements(n+1) A compound if statement(more than one conditional test) If one of the code blocks is executed the next instruction to execute is the one that follows the conditional code after the end statement 2. In particular, if there is no else clause, it is possible that no code at all is executed in this conditional statement
20 if Statements 1. If one of the code blocks is executed, the next instruction to execute is the one that follows the conditional code after the end statement. 2. In particular, if there is no else clause, it is possible that no code at all is executed in this conditional statement. if condition statements(1) true false A compound if statement (more than one conditional test) elseif condition statements(2) true false elseif condition statements(n) true false statements(n+1) … else
General Template of if statements if <logical expression 1> <code block 1> elseif <logical expression 2> code block 2 elseif <logical expression n> <code block n> <default code block> end
21 General Template of if Statements if <logical expression 1> <code block 1> elseif <logical expression 2> <code block 2> elseif <logical expression n> <code block n> else <default code block> end …