statements
play

Statements So far, we ve used return statements and expression - PDF document

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 o Iteration


  1. 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 o Iteration statements: while , do , and for Based on slides from K. N. King o Jump statements: break , continue , and goto . ( return also belongs in this category.) • Other C statements: Bryn Mawr College CS246 Programming Paradigm o Compound statement o Null statement 2 1 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 ¡

  2. 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: they first evaluate the left operand, then the right one. ! expr has the value 1 if expr has the value 0. • If the value of the expression can be deduced from the expr1 && expr2 has the value 1 if the values of expr1 left operand alone, the right operand isn ’ t evaluated. and expr2 are both nonzero. • Example: expr1 || expr2 has the value 1 if either expr1 or expr2 (or (i != 0) && (j / i > 0) both) has a nonzero value. (i != 0) is evaluated first. If i isn ’ t equal to 0, then • In all other cases, these operators produce the value (j / i > 0) is evaluated. 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 ¡

  3. 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 • To make an if statement control two or more if (0 <= i && i < n) … statements, use a compound statement. • To test the opposite condition ( i is outside the • A compound statement has the form range): { statements } if (i < 0 || i >= n) … • 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 • It ’ s not unusual for if statements to be nested inside • An if statement may have an else clause: 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 max = k; else • Aligning each else with the matching if makes the max = j; nesting easier to see. 17 18 3 ¡

  4. 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 test a series of conditions, stopping as soon as one if (i > j) { of them is true. if (i > k) 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) else if ( expression ) printf("n is less than 0\n"); statement else if (n == 0) … printf("n is equal to 0\n"); else if ( expression ) else 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 amount of the trade, then displays the amount of stocks traded. the commission: • Suppose that a broker charges the amounts shown in the following table: Enter value of trade: 30000 Transaction size Commission rate Commission: $166.00 Under $2,500 $30 + 1.7% • The heart of the program is a cascaded if $2,500–$6,250 $56 + 0.66% statement that determines which range the trade $6,250–$20,000 $76 + 0.34% $20,000–$50,000 $100 + 0.22% falls into. $50,000–$500,000 $155 + 0.11% Over $500,000 $255 + 0.09% • The minimum charge is $39. 23 24 4 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend