Reading to and
Writing from files
openRead + open a file to read
readline return line of text
from the file
->
a
myfile-openRead ("sample .
Ext")
X= file readline
my
.
file
. close
my
↳ makes
X first line of ext file
endOffile() determines end of a
file
file openRead ("sample
It'l
my
: .
while NOT
my
file. end
Of file )
print (myfile . readLine()
endwhile
myfile . close)
↳
prints contents of sample Ixt .
openWrite -> open a file to write to
writeline add line
of text to file
-
my Gle
=
open Write ("sample Ixt") .
file WriteLine ("Hello
S
World"
my .
file close
my .
↳ hello world is made the contents . It
of sample (previous contents are overwritten
, 2 2 1 . .
Programming techniques
programmingconstruc
its are used to represent a
program's control flow in the
popular subsection of procedural programming called structured
programming
:
Sequence
↳ code executed
line-by-line , top to bottom
Branching
certain block of code is if specific condition is met
If
using
↳ a run a
,
statements.
↳
also known as selection
Iteration a block of code is executed a certain number ofhimes or while a
condition is met
↳ Iteration uses COR ,
WHILE or REPEAT UNTIL loops
↳ Iteration can be either :
count-controlled : iteration number of times
is
given a
-
for i in (0 10
range ,
print i
next i
↳ condition-controlled : iteration continues until condition is
a
given
met
While i = 20 :
print "Not true" :
i = i + 1
endwhile
Recursion :
↳ construct in which a subroutine calls itself during its execution
condition-called the
until
stopping
↳ continues a certain condition is met
↳ suited to certain problems which
produces the same result as iteration , but more are
more
easily expressed using recursion
Advantage:
they can be represented using fewer lines of code so less prone to errors
↳
,
However it is essential that recursive subroutines are to
defined clearly
↳ so as
reach a
stopping condition after a
finite number of
function calls
Common example of a
naturally recursive
function factorial (number
functionisfactoria function calls itself
,
number == O or I stack is created within
if a new frame
return / the call stack , where parameters
,
else : local variables and return addresses
return number # factorial (number-1) ; are .
stored
end if
end function
Writing from files
openRead + open a file to read
readline return line of text
from the file
->
a
myfile-openRead ("sample .
Ext")
X= file readline
my
.
file
. close
my
↳ makes
X first line of ext file
endOffile() determines end of a
file
file openRead ("sample
It'l
my
: .
while NOT
my
file. end
Of file )
print (myfile . readLine()
endwhile
myfile . close)
↳
prints contents of sample Ixt .
openWrite -> open a file to write to
writeline add line
of text to file
-
my Gle
=
open Write ("sample Ixt") .
file WriteLine ("Hello
S
World"
my .
file close
my .
↳ hello world is made the contents . It
of sample (previous contents are overwritten
, 2 2 1 . .
Programming techniques
programmingconstruc
its are used to represent a
program's control flow in the
popular subsection of procedural programming called structured
programming
:
Sequence
↳ code executed
line-by-line , top to bottom
Branching
certain block of code is if specific condition is met
If
using
↳ a run a
,
statements.
↳
also known as selection
Iteration a block of code is executed a certain number ofhimes or while a
condition is met
↳ Iteration uses COR ,
WHILE or REPEAT UNTIL loops
↳ Iteration can be either :
count-controlled : iteration number of times
is
given a
-
for i in (0 10
range ,
print i
next i
↳ condition-controlled : iteration continues until condition is
a
given
met
While i = 20 :
print "Not true" :
i = i + 1
endwhile
Recursion :
↳ construct in which a subroutine calls itself during its execution
condition-called the
until
stopping
↳ continues a certain condition is met
↳ suited to certain problems which
produces the same result as iteration , but more are
more
easily expressed using recursion
Advantage:
they can be represented using fewer lines of code so less prone to errors
↳
,
However it is essential that recursive subroutines are to
defined clearly
↳ so as
reach a
stopping condition after a
finite number of
function calls
Common example of a
naturally recursive
function factorial (number
functionisfactoria function calls itself
,
number == O or I stack is created within
if a new frame
return / the call stack , where parameters
,
else : local variables and return addresses
return number # factorial (number-1) ; are .
stored
end if
end function