I am doing AQA Computing and I am using Pascal with Delphi. I understand all of the basic stuff and have been practicing for some time, and I can do the Repeat and While loops, but i just don't understand the For loop.
I can do basic for loops, for example "Display Hello 5 times" or "Ask the user to input a message and how many times they want to display it, then display the message that number of times". But I can't do past this.
For example, one question I am trying to do is "write a program to display the squares of all the integers from 1 to 12 in two columns labeled number and square number".
If anyone can do this, can you put your code in the reply?
Thanks in advance
Pascal For Loop Help Watch
- Thread Starter
- 11-04-2013 17:42
- 14-04-2013 12:52
I don't know pascal so can only provide pseudocode.
Go to your "Display Hello 5 times" program and do the following:
1) BEFORE the for loop output the string literal: "Number" followed by a tab followed by "Square Number". This is your title.
2) Make the for loop say Hello 12 times instead of 5. The obvious way to do this is using a variable initialised to 1 and iterating until the condition "variable does not equal 12" is untrue. If you can say hello five times you should be able to do this.
3) Change saying hello to writing: the number, a tab, the square of the number and then a newline.Last edited by The_Last_Melon; 14-04-2013 at 12:53.
- 14-04-2013 13:18
You can easily put code within a for loop to do a simple calculation as you are describing, e.g.
i : Integer;
isquare : Integer;
// Loop 12 times
For i := 1 to 12 do
isquare := i*i;
writeln('number = '+IntToStr(i)+' square = '+IntToStr(isquare));
Note: I haven't used Pascal for a very long time, and don't have a compiler on my PC now, so take the code with a pinch of salt, but it should help with the gist of it.
- Thread Starter
- 14-04-2013 18:55
Thanks to everyone who replied
I literally cracked the loop this morning, but thanks anyway. I think tangledweb, that would work.