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

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

View Full Document Right Arrow Icon
COP 3275: Chapter 05 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 9

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

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

Page1 / 60

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

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

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