You are on page 1of 3
2.3.4 Conditional Statements (if) Like most programming languages, the way to exeente code conditionally is with the if statement. In the following example, the division is done only when the denominator does uot equal zero. Dividing by zero is one common mistake that can stop a program. % Look out for division by 0 if (denominator “= 0) result = numerator / denominator; end Here is another way of handling this. In the following example, the if statement either performs the sprinté, or else it performs the division, but it will not do both. Notice how a single equal sign would indicate assignment, ¢.g., a=1, but when comparing two values, two equal signs are used, e.g., if (a==1). This way it is clear to the computer which operation to perform (assignment or comparison). Un- fortunately, this is a common source of error in a program. % Look out for division by 0 if (denominator == 0) sprintf('Cannot perform division since denominator is 0') else result = numerator / denominator; end As might be expected, if statements can be nested. For example, the following code checks a pair of indices (m and n) before allowing them to be used to access a matrix’s value. This kind of check is useful to make sure that the program does not crash. Before running this code, however, values for m,n, and A (the matrix) need to be defined. 534,56; 78); Now try the following code. Note that if and(m>0, n>0) would be equivalent to if ((m>0) && (n>0)) in C/C++. Experiment with different values for m and n. An easy way to try this repeatedly is to use MATLAB's built-in editor, and save these commands in a file (for example, “checkindices.n”). When you want to run it, simply type the filename without the extension (e.g., checkindices) at the command prompt. The code does have a precondition, in that it expects A, m, and n to be defined before this code is executed. % Check that the indices m and n are valid [rows, cols] = size(A); if and(m>0, n>0) if and(m<=rows, n<=cols) sprintf(' A ( %d, 4d) = %d', m, n, A(m,n)) else sprintf (‘One of the indices m or n is too big.') end else sprintf('The indices m and n must be greater than 0.') end Also, version 7 of MATLAB was enhanced to allow the && operator to be used just like in C/C++. (This does not work with version 6.) Here is a short example. >> array= [37 85 58]; >> index = 3; >> if (index >=1 ) && (index <= length(array)) array (index) = array(index)+2; end >> array array = 37 85. The else statement goes with the nearest if, Note that else if is treated differently than elseif. The command else if is treated as two separate state- ments; an end is expected for both the second if as well as the first. The command elseif is a continmation of the first if, much like a switch/case statement. The two examples below show how the if statement can be structured % One way to structure the if statement if (a= 1) sprintf('a = 1') else if (a == 2) sprintf('a = 2') end end % Another way to structure the if statement if (a == 1) sprintf('a = 1") elseif (a = 2) sprinti('a = 2") end Any if should have a corresponding end, unless it is an elseif. For clarity, it is better to use elseif, or to put else and if on different lines.

You might also like