1.

Solve : help understanding C++ code?

Answer»

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.

Code: [Select]#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;
}
nevermind, someone gave me a tip..

I couldn't visualize that total += 3; is the same thing as total = total + 3;

The latter makes more SENSE and I think I'd rather use that syntax instead..
-makes the ULTIMATE NOOB sound-

Just kidding EEVIAC. That's rather beginners stuff yes. =P
You could also read up on the difference between i++ and ++i. =)I've got two books that I'm learning out of right now... One of them is like a text book for students and the other is a simplified explanation of the language. I started out with the text book, got overwhelmed a little and switched over to the basic book which is not as thorough in it's explanations, so I get confused at times. After finishing the basic book I'm going back to the text book where I left off and doing the assignment programs at the end of the chapters.. The basic book is quite handy actually. When I get confused about something in the text book, I look for a simplified explanation in the basic book.. hehe

Quote from: Treval on April 17, 2010, 08:11:03 AM

You could also read up on the difference between i++ and ++i. =)

HAHA! I know that!

Code: [Select]#include <iostream>
using namespace std;
int main()
{
int a,b,y,z;
z=y=5;
a=z++;
b=++y;
cout << a << " " << b << " " << y << " " << z;
}
would print:
5 6 6 6

basically, ++ or -- the variable will increment/decrement the value RETURNING it's value, so if you did something like cout << x++; it would print the current value of x and then increment it by one, whereas cout << ++x would increment it and then print that value.



}
I don't get the z=y=5 part. =P

EEVIAC, through my own experience, I'd say take the basics book first.
However it's fine how you're doing.Quote from: Treval on April 17, 2010, 11:29:09 AM
I don't get the z=y=5 part. =P

EEVIAC, through my own experience, I'd say take the basics book first.
However it's fine how you're doing.

assignment- think of it this way:

z=(y=5)

of course, = is assignment, so y is assigned 5, but assignment also "returns" (as a rval) the assigned value- so 5 is returned, and then the other assignment assigns it to z. the end!


Discussion

No Comment Found