# I have a midterm tomorrow and I'd like to know if I have these questions right.

This is a discussion on I have a midterm tomorrow and I'd like to know if I have these questions right. within the C Programming forums, part of the General Programming Boards category; The example from the previous slide was: Code: sum = n = 0; while (sum <= 1000) { n++; sum ...

1. ## I have a midterm tomorrow and I'd like to know if I have these questions right.

The example from the previous slide was:
Code:
```sum = n = 0;
while (sum <= 1000)
{
n++;
sum += n;
}
printf("The first sum > 1000 is %d, and n is %d\n", sum, n);```
Since we now know how to use the increment operators in expressions, we could get the same results by replacing the above while statement
with exactly one of the following:

Code:
```while (sum <= 1000)
sum += n++;```
or

Code:
```while (sum <= 1000)
sum += ++n;```
ALSO:

Code:
```for (n = 1; n <= 50; n += 1)
printf("%8d %8d %8d\n", n, n * n, n * n * n);```
What is the value of n for the above for loop?

2. I can't tell you if you're right because you haven't actually tried to answer either of those. Tell us what you think the answer should be and why, and we'll tell you if you're right. You need to be the compiler/computer. Get out a pencil and paper, and keep track of the value of each variable after each statement. Run through the code by hand for several iterations of the loops.

For the first one, you need to know the difference between ++n and n++. Is the value of n used before or after the increment?

3. For the first one, I think it would be

sum+ = ++n
because in the first part, n++ is before the sum+=n.

For the second one, I think the final value of n will be 50, because it says n<= 50, therefore after testing the 51th value, the program will stop because 51>50.

4. Originally Posted by CProgWiz
What is the value of n for the above for loop?
What do you mean by that? While the loop is running, or after it is done running? It has a bunch of values while it's running. It has another value once it's done.

Also, x = ++n and x = n++ give different values for x.

Quzah.

5. Code:
```for (n = 1; n <= 50; n += 1)
printf("%8d %8d %8d\n", n, n * n, n * n * n);```
What is the value of n for the above for loop?[/QUOTE]

For that I meant to type, what value is n once the above forloop finishes. I think it would be 50, but I'm not sure.

6. Does it not occur to you to simply type these things up, compile them and see for yourself?