Hello, before I post my question I just wanted to thank those who helped me last time I posted. I was seriously considering dropping my class thinking that I wouldn't be able to do it. However, with my last hw assignment I came here and Niek_e especially pointed out some of my errors. I stayed up until around 3 am that night but I finally figured it out. So I just wanted to say thanks. Now my question. Here's what I'm supposed to do:

1. After following statements have executed, find hte value of the variable a.

int a=5;
while (a<=12)
       a=a*2;

My main question is when I plug 5 in for a and get a=10, does that 10 replace the a=5 at the beginning of the problem? So for this problem my answer would be 10, correct?

2.

int a=2
   while (a<=25)
          a=a*a;

and for this one when I multiply 2 by itself and get 4, does that 4 replace the 2? So my answers would be...4 and 16?

Thanks, and hopefully I'm doing this correctly.

Dani AI

Generated

Short answer: the assignment inside the loop replaces the old value and the loop then re-checks the condition. The loop keeps running until the while condition becomes false, so the first update is not necessarily the final value.

For the first two examples: start tracing values each iteration rather than stopping after the first multiplication. Starting at 5 you get 5 → 10, the condition is still true, so it runs again and becomes 20; then 20 fails the <= 12 test and the loop ends (final value 20). Starting at 2 and repeatedly squaring produces 2 → 4 → 16 → 256 and then stops because 256 no longer satisfies <= 25 (final value 256). As pointed out, the loop repeats until the condition is false.

For the third example with a = 2, i = 0, note that i++ is post‑increment: the current i is used in the expression and then i is incremented. Tracing yields a = 2 (add 0), 3 (add 1), 5 (add 2), 8 (add 3), 12 (add 4), 17 (add 5). Final a is 17 — equivalently 2 + sum(0..5) = 2 + 15.

Practical tips: when unsure, trace values on paper or put a quick print inside the loop to observe each iteration. Also be aware of integer overflow in C: repeatedly multiplying or squaring can overflow signed ints (undefined behavior), so check bounds or use a larger/unsigned type if needed. Credit to for the iteration trace and to for catching the correction on the first two problems.

Recommended Answers

All 5 Replies

Thank you :cheesy:
Problem #1:

So for this problem my answer would be 10, correct?

Correct.
Problem #2:

So my answers would be...4 and 16?

Well, the value of 'a' would first be 2, then 4 and then 16, so you're correct. But I wouldn't call it answers.

Thanks again for the help. I have another question though.

find the value of the variable a

int a=2, i=0;
while (i<=5){
       a=a+i;
       i++;
}

I'm not really sure how to do this one. i++ = 1, so the first time through a= 2, i=1. Then a=3, i=2 and that's where the program would end, right? Sorry if that doesn't make sense.

I'm not really sure how to do this one. i++ = 1, so the first time through a= 2, i=1. Then a=3, i=2 and that's where the program would end, right? Sorry if that doesn't make sense.

No, the i++ is executed AFTER a=a+i, so it starts with a =2 & i=0
Then: while (i<=5){ i starts at '0' and ends with '5', so the code will be executed 6 times:

times   a   i   | new-a (output..)
---------------------
  1      2   0  |   2
  2      2   1  |   3
  3      3   2  |   5
  4      5   3  |   8
  5      8   4  |   12
  6      12  5  |   17

That's it!

for the original problems, the answers should be 20 and 256. Since it's a loop, it'll repeat until the condition is false (i.e. until a>12 and a>25 respectively).

commented: Good catch, totally missed it.... -Niek E +1

You're right, I was totaly off.. :o Sorry bout that one....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.