Recent

Author Topic: erro assingment in for loop  (Read 1881 times)

hvn52

  • Newbie
  • Posts: 6
erro assingment in for loop
« on: September 22, 2021, 11:26:43 am »
Hi,

I try to create a for loop like this:

for i := 0 to ((i > 4) and ((c2 := ReadKey) <> #27))) do

and the error states Fatal: Syntax error, ")" expected but ":=" found. After a lot of searching about for-loop, compound conditions etc, I can't find if this for loop is possible in FPC. Can someone help me please?

Thanks

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: erro assingment in for loop
« Reply #1 on: September 22, 2021, 11:43:37 am »
Short answer: No!

and ":=" is assignment, not comparison. It should have been "c2 = ReadKey"

This looks like you're trying to translate a For-Loop from C

Your best bet is to refactor it to a While..Do --> https://wiki.freepascal.org/WHILE..DO

Right now i'm trying (and failing) to understand how that loop is supposed to work with i starting from 0 but your first conditional being "i>4"

EDIT: Figured out what's supposed to happen with "c2:=ReadKey"
« Last Edit: September 22, 2021, 11:47:36 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: erro assingment in for loop
« Reply #2 on: September 22, 2021, 11:46:29 am »
Perhaps you simply want
Code: Pascal  [Select][+][-]
  1. if (ReadKey <> #27) and (i > 4) then...
,maybe enclosed in a while or repeat loop?

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: erro assingment in for loop
« Reply #3 on: September 22, 2021, 11:48:51 am »
Howard,
his c2-thing is probably because he needs the input from ReadKey to process within the loop
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

balazsszekely

  • Guest
Re: erro assingment in for loop
« Reply #4 on: September 22, 2021, 11:52:52 am »
It's not clear what are you trying to achieve. I guess you meant to read a key until Esc is pressed:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes,
  10.   Crt;
  11.  
  12. var
  13.   C: Char;
  14. begin
  15.   repeat
  16.      C := Readkey;
  17.      if c <> #27 then
  18.      begin
  19.        Writeln('You typed the following key: ', C);
  20.        //do something
  21.      end;
  22.   until c = #27;
  23. end.
  24.  

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: erro assingment in for loop
« Reply #5 on: September 22, 2021, 11:55:06 am »
GetMem,
yeah, figured that out too, but where does the "i>4" comes in? Which is the loop-variable. Starting at 0.... not making any sense....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

hvn52

  • Newbie
  • Posts: 6
Re: erro assingment in for loop
« Reply #6 on: September 22, 2021, 12:01:06 pm »
Short answer: No!

and ":=" is assignment, not comparison. It should have been "c2 = ReadKey"

This looks like you're trying to translate a For-Loop from C

Your best bet is to refactor it to a While..Do --> https://wiki.freepascal.org/WHILE..DO

Right now i'm trying (and failing) to understand how that loop is supposed to work with i starting from 0 but your first conditional being "i>4"

EDIT: Figured out what's supposed to happen with "c2:=ReadKey"

Thank you for your response. I tried with Repeat Until, but got the same error on the ":=". And like Zvoni and GetMem already pointed out, using Readkey I want to read a key until Esc is pressed. Which works fine for me in another piece of code.

hvn52

  • Newbie
  • Posts: 6
Re: erro assingment in for loop
« Reply #7 on: September 22, 2021, 12:01:41 pm »
Howard,
his c2-thing is probably because he needs the input from ReadKey to process within the loop

Correct.

alpine

  • Hero Member
  • *****
  • Posts: 1032
Re: erro assingment in for loop
« Reply #8 on: September 22, 2021, 12:30:14 pm »
Thank you for your response. I tried with Repeat Until, but got the same error on the ":=". And like Zvoni and GetMem already pointed out, using Readkey I want to read a key until Esc is pressed. Which works fine for me in another piece of code.

Guessing your C loop is something like:
Code: C  [Select][+][-]
  1. for( i = 0; i < 4 && ( c2 = getch() ) != 27; i++ )
  2. {
  3.   // do something with c2
  4. }
The Pascal equivalent should be:
Code: Pascal  [Select][+][-]
  1. i := 0;
  2. c2 := ReadKey;
  3. while (i < 4) and (c2 <> #27) do
  4. begin
  5.    // do something with c2
  6.   c2 := ReadKey;
  7.   i := i + 1;
  8. end;
In Pascal the assignment ( := ) is a statement, not an operator, and you can't use it in an expression.

Which works fine for me in another piece of code.
You should familiarize yourself more with the C syntax. The C-for loop is different from Pascal-for loop and it is actually a shorthand for the while() statement.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: erro assingment in for loop
« Reply #9 on: September 22, 2021, 12:34:46 pm »
Now we're getting somewhere, though ivanov turned the condition around ("i>4" vs "i<4")
Let's see what's correct.

That "i>4" in the first post still throws me for a loop (Oh the irony.. :-))
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

hvn52

  • Newbie
  • Posts: 6
Re: erro assingment in for loop
« Reply #10 on: September 22, 2021, 01:09:51 pm »
Now we're getting somewhere, though ivanov turned the condition around ("i>4" vs "i<4")
Let's see what's correct.

That "i>4" in the first post still throws me for a loop (Oh the irony.. :-))

The exact condition doesn't really matter in this example (although, yes it actually is a '<'), but the important conclusion is that I can't use ':=' in a for-loop, Another is that I can't use a logical operator such as 'and' in a for-loop either.
I'm well aware that C has different properties than Pascal, but I like to try things. And when I can't find resources online like wiki,I have to ask.

So, thank you for your help.

damieiro

  • Full Member
  • ***
  • Posts: 200
Re: erro assingment in for loop
« Reply #11 on: September 22, 2021, 01:38:32 pm »
Other option using for .. in
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses crt;
  3. Type
  4.   TLoopRange=0..3;
  5. Var
  6.   c2:char;
  7.   i:integer;
  8.  
  9. begin
  10.   for i in TLoopRange do
  11.     begin
  12.       c2:=readkey;
  13.       if c2=#27 then break;
  14.       // do loop things
  15.     end;
  16. end.  
  17.  

Less error-prone than
Code: Pascal  [Select][+][-]
  1. // For the fun of using a for
  2. for i:=0 to 3 do
  3.   begin
  4.     c2:=readkey;
  5.     if c2=#27 then break
  6.    
  7.     //do loop things
  8.   end;
« Last Edit: September 22, 2021, 01:40:22 pm by damieiro »

alpine

  • Hero Member
  • *****
  • Posts: 1032
Re: erro assingment in for loop
« Reply #12 on: September 22, 2021, 02:02:29 pm »
*snip*
but the important conclusion is that I can't use ':=' in a for-loop,
Inability to use assignment in expressions gives you much less chances to screw up the things with regards to the evaluation order, boolean shortcut evaluation, etc.

Another is that I can't use a logical operator such as 'and' in a for-loop either.
Not true. Boolean is an enumerated type and can be used into for-loop as any other:
Code: Pascal  [Select][+][-]
  1. var
  2.   I: Integer;
  3.   B, C1, C2: Boolean;
  4. begin
  5.   for C1 := False to True do
  6.     for C2 := False to True do
  7.     begin
  8.       I := 0;
  9.       for B := not (C1 or C2) to (C1 and C2) do
  10.         Inc(I);
  11.       WriteLn(C1, ', ', C2, ' = ', I);
  12.  
  13.       I := Ord(C1) + Ord(C2);
  14.       WriteLn(C1, ', ', C2, ' = ', I);
  15.     end;
  16. end.
Consider the loop on line 9 which will be evaluated zero, one or two times, thus counting the true expressions in C1, C2. Of course this can be achieved by simply adding the two (line 13), but somebody can figure more elaborated example.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: erro assingment in for loop
« Reply #13 on: September 22, 2021, 02:12:46 pm »
And when I can't find resources online like wiki,I have to ask.

Strange. One would say the language guide is the primary documentation.

hvn52

  • Newbie
  • Posts: 6
Re: erro assingment in for loop
« Reply #14 on: September 22, 2021, 03:49:27 pm »
*snip*
but the important conclusion is that I can't use ':=' in a for-loop,
Inability to use assignment in expressions gives you much less chances to screw up the things with regards to the evaluation order, boolean shortcut evaluation, etc.

Another is that I can't use a logical operator such as 'and' in a for-loop either.
Not true. Boolean is an enumerated type and can be used into for-loop as any other:
Code: Pascal  [Select][+][-]
  1. var
  2.   I: Integer;
  3.   B, C1, C2: Boolean;
  4. begin
  5.   for C1 := False to True do
  6.     for C2 := False to True do
  7.     begin
  8.       I := 0;
  9.       for B := not (C1 or C2) to (C1 and C2) do
  10.         Inc(I);
  11.       WriteLn(C1, ', ', C2, ' = ', I);
  12.  
  13.       I := Ord(C1) + Ord(C2);
  14.       WriteLn(C1, ', ', C2, ' = ', I);
  15.     end;
  16. end.
Consider the loop on line 9 which will be evaluated zero, one or two times, thus counting the true expressions in C1, C2. Of course this can be achieved by simply adding the two (line 13), but somebody can figure more elaborated example.

Thank you for pointing this out.

 

TinyPortal © 2005-2018