Computer Science & C Programming


Control structures (revised)

  1. The loop is particular in the sense it has an iterator 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 loop
int 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.


9 - 10