Chapter

Summation

From ComputerScienceOne by Dr Chris Bourke

Concepts

  1. How does an enhanced for loop compute the sum of a list of integers?

    "List<Integer> list = Arrays.asList(10, 20, 8, 42);\n\nint sum = 0;\n\nfor(Integer a : list) {\n\nsum += a;\n\n}\n\nCode Sample 28.6.: Enhanced For Loops in Java Example 2"

  2. How can a summation loop be generalized to sum up to a variable n?

    "Let’s revisit the example from Section 4.2.1 in which we computed the sum of integers 1 + 2 + · · · + 10. The code is presented in Code Sample 28.8 We could have easily generalized the code somewhat.

Summation | ComputerScienceOne | Bifalgorithm | Bifalgorithm