Forum > Beginners

Need help understanding this beginner loop & block program

(1/2) > >>

MouseTech:
I am learning Pascal using a book called Pascal Made Simple by the author PK McBride and I am now currently learning about loops and blocks.

The program below is suppose to demonstrate how a block works with a loop by making a simple program that shows a list of answers of the ascending exponents for the number you enter in the first line.

I chose to enter 2 and as you see, the program works fine.

What I don't understand is how the program managed to multiply everything.

I've never been too good at math, but I know that 2 to the power of 3 is written 2 * 2 * 2 and that the answer is "8". But I can't see how the program I made is calculating the answers that way.

I know that the answer must be simple and I will feel quite foolish for not seeing it in the first place, but any enlightening help would be appreciated. Thank you in advanced.

ojz0r:
Its in the "powered := powered * table".
For every loop iteration, powered is the product of the previous calculation

egsuh:
Simply it as follows.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---powered:= 1; Table:= 2;  for loop := 1 to 12 do begin     powered:= powered * table; end; In variable Powered, one is stored. When the program reaches  "for loop := 1 to 12", it repeats the block --- codes between begin and end --- 12 times.

At the statement

powered := powered * table;

first, the value stored in powered (right powered in the above expression) is retrieved, which is one at the first repetition. And then it is mulitplied with value stored in table, which is 2. So 1 * 2  equals 2. And this value, 2, is stored in the powered variable. Powered:= ... means to save the right side value to the left side variable.

The original value of powered, which used to be 1, is not gone (and do not exist anywhere).

At the second repetition, it does the same --- it retrieves value stored in powered, which is now 2, and then make calculaiton of 2 * 2, which is 4. Now 4 is stored in the powered. In this way it calculates what you see.

dseligo:
Correction:

--- Quote from: egsuh on April 25, 2021, 10:43:08 am ---The original value of powered, which used to be 1, is now gone (and do not exist anywhere).

--- End quote ---

MouseTech:

--- Quote from: egsuh on April 25, 2021, 10:43:08 am ---Simply it as follows.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---powered:= 1; Table:= 2;  for loop := 1 to 12 do begin     powered:= powered * table; end; In variable Powered, one is stored. When the program reaches  "for loop := 1 to 12", it repeats the block --- codes between begin and end --- 12 times.

At the statement

powered := powered * table;

first, the value stored in powered (right powered in the above expression) is retrieved, which is one at the first repetition. And then it is mulitplied with value stored in table, which is 2. So 1 * 2  equals 2. And this value, 2, is stored in the powered variable. Powered:= ... means to save the right side value to the left side variable.

The original value of powered, which used to be 1, is not gone (and do not exist anywhere).

At the second repetition, it does the same --- it retrieves value stored in powered, which is now 2, and then make calculaiton of 2 * 2, which is 4. Now 4 is stored in the powered. In this way it calculates what you see.

--- End quote ---

I think I understand more how the program works.

Below is a diagram I made to help me understand how the program is calculating the data it is given.

Please correct me if it is wrong.

So basically the "powered:= 1" variable taken by the "powered:= powered * table" variable in the loop and is calculated as "1 x 2 = 2". Then the answer "2" overwrites the "powered:= 1" variable and changes it to "powered:= 2".

Then the loop starts again but instead of "powered:= powered * table" equaling "1 x 2 = 2" it now reads as "2 x 2 = 4".

Then for the 3rd loop the original "powered:= 1" variable is now "powered:= 4" so when the loop begins again "powered:= powered * table" now calculates as "4 x 2"?

Navigation

[#] Next page

Go to full version