Bifalgorithm post · Computer science one · Summation

How can a for loop compute a generalized summation in PHP?

How can a for loop compute a generalized summation in PHP? (Computer science one · Summation) — "Let’s revisit the example from Section 4.2.1 in which we compu…

Ideas in this flowchart

  • Generalizing the integer-sum code
  • The original example computes 1 + 2 + · · · + 10.
  • The original code appears in Code Sample 39.6.
  • The code can be generalized to sum up to another value.
  • The upper limit is represented by the variable n.
  • The generalized solution uses a for loop.
  • The loop initializes i to 1.
  • The loop continues while i is less than or equal to n.
  • The loop increments i after each iteration.
  • Each iteration adds i to sum.
Feed/

How can a for loop compute a generalized summation in PHP?

No cover

Channel

Computer science one

1 page

"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 39.6 We could easily generalize this code. Instead of computing a sum up to a particular number, we could have written it to sum up to another variable $n, in which case the for loop would instead look like the following.\n\n1 for($i=1; $i<=$n; $i++) {\n\n2 $sum += $i;\n\n3 }"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.