Yeah whenever the current element is 5, it will be calculated as s = 2 + 5;
And every element after 5 is 6 or greater so nothing else will be added to the sum.
You have an array with five elements, hence why you're for-looping through it five times. The variable s starts at the value of 0.
Inside your loop, all you're doing is seeing if the value stored in the index you're at (so index 0 would be the value 2, index 1 would be the value 6) is less than 6 or not. The phrase s = s + arr[c]; (or s += arr[c]; means that it's going to add the value of the current index to variable s if that number meets the conditions of the if statement.
So knowing this, all you have to do is check that condition against the values in your array. Only two of the numbers are less than 6.
int arr[5] = {2, 6, 5, 7, 9}; // An array of integers (some of the numbers in it are LESS THAN 6)
int s = 0; // Declare a variable to hold a SUM and initialize it to 0
for (int c = 0; c < 5; c = c + 1) // Start a loop that counts from 0 to 5
{
if (arr[c] < 6) // If the number in the array at index c is LESS THAN 6...
{
s = s + arr[c]; // ADD that number to the current SUM
}
}
cout << s << endl; // Print the SUM
Basically the array elements are checked one at a time. If any element is less than 6 it gets added to the sum. Since only two elements are less than 6 (2 and 5), you get a total of 7.
The best way to understand this type of question is to get a piece of paper and pretend you are the compiler. Work through the program stepping through the loop and updating your values on the paper.