While Loops with math

I need help with while loops containing math. We're supposed to read the codes given and put the answers on paper. We're not supposed to actually write or run a program.(My teacher doesn't explain anything.) For example, here's one of our assignment questions, copied and pasted exactly as is:

"After following statements have executed, find the value of the variable a.
1
2
3
  int a=5;
  while (a<=12)
    a=a*2;

"
Unlike all the examples I'm finding on the internet, there's no stepping of numbers. I did my best and got 640. Is that correct?

My teacher does not really give examples. For example, this is what we're given in the class
"Task: print out the squares of all the integers between 1 and 25.
1
2
3
int i;
for(i=1;i<=25;i++)
  cout<<i*i<<endl;

"
That's the entire thing. There is no answer given and the results are not shown.

(I'm including all this information about my class because last time I posted for help, people were mostly commenting on my teacher's codes being incomplete, on the assumption that I had written them and that's what I needed help with, which is not the case. I'm trying to understand how the math is done and how to do it without the program doing the math for me)
The answer to question 1 should be 20.

The initial value of your integer variable a is 5. A look at the second line tells you that it will carry out the multiplication while the value of a is less than or equal to 12. Now, let us do the math:

After the first iteration a = 10, since 5 * 2 = 10. 10 is lower than 12, so the loop will iterate for a second time. Now, because the value of a after executing the statement the first time is 10, the result of a = a * 2 (or simply a *= 2), is now 20. Since a now is 20 and is larger than 12, the loop will exit.

As to your second example, the answer shouldn't be too difficult to find it out for yourself. You know how to multiply, do you? But for the sake of helping you explain it.

The for loop's initial value is 1. In the condition statement you find it will execute as long as i is lower than or equal to 25, so it will loop 25 times. The variable is incremented by 1 each time. All it does is the following calculating for each number up to and including 25:

1*1 = 1;
2*2 = 4;
3*3 = 9;
4*4 = 16;
...
25*25 = 625;

I hope this will help you understand a little better what is going on.
Last edited on
What is the value of a at start?

The loop tests a<=12.
Is that true on first time? If it is true, what does happen to the value of a?

If the test was true, then the loop repeats the test. The a has changed (how?), and the test result could be different.

When the test becomes false, the loop is over. What is the value of a now?


How did you reach the value 640?


The for loop. How many times does it repeat? What is the value of i on them? What is the value of i*i on them? What does the loop body do? Could you list them on paper?

If you do replace word "Task" with word "Example" in teacher's writing, does that change something?
Yes, I know how to multiply. The second part was an example of how he teaches. The part of his teaching style that gives me trouble is that he'll say something, like in the example given, "okay, this will give the numbers 1 through 25" and gives some code, but explains nothing else and doesn't actually show the example's results, and he doesn't explain how the code even works, and he is prone to typos. Our first lesson on basic math had the example of
1
2
3
s=10;
a=7;
s=s+a;

and for once gave us the answer to his example, which according to our powerpoint, is 15. And he's been confusing me ever since.

I reached the answer of 640 by doing 5*2 = 10, 10*2=20, and so on, 15 times total (from 5 to 20). Because my teacher never explained what while loops are, what they do, or how they work. So I did some Googling and couldn't find anything written in the same way, so I guessed.

But thank you so much for the help! I will be able to actually turn in my assignment on time now!
playing computer with code is a good skill to have to a point. It helps debug stuff or write concise logic that does what you need without waste. It was essential in assembly where the side effects of statements are tricky (in complex assembly languages).

But if he does not cover it correctly, you are going to have a rough class.
my advice is to get a compiler and learn enough of the language to write these little snippets and run them. Then you can add additional statements to unravel what happened, or play with it to see how it works, and then re-read some online references to drive it home.

imagine you took that example and wrote this out of it:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
int main()
{
int i;

for(i=1;i<=25;i++)
{   
   cout << "inside loop, i = " << i << endl;
  cout<<i*i<<endl;
}

cout << "loop done, i = " << i endl;
}


its overkill, but you get the point... put more print statements in there, watch what it is doing, then try to get why it is doing that. Change its behavior, see what happens ... change the other one to this, see what you get...

int x = 5;
int a=5;
while (x<=12)
{
x ++;
a=a*2;
}
cout << a << endl;
If that teacher you have is not able to teach to an acceptable standard and you feel that you or even others in that class aren't learning as well as they could because of this, I would recommend either confronting him about it and letting him know what he is doing wrong so he may be able to improve, or ask and sort of head of curriculum for a new teacher.

Similar things have happened to me and it can be worth your while (loop) asking for a new teacher. Sorry for the pun. Good luck with your classes!
Topic archived. No new replies allowed.