Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Replacing for-loops

I've got to write a program that replaces any for-loops in a program with repeat-loops. The input is a valid Pascal program entered line-by-line (so I'm assuming the conversion will happen after each line entered). Any thoughts, partial algorithms, etc. to get me going in the right direction? We cannot use strings or arrays, just reading in characters.
1 Solution
Is this program reading in a text file (for example, something like chadd.pas), or are you typing in at the keyboard?  Also, correct me if I'm not fully comprehending this, but basically is sounds like your program is to replace a statement like:

  for i:=1 to 10 do
    write('Hey maw, get off the dang roof!');


    write('Hey maw, get off the dang roof!');
  until i>10;


Just give me a little clarification, and I should be able to present you with some ideas...  Thanks!
please clearify your question
If you can't use strings and arrays, you will need to use a (linked) list or stack to allow for nested loops.  The rest is straightforward.

chadd082197Author Commented:
The conversion you presented is exactly right..... the intent is to have the user type in each line at the keyboard and have the program perform the conversion line-by-line (after each Return key). If the line doesn't contain a for-statement, it just prints it as is. Also, we're supposed to read each char.
OK, here we go...  First you are going to neat a huge repeat ... until someBoolean;  Then, in this huge repeat, you need these statements:

  repeat until keypressed;

ch is declared as a char up in your variable section.  This will take in a keystroke from the keyboard and save it in ch.  You can then do:


and do a big case for ch.  When the user types a for loop, they need to have a begin and end too, even if it is just a one line for loop, else you won't know where to put the until ... at.  You will need to store each line in an array or some kind of data structure.  I suggest a simple string (which is an array of characters, obviously).  so, do a var blah:string;

start off with string:='';

then, after each keystroke (if not ENTER), do:  string:=string+ch;  If they type enter, you will need to reset string.  To tell what key they are entering, you will need a case statement, like I said, which looks at the ordinal value (ASCII value) of the key typed.  So, a case ord(ch) of ...  ENTER = 13, characters = 65..81 (?) and something or other to something or other.  In any case, once the user hits return, look at your string, search it for the word "for".  If you find that, you will need to save the variable name used and the limiting values (i.e. "for i:=1 to 10"  save "i","1" and "10").  You will then print out to the screen:

  i:=1;  {or whatever variable := whatever lower limit}

then you will let then enter at will until they enter the corresponding end, at which case you'll have it print:

  until i=10;  {until variable = upper limit}

Of course, a stack would be a useful data structure to use, since nested for loops or if statements using begins and ends could cause your program to return funky results.  It does not sound like a terribly difficult project, but do not get me wrong, it is not terribly simple either.  If you have any further questions just ask...  good luck!!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now