So I'm taking a programming class for first year Engineering. We're coding in C#. I'm understanding everything pretty much perfectly, doing really well. But we had a quiz a few days ago and I got this question wrong. Even after looking over it again and again, I don't understand what I did wrong. What value does count hold when the following code fragment is done executing? Code (Text): int count = 0; int i = 0; while (i <= 5) { i = i + 1; int j = 0; while (j <= 3) { count = count + 1; j = j + 1; } }
Ewww, C#... But prejudice aside... I think the answer is 24. The first while loop executes 6 times (i = 0... 5) and the inside while loop executes 4 times (j = 0... 3). Therefore the "count=count+1" statement is executed a grand total of 24 times.
Did you even read what I wrote? Silly The answer they gave was either 3 or 24. It's weird. No idea how it could be 3. 24 makes sense now that it's explained. I said 15, because I thought it was 5x3, rather than 6x4. That's where I went wrong. Thanks guys.