chapter05

# chapter05 - COP 3275 Chapter 05 Jonathan C.L Liu Ph.D CISE...

This preview shows pages 1–10. Sign up to view the full content.

COP 3275: Chapter 05 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

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

View Full Document
Statements So far, we’ve used return statements and expression statements. Most of C’s remaining statements fall into three categories: Selection statements: if and switch Iteration statements: while , do , and for Jump statements: break , continue , and goto . ( return also belongs in this category.) Other C statements: Compound statement Null statement 2
Logical Expressions Several of C’s statements must test the value of an expression to see if it is “true” or “false.” For example, an if statement might need to test the expression i < j ; a true value would indicate that i is less than j . In many programming languages, an expression such as i < j would have a special “Boolean” or “logical” type. In C, a comparison such as i < j yields an integer: either 0 (false) or 1 (true). 3

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

View Full Document
Relational Operators C’s relational operators: < less than > greater than <= less than or equal to >= greater than or equal to These operators produce 0 (false) or 1 (true) when used in expressions. The relational operators can be used to compare integers and floating-point numbers, with operands of mixed types allowed. 4
Relational Operators The precedence of the relational operators is lower than that of the arithmetic operators. For example, i + j < k - 1 means (i + j) < (k - 1) . The relational operators are left associative. 5

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

View Full Document
Relational Operators The expression i < j < k is legal, but does not test whether j lies between i and k . Since the < operator is left associative, this expression is equivalent to (i < j) < k The 1 or 0 produced by i < j is then compared to k . The correct expression is i < j j < k . 6
Equality Operators C provides two equality operators: == equal to != not equal to The equality operators are left associative and produce either 0 (false) or 1 (true) as their result. The equality operators have lower precedence than the relational operators, so the expression i < j == j < k is equivalent to (i < j) == (j < k) 7

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

View Full Document
Logical Operators More complicated logical expressions can be built from simpler ones by using the logical operators: ! logical negation logical and || logical or The ! operator is unary, while and || are binary. The logical operators produce 0 or 1 as their result. The logical operators treat any nonzero operand as a true value and any zero operand as a false value. 8
Behavior of the logical operators: ! expr has the value 1 if expr has the value 0. expr1 && expr2 has the value 1 if the values of expr1 and expr2 are both nonzero. expr1 || expr2 has the value 1 if either expr1 or expr2 (or both) has a nonzero value. In all other cases, these operators

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 60

chapter05 - COP 3275 Chapter 05 Jonathan C.L Liu Ph.D CISE...

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

View Full Document
Ask a homework question - tutors are online