"continue" statement

Apr 17, 2010 at 8:12am
This is a sample program out of a book I have (C++), demonstrating the use of the "continue" statement, in a FOR LOOP. The program asks you how many "items" you want to buy and charges 3 dollars per item, but the 13th item is free. For example, if you enter that you want 12 items, you'll be charged 36 dollars. If you enter that you want 13 items, you're still charged only 36 dollars. When you enter "14" the "total" begins to increment again, and you would be charged $ 39 in that case. (Every 13th item is free)

Where I get confused is the use of the "total +=3;" statement.. I just don't get how exactly it works



#include <iostream>
using namespace std;
int main(void)
{
int num;
int counter;
int total = 0;
cout << "How many items do you want to buy: ";
cin >> num;
for (int counter = 1; counter <= num; counter++)
{
if (counter % 13 == 0)
continue;
total += 3;
}
cout << "Total for " << num << " items is $ " << total << "\n";
return 0;
}

Apr 17, 2010 at 8:25am
total+=3; is semantically equivalent to total = total +3;.

Basically, the part in the loop says:
"If the counter is divisible with 13, then skip this iteration. Otherwise, add 3 to the total price"
Last edited on Apr 17, 2010 at 8:28am
Apr 17, 2010 at 8:33am
thanks so much !
Topic archived. No new replies allowed.