CEE 15 Introduction to Computing for Civil Engineers 3 - Branching Statements

CEE 15 Introduction to Computing for Civil Engineers 3 - Branching Statements

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

View Full Document Right Arrow Icon
CEE 15 Introduction to Computing for Civil Engineers 24 Chapter 3 Branching Statements 3.1. Relational Operators MATLAB has six relational operators for comparing two matrices of equal size or two scalars. Table 3.1 Relational operators Relational Operator Interpretation < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to Consider the following program: a = input('a ='); b = input('b ='); if a > b disp('a is greater than b') end if a < b disp('a is greater than b') end if a == b disp('a is equal to b') end Output 1: a =3 b =4 b is greater than a Output 2: a =4 b =2 a is greater than b Output 3: a =5 b =5 a is equal to b Output 4: a =[2 3 5] b =[3 7 9] b is greater than a Output 5: a = [2 4 5] b = [1 3 6]
Background image of page 1

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

View Full DocumentRight Arrow Icon
CEE 15 Introduction to Computing for Civil Engineers 25 When we compare two matrices or vectors using a relational operator, the logical expression is only true if all the elements satisfy the condition individually. For example, both a and b vectors have 3 elements. In order to have the logical expression a > b to be true, all the 3 elements of a must be greater than the corresponding elements of b . All the relational operators have the same precedence. Consider the following program and its output a = input('a =') b = input('b =') if a > b disp('a is greater than b') end if a < b disp('b is greater than a') end if a == b disp('a is equal to b') end Output : a = [2 3] a = 2 3 b = [4 5] b = 4 5 b is greater than a 3.2. Logical Operators We can also combine two logical expressions using the logical operators . Table 3.2 Logical Operators Logical Operator Symbol and & or | not ~ The hierarchy from highest to lowest, is not , and, and or . Table 3.3 Result of Logical Operators A B ~A A | B A & B 0 0 1 0 0 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 In MATLAB, there is no logical variable. Therefore, the assignment A=.TRUE. or A=.FALSE. is not correct, neither A=TRUE or A=FALSE. If you want to have a value ‘ true ’ , then you assign A= ’ , and A is treated as a character array, and the length of the array is 4, but later you cannot assign A= ’ false ’ , because the length of ‘ false ’ is 5 and the length of the array cannot be changed that way unless you do A= ’ fals ’ and A(5)= ’ e’ .
Background image of page 2
CEE 15 Introduction to Computing for Civil Engineers 26 3.3. Simple if Statement: if logical expression statement group a end Consider the following program : n = input('n=') count = 0; if n < 50 count = count + 1; disp('count') disp(count) end disp('count') disp(count) Output 1 : n=30 n = 30 count 1 count 1 Output 2 n=100 n = 100 count 0 3.4. Nested if Statement: logical expression 1 statement group a logical expression 2 statement group b end statement group c end
Background image of page 3

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

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

This note was uploaded on 09/28/2011 for the course CE 13972 taught by Professor Chow during the Spring '09 term at Berkeley.

Page1 / 14

CEE 15 Introduction to Computing for Civil Engineers 3 - Branching Statements

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

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