80 likes | 210 Vues
This document provides an overview of conditional statements and logical operators in programming. It covers relational operators, their comparison results, and how they are used in array handling. The section explains how to apply comparisons between arrays and evaluates conditions using logical operators. Concepts such as array addressing, logical evaluations, and practical examples are included. This resource is invaluable for anyone looking to strengthen their understanding of conditional logic in coding.
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