erro assingment in for loop lazarus / 21-General ---------------------------------------------------------------------- hvn52 · 2021-09-22 09:26 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 · 2021-09-22 09:43 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" ---------------------------------------------------------------------- howardpc · 2021-09-22 09:46 Perhaps you simply wantCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if (ReadKey <> #27) and (i > 4) then...,maybe enclosed in a while or repeat loop? ---------------------------------------------------------------------- Zvoni · 2021-09-22 09:48 Howard, his c2-thing is probably because he needs the input from ReadKey to process within the loop ---------------------------------------------------------------------- balazsszekely · 2021-09-22 09:52 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes,  Crt; var  C: Char;begin  repeat     C := Readkey;     if c <> #27 then     begin       Writeln('You typed the following key: ', C);       //do something     end;  until c = #27;end. ---------------------------------------------------------------------- Zvoni · 2021-09-22 09:55 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.... ---------------------------------------------------------------------- hvn52 · 2021-09-22 10:01 Quote from: Zvoni on September 22, 2021, 11:43:37 amShort 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 · 2021-09-22 10:01 Quote from: Zvoni on September 22, 2021, 11:48:51 amHoward, his c2-thing is probably because he needs the input from ReadKey to process within the loop Correct. ---------------------------------------------------------------------- alpine · 2021-09-22 10:30 Quote from: hvn52 on September 22, 2021, 12:01:06 pmThank 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for( i = 0; i < 4 && ( c2 = getch() ) != 27; i++ ){  // do something with c2}The Pascal equivalent should be: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i i := 0;c2 := ReadKey;while (i < 4) and (c2 <> #27) dobegin   // do something with c2  c2 := ReadKey;  i := i + 1;end;In Pascal the assignment ( := ) is a statement, not an operator, and you can't use it in an expression. Quote from: hvn52 on September 22, 2021, 12:01:06 pmWhich 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. ---------------------------------------------------------------------- Zvoni · 2021-09-22 10:34 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.. :-)) ---------------------------------------------------------------------- hvn52 · 2021-09-22 11:09 Quote from: Zvoni on September 22, 2021, 12:34:46 pmNow 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 · 2021-09-22 11:38 Other option using for .. in Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses crt;Type  TLoopRange=0..3;Var  c2:char;  i:integer; begin  for i in TLoopRange do    begin      c2:=readkey;    if c2=#27 then break;      // do loop things    end;end. Less error-prone than Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // For the fun of using a forfor i:=0 to 3 do  begin    c2:=readkey;    if c2=#27 then break       //do loop things  end; ---------------------------------------------------------------------- alpine · 2021-09-22 12:02 Quote from: hvn52 on September 22, 2021, 01:09:51 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. Quote from: hvn52 on September 22, 2021, 01:09:51 pmAnother 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  I: Integer;  B, C1, C2: Boolean;begin  for C1 := False to True do    for C2 := False to True do    begin      I := 0;      for B := not (C1 or C2) to (C1 and C2) do        Inc(I);      WriteLn(C1, ', ', C2, ' = ', I);       I := Ord(C1) + Ord(C2);      WriteLn(C1, ', ', C2, ' = ', I);    end;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. ---------------------------------------------------------------------- marcov · 2021-09-22 12:12 Quote from: hvn52 on September 22, 2021, 01:09:51 pmAnd 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 · 2021-09-22 13:49 Quote from: y.ivanov on September 22, 2021, 02:02:29 pmQuote from: hvn52 on September 22, 2021, 01:09:51 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. Quote from: hvn52 on September 22, 2021, 01:09:51 pmAnother 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  I: Integer;  B, C1, C2: Boolean;begin  for C1 := False to True do    for C2 := False to True do    begin      I := 0;      for B := not (C1 or C2) to (C1 and C2) do        Inc(I);      WriteLn(C1, ', ', C2, ' = ', I);       I := Ord(C1) + Ord(C2);      WriteLn(C1, ', ', C2, ' = ', I);    end;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. ---------------------------------------------------------------------- hvn52 · 2021-09-22 13:52 Quote from: marcov on September 22, 2021, 02:12:46 pmQuote from: hvn52 on September 22, 2021, 01:09:51 pmAnd when I can't find resources online like wiki,I have to ask. Strange. One would say the language guide is the primary documentation. Well, yes, but the examples in the documentation are quite basic, I think.