Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:
    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
    Offline

    3
    ReputationRep:
    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.
    Offline

    0
    ReputationRep:
    You can easily put code within a for loop to do a simple calculation as you are describing, e.g.

    var
    i : Integer;
    isquare : Integer;

    begin
    // Loop 12 times
    For i := 1 to 12 do
    begin
    isquare := i*i;
    writeln('number = '+IntToStr(i)+' square = '+IntToStr(isquare));
    end;
    end;

    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
    Offline

    0
    ReputationRep:
    Thanks to everyone who replied

    I literally cracked the loop this morning, but thanks anyway. I think tangledweb, that would work.
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Would you like to hibernate through the winter months?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.