HP Forums
"for" cycle as a function command - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: "for" cycle as a function command (/thread-11789.html)



"for" cycle as a function command - compsystems - 11-12-2018 08:42 PM

Hello
Xcas supports the definition of the "for" cycle as a function command FOR() ;, for many this type of instructions is more compact and readable, as IFTE() function cmd.

In the hpprime when writing the following sentences on history view, the system can interpret it.

PHP Code:
n:=1:; for j from 1 to 5 by 1 do n:=n*j endn
[enter] returns 120, 120 // ok

but the cycle for as a function test_for_2( ) does not.

PHP Code:
#cas
test_for1( ):=
begin
  local j
n;
  
:= 1
     for 
j from 1 to 5 by 1 do n:=n*jend;
  return 
n;
 
end;
#end 
[check] No errors in the program
test_for1( ) [enter] returns 120

PHP Code:
#cas
test_for2( ):=
begin
  local j
n;
  
:= 1
   for( 
j:=1j<=5j++ ) n:=n*;
  return 
n;
 
end;
#end 
[check] Error: Syntax Error line for( j:=1; j<=5; j++ )
test_for2( )


RE: "for" cycle as a function command - ijabbott - 11-13-2018 08:44 AM

(11-12-2018 08:42 PM)compsystems Wrote:  Hello
Xcas supports the definition of the "for" cycle as a function command FOR() ;, for many this type of instructions is more compact and readable, as IFTE() function cmd.

It's not really a function. It's just syntactic sugar to appease programmers familiar with C-like syntax a little bit.