computes the sum 1 2 3 100 include stdioh int mainvoid int sum 0 i 1 while i

Computes the sum 1 2 3 100 include stdioh int

This preview shows page 9 - 20 out of 47 pages.

/* computes the sum: 1 + 2 + 3 + .... + 100 */ #include <stdio.h> int main(void) { int sum =0, i = 1; while (i <= 100) { sum = sum + i; i = i + 1; } printf("Sum is %d\n", sum); return 0; } = 100 1 i i
Image of page 9
10 Compound Assignment Operators Several times we have seen: variable = variable <operator> expression; Example: sum = sum + i; where <operator> is a C operator This occurs so often, C gives us short cuts. Instead of writing x = x +1 we can write: x += 1. W can use -=, *=, /=, and %= in the same way.
Image of page 10
11 The For Statement A better way to construct a counting loop is to use the for statement. C provides the for statement as another form for implementing loops. As before we need to Initialize the loop control variable Test the loop repetition condition Update the loop control variable. An important feature of the for statement in C is that it supplies a designated place for each of these three components. An example of the for statement is shown in the next slide.
Image of page 11
12 For Example To compute the sum of 1 to 100: int sum = 0; int i; for (i = 1; i <= 100; i++) { sum = sum + i; } Note: i++ is the same as i = i + 1 and as i += 1 .
Image of page 12
13 General Form of For statement for (initialize; test; update) { //Steps to perform each iteration } First, the initialization expression is executed. Then, the loop repetition condition is tested. If the condition is true, the statement enclosed in { } are executed. After that the update expression is evaluated. Then the loop repetition condition is retested. The statement is repeated as long as the condition is true. For loop can be used to count up or down by any interval.
Image of page 13
14 Program Style For clarity, it can be useful to place each expression of the for heading on a separate line. If all three expressions are very short, we will place them together on one line, like we did in the example. The body of the for loop is indented just as the if statement.
Image of page 14
15 Increment and Decrement Operators The counting loops that we have seen have all included assignment expressions of the form counter = counter + 1 or counter++ or counter += 1 This will add 1 to the variable counter. If we use a - instead of a +, it will subtract 1 from the variable counter. Be careful about using the ++ or -- options.
Image of page 15
16 Increment and Decrement Other Than 1 Instead of adding just 1, we can use sum = sum + x or sum += x Both of these will take the value of sum and add x to it and then assign the new value to sum . We can also use temp = temp -x or temp -= x Both of these will take the value of temp and subtract x from it and then assign the new value to temp .
Image of page 16
17 Prefix and Postfix Increment/Decrement The values of the expression in which the ++ operator is used depends on the position of the operator. When the ++ operator is placed immediately in front of its operand (prefix increment, Ex: ++x ), the value of the expression is the variable’s value after incrementing. When the ++ operator is placed immediately after the operand (postfix increment , Ex: x++ ), the value of the expression is the value of the variable before it is incremented.
Image of page 17
18 Comparison of Prefix and Postfix Increments
Image of page 18
19 More on prefix and postfix operator printf(“%3d”, --n); printf(“%3d”, n); printf(“%3d”, n--); printf(“%3d”, n); If n = 4, what will be the output of the following?
Image of page 19
Image of page 20

You've reached the end of your free preview.

Want to read all 47 pages?

  • Spring '10
  • baleh
  • sentinel value, SENTINEL, Decrement Operators

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes