InterviewSolution
| 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) 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 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! |
|