80 likes | 201 Views
Conditional Statements. ผศ.ดร.อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th. Overview. Relational operators Logical operators Condition Statements. Relational Operators. Relational Operators. The comparison result is either “ 0 ” if false
E N D
Conditional Statements ผศ.ดร.อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th
Overview • Relational operators • Logical operators • Condition Statements
Relational Operators • The comparison result is either • “0” if false • “1” if true • Normally is “Array comparison” • Can be used for “Array addressing”
Array comparison >>c = (a == b) c = 0 0 1 0 >>a = [1 2 3 4]; >>b = [-1 4 3 0]; >>c = (a < b) >>c = (a ~= b) c = 1 1 0 1 c = 0 1 0 0 >>c = (a = b) >>c = (a >= b) c = 1 0 1 1 ??? c = (a = b) | Error: The expression …
Array addressing >>a = [1 2 3 4]; >>b = [-1 4 3 0]; >>c = a(a < b) >>c = b(a == b) c = 3 c = 2 >>c = a(a ~= b) >>c = b(a >= b) c = -1 3 0 c = 1 2 4
More on comparison >> 3 < 5 ans = 1 >>a = [0:4] a = 0 1 2 3 4 >>x =(sqrt(a)>=1) x = 0 1 1 1 1 >>y = a(x) y = 1 2 3 4 >> a = (12 < 7) a = 0