counter
with a start point counter=1
, an end point counter>n
and an increment counter=counter+1
. In such a situation it is possible to use a for
loopint sum(int n) {
int result = 0;
for (int counter=1; counter<=n; counter=counter+1)
result = result+counter;
return result;
}
Nb. This loop is associated with two specific keywords:
a. break
inside the loop will stop this one and perform instructions after the loop's body.
b. continue
will pass directly to the next iteration.