
1/30/14 Statements • So far, we’ve used return statements and expression statements. Control Flow Statements • Most of C’s remaining statements fall into three categories: o Selection statements: if and switch Iteration statements: , , and o while do for Based on slides from K. N. King o Jump statements: break, continue, and goto. (return also belongs in this category.) Bryn Mawr College • Other C statements: CS246 Programming Paradigm o Compound statement o Null statement 1 2 Logical Expressions Relational Operators • In many programming languages, an expression • C’s relational operators: such as i < j would have a special “Boolean” or < less than “logical” type. > greater than less than or equal to • In C, a comparison such as i < j yields an integer: <= >= greater than or equal to either 0 (false) or 1 (true). • 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. 3 4 Relational Operators Relational Operators • The precedence of the relational operators is lower • Consider the expression than that of the arithmetic operators. i < j < k o For example, i + j < k - 1 means (i + j) < (k - o Is it legal? YES 1). o What does it test? • The relational operators are left associative. 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. • How to test whether j lies between i and k? The correct expression is i < j && j < k. 5 6 1 1/30/14 Equality Operators Logical Operators • C provides two equality operators: • More complicated logical expressions can be built == equal to from simpler ones by using the logical operators: != not equal to ! logical negation • The equality operators are left associative and produce && logical and either 0 (false) or 1 (true) as their result. || logical or • The equality operators have lower precedence than the • The ! operator is unary, while && and || are relational operators, so the expression binary. i < j == j < k • The logical operators produce 0 or 1 as their result. is equivalent to • The logical operators treat any nonzero operand as (i < j) == (j < k) a true value and any zero operand as a false value. 7 8 Logical Operators Logical Operators • Behavior of the logical operators: • Both && and || perform “short-circuit” evaluation: !expr has the value 1 if expr has the value 0. they first evaluate the left operand, then the right one. expr1 && expr2 has the value 1 if the values of expr1 • If the value of the expression can be deduced from the and expr2 are both nonzero. left operand alone, the right operand isn’t evaluated. expr1 || expr2 has the value 1 if either expr1 or expr2 (or • Example: both) has a nonzero value. (i != 0) && (j / i > 0) • In all other cases, these operators produce the value (i != 0) is evaluated first. If i isn’t equal to 0, then is evaluated. 0. (j / i > 0) • If i is 0, the entire expression must be false, so there’s no need to evaluate (j / i > 0). Without short-circuit evaluation, division by zero would have occurred. 9 10 Logical Operators Logical Operators • Thanks to the short-circuit nature of the && and || • The ! operator has the same precedence as the operators, side effects in logical expressions may unary plus and minus operators. not always occur. • The precedence of && and || is lower than that • Example: of the relational and equality operators. i > 0 && ++j > 0 o For example, i < j && k == m means (i < j) If i > 0 is false, then ++j > 0 is not evaluated, so j && (k == m). ’ isn t incremented. • The ! operator is right associative; && and || • The problem can be fixed by changing the are left associative. condition to ++j > 0 && i > 0 or, even better, by incrementing j separately. 11 12 2 1/30/14 The if Statement The if Statement • The if statement allows a program to choose • Confusing == (equality) with = (assignment) is between two alternatives by testing an expression. perhaps the most common C programming error. • In its simplest form, the if statement has the form • The statement if ( expression ) statement if (i == 0) … • When an if statement is executed, expression is tests whether i is equal to 0. evaluated; if its value is nonzero, statement is • The statement executed. if (i = 0) … • Example: assigns 0 to i, then tests whether the result is if (line_num == MAX_LINES) nonzero. line_num = 0; 13 14 The if Statement Compound Statements • Often the expression in an if statement will test • In the if statement template, notice that statement whether a variable falls within a range of values. is singular, not plural: • To test whether 0 ≤ i < n: if ( expression ) statement if (0 <= i && i < n) … • To make an if statement control two or more • To test the opposite condition (i is outside the statements, use a compound statement. range): • A compound statement has the form if (i < 0 || i >= n) … { statements } • Putting braces around a group of statements forces the compiler to treat it as a single statement. 15 16 The else Clause The else Clause • An if statement may have an else clause: • It’s not unusual for if statements to be nested inside other if statements: if ( expression ) statement else statement if (i > j) • The statement that follows the word else is if (i > k) max = i; executed if the expression has the value 0. else max = k; • Example: else if (j > k) if (i > j) max = j; max = i; else else max = k; max = j; • Aligning each else with the matching if makes the nesting easier to see. 17 18 3 1/30/14 The else Clause Cascaded if Statements • To avoid confusion, don’t hesitate to add braces: • A “cascaded” if statement is often the best way to if (i > j) { test a series of conditions, stopping as soon as one if (i > k) of them is true. max = i; • Example: else if (n < 0) max = k; printf("n is less than 0\n"); } else { else if (j > k) if (n == 0) max = j; printf("n is equal to 0\n"); else else max = k; printf("n is greater than 0\n"); } 19 20 Cascaded if Statements Cascaded if Statements • Although the second if statement is nested inside • This layout avoids the problem of excessive the first, C programmers don’t usually indent it. indentation when the number of tests is large: • Instead, they align each else with the original if: if ( expression ) statement if (n < 0) expression printf("n is less than 0\n"); else if ( ) statement else if (n == 0) … printf("n is equal to 0\n"); expression else else if ( ) statement printf("n is greater than 0\n"); else statement 21 22 Program: Calculating a Broker’s Commission Program: Calculating a Broker’s Commission • When stocks are sold or purchased through a broker, the • The broker.c program asks the user to enter the broker’s commission often depends upon the value of the stocks traded. amount of the trade, then displays the amount of • Suppose that a broker charges the amounts shown in the the commission: following table: Enter value of trade: 30000 Transaction size Commission rate Commission: $166.00 Under $2,500 $30 + 1.7% $2,500–$6,250 $56 + 0.66% • The heart of the program is a cascaded if $6,250–$20,000 $76 + 0.34% statement that determines which range the trade $20,000–$50,000 $100 + 0.22% $50,000–$500,000 $155 + 0.11% falls into. Over $500,000 $255 + 0.09% • The minimum charge is $39. 23 24 4 1/30/14 The “Dangling else” The “Dangling else” • When if statements are nested, the “dangling else” • A correctly indented version would look like this: problem may occur: if (y != 0) if (y != 0) if (x != 0) if (x != 0) result = x / y; result = x / y; else else printf("Error: y is equal to 0\n"); printf("Error: y is equal to 0\n"); • The indentation suggests that the else clause belongs to the outer if statement. • However, C follows the rule that an else clause belongs to the nearest if statement that hasn’t already been paired with an else. 25 26 The “Dangling else” Conditional Expressions • To make the else clause part of the outer if • C’s conditional operator allows an expression to statement, we can enclose the inner if statement produce one of two values depending on the value in braces: of a condition. if (y != 0) { • The conditional operator consists of two symbols if (x != 0) (? and :), which must be used together: result = x / y; expr1 expr2 expr3 } else ? : printf("Error: y is equal to 0\n"); • The operands can be of any type. • Using braces in the original if statement would • The resulting expression is said to be a conditional have avoided the problem in the first place. expression. 27 28 Conditional Expressions Conditional Expressions • The conditional operator requires three operands, • Example: so it is often referred to as a ternary operator. int i, j, k; • The conditional expression expr1 ? expr2 : expr3 i = 1; should be read “if expr1 then expr2 else expr3.” j = 2; • The expression is evaluated in stages: expr1 is k = i > j ? i : j; /* k is now 2 */ k = (i >= 0 ? i : 0) + j; /* k is now 3 */ ’ evaluated first; if its value isn t zero, then expr2 is The parentheses are necessary, because the evaluated, and its value is the value of the entire precedence of the conditional operator is less than conditional expression.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-