Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lecture 4 - Programming With RR

Uploaded by

ehdidb2019
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 4 - Programming With RR

Uploaded by

ehdidb2019
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 222

Lecture 4:

Programming
with R

Professor
Amany E. Aly
Lecture 4: Programming with R
Programming
with R
Flow control
The f or()
loop
The if ()
statement
Professor Amany E. Aly
The while()
loop
Newton’s Department of Mathematics, Faculty of Science,
method for root
finding Helwan University, Ain Helwan, Cairo, Egypt.
The repeat
loop, and the
break and
next
statements
Managing
28 / 10 / 2024
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 1 / 111
Presentation Outline

Lecture 4:
Programming
with R 1 Programming with R
Professor
Amany E. Aly Flow control
The f or() loop
Programming
with R The if () statement
Flow control The while() loop
The f or()
loop Newton’s method for root finding
The if ()
statement The repeat loop, and the break and next statements
The while()
loop
Newton’s
method for root
Managing complexity through functions
finding What are functions?
The repeat
loop, and the Scope of variables
break and
next
statements
Returning multiple objects
Managing Using S3 classes to control printing
complexity
through
functions
The magrittr pipe operator
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 2 / 111
Lecture 4:
Programming
with R

Professor
Programming involves writing relatively complex systems of in-
Amany E. Aly
structions. There are two broad styles of programming:
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 3 / 111
Lecture 4:
Programming
with R

Professor
Programming involves writing relatively complex systems of in-
Amany E. Aly
structions. There are two broad styles of programming:
Programming
with R
Flow control
The imperative style (used in R, for example) involves stringing
The f or()
loop
The if ()
together instructions telling the computer what to do.
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 3 / 111
Lecture 4:
Programming
with R

Professor
Programming involves writing relatively complex systems of in-
Amany E. Aly
structions. There are two broad styles of programming:
Programming
with R
Flow control
The imperative style (used in R, for example) involves stringing
The f or()
loop
The if ()
together instructions telling the computer what to do.
statement
The while()
loop
The declarative style (used in HTML in web pages, for exam-
Newton’s
method for root
finding ple, and to some extent in ggplot2) involves writing a descrip-
The repeat
loop, and the
break and
next
tion of the end result, without giving the details about how to
statements
Managing
complexity
get there.
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 3 / 111
Lecture 4:
Programming Within each of these broad styles, there are many subdivisions,
with R

Professor
and a given program may involve aspects of several of them.
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 4 / 111
Lecture 4:
Programming Within each of these broad styles, there are many subdivisions,
with R

Professor
and a given program may involve aspects of several of them.
Amany E. Aly
For example, R programs may be
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 4 / 111
Lecture 4:
Programming Within each of these broad styles, there are many subdivisions,
with R

Professor
and a given program may involve aspects of several of them.
Amany E. Aly
For example, R programs may be
Programming
with R
Flow control
procedural (describing what steps to take to achieve a task)
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 4 / 111
Lecture 4:
Programming Within each of these broad styles, there are many subdivisions,
with R

Professor
and a given program may involve aspects of several of them.
Amany E. Aly
For example, R programs may be
Programming
with R
Flow control
procedural (describing what steps to take to achieve a task)
The f or()
loop
The if ()
modular (broken up into self-contained packages)
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 4 / 111
Lecture 4:
Programming Within each of these broad styles, there are many subdivisions,
with R

Professor
and a given program may involve aspects of several of them.
Amany E. Aly
For example, R programs may be
Programming
with R
Flow control
procedural (describing what steps to take to achieve a task)
The f or()
loop
The if ()
modular (broken up into self-contained packages)
statement
The while()
loop
Newton’s
object-oriented (organized to describe operations on complex
method for root
finding
The repeat
objects)
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 4 / 111
Lecture 4:
Programming Within each of these broad styles, there are many subdivisions,
with R

Professor
and a given program may involve aspects of several of them.
Amany E. Aly
For example, R programs may be
Programming
with R
Flow control
procedural (describing what steps to take to achieve a task)
The f or()
loop
The if ()
modular (broken up into self-contained packages)
statement
The while()
loop
Newton’s
object-oriented (organized to describe operations on complex
method for root
finding
The repeat
objects)
loop, and the
break and
next
statements
and/or functional (organized as a collection of functions which
Managing
complexity
through
functions
do specific calculations without having external side-effects)
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 4 / 111
Lecture 4:
Programming Within each of these broad styles, there are many subdivisions,
with R

Professor
and a given program may involve aspects of several of them.
Amany E. Aly
For example, R programs may be
Programming
with R
Flow control
procedural (describing what steps to take to achieve a task)
The f or()
loop
The if ()
modular (broken up into self-contained packages)
statement
The while()
loop
Newton’s
object-oriented (organized to describe operations on complex
method for root
finding
The repeat
objects)
loop, and the
break and
next
statements
and/or functional (organized as a collection of functions which
Managing
complexity
through
functions
do specific calculations without having external side-effects)
What are
functions?
Scope of
among other possibilities.
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 4 / 111
The f or() loop

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
The f or() statement allows one to specify that a certain operation
Flow control
The f or()
should be repeated a fixed number of times.
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 5 / 111
Lecture 4:
Programming
with R

Professor This sets a variable called name equal to each of the elements
Amany E. Aly
of vector, in sequence.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 6 / 111
Lecture 4:
Programming
with R

Professor This sets a variable called name equal to each of the elements
Amany E. Aly
of vector, in sequence.
Programming
with R
Flow control
For each value, whatever commands are listed within the curly
The f or()
loop
The if ()
braces will be performed.
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 6 / 111
Lecture 4:
Programming
with R

Professor This sets a variable called name equal to each of the elements
Amany E. Aly
of vector, in sequence.
Programming
with R
Flow control
For each value, whatever commands are listed within the curly
The f or()
loop
The if ()
braces will be performed.
statement
The while()
loop
Newton’s
The curly braces serve to group the commands so that they
method for root
finding
The repeat
are treated by R as a single command.
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 6 / 111
Lecture 4:
Programming
with R

Professor This sets a variable called name equal to each of the elements
Amany E. Aly
of vector, in sequence.
Programming
with R
Flow control
For each value, whatever commands are listed within the curly
The f or()
loop
The if ()
braces will be performed.
statement
The while()
loop
Newton’s
The curly braces serve to group the commands so that they
method for root
finding
The repeat
are treated by R as a single command.
loop, and the
break and
next
statements
If there is only one command to execute, the braces are not
Managing
complexity
through
needed.
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 6 / 111
Example 4.1

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The factorial n! counts how many ways n different objects could
The f or()
loop
The if ()
be ordered. It is defined as n! = 1 × 2 × 3...(n − 1) × n. One way
statement
The while()
loop
to calculate it would be to use a f or() statement.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 7 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
For example, we could find the value of 100! using the code
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 8 / 111
Lecture 4:
Programming
with R
The first line sets a variable named n to 100.
Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 9 / 111
Lecture 4:
Programming
with R
The first line sets a variable named n to 100.
Professor
Amany E. Aly
The second line initializes result to 1, i.e. a product with no
Programming
terms.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 9 / 111
Lecture 4:
Programming
with R
The first line sets a variable named n to 100.
Professor
Amany E. Aly
The second line initializes result to 1, i.e. a product with no
Programming
terms.
with R
Flow control The third line starts the f or() statement: the variable i will
The f or()
loop
The if ()
statement
be set to the values 1, 2, ..., n in succession.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 9 / 111
Lecture 4:
Programming
with R
The first line sets a variable named n to 100.
Professor
Amany E. Aly
The second line initializes result to 1, i.e. a product with no
Programming
terms.
with R
Flow control The third line starts the f or() statement: the variable i will
The f or()
loop
The if ()
statement
be set to the values 1, 2, ..., n in succession.
The while()
loop
Newton’s
Line four multiplies result by i in each of those steps, and the
method for root
finding
The repeat
final line prints it.
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 9 / 111
Lecture 4:
Programming
with R
The first line sets a variable named n to 100.
Professor
Amany E. Aly
The second line initializes result to 1, i.e. a product with no
Programming
terms.
with R
Flow control The third line starts the f or() statement: the variable i will
The f or()
loop
The if ()
statement
be set to the values 1, 2, ..., n in succession.
The while()
loop
Newton’s
Line four multiplies result by i in each of those steps, and the
method for root
finding
The repeat
final line prints it.
loop, and the
break and
next
statements
There is also a f actorial() function built in to R; it is much
Managing
complexity
through
faster than the f or() loop. But in many cases, you will want
functions
What are
functions? to write your own loops.
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 9 / 111
Example 4.2

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
In the 1960s, IBM sold a large computer called the System/360.
Flow control
The f or()
loop
(We do mean large: each computer filled a room.) Some programs
The if ()
statement
The while()
needed unpredictable numbers, so they used the following scheme
loop
Newton’s
method for root
(called RANDU):
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 10 / 111
Lecture 4:
Programming
with R
Start with an odd integer x between 1 and 231 − 1 inclusive.
Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 11 / 111
Lecture 4:
Programming
with R
Start with an odd integer x between 1 and 231 − 1 inclusive.
Professor
Amany E. Aly
Multiply it by 65539, and take the remainder when dividing by
Programming
231 . (These numbers have particularly simple binary represen-
with R
Flow control tations, so these operations are very fast.) This gives a new
The f or()
loop
The if ()
statement
value for x.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 11 / 111
Lecture 4:
Programming
with R
Start with an odd integer x between 1 and 231 − 1 inclusive.
Professor
Amany E. Aly
Multiply it by 65539, and take the remainder when dividing by
Programming
231 . (These numbers have particularly simple binary represen-
with R
Flow control tations, so these operations are very fast.) This gives a new
The f or()
loop
The if ()
statement
value for x.
The while()
loop
Newton’s
Repeat to generate a somewhat unpredictable sequence of
method for root
finding
The repeat
numbers. Divide them by 231 to put them on the scale from
loop, and the
break and
next
statements
0 to 1.
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 11 / 111
Lecture 4:
Programming
with R
Start with an odd integer x between 1 and 231 − 1 inclusive.
Professor
Amany E. Aly
Multiply it by 65539, and take the remainder when dividing by
Programming
231 . (These numbers have particularly simple binary represen-
with R
Flow control tations, so these operations are very fast.) This gives a new
The f or()
loop
The if ()
statement
value for x.
The while()
loop
Newton’s
Repeat to generate a somewhat unpredictable sequence of
method for root
finding
The repeat
numbers. Divide them by 231 to put them on the scale from
loop, and the
break and
next
statements
0 to 1.
Managing
complexity
through
functions
We can implement RANDU in a for loop. Suppose we want 1000
What are
functions?
Scope of
unpredictable numbers.
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 11 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 12 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
In the first line, we set up a vector to hold the results.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 13 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
In the first line, we set up a vector to hold the results.
with R
Flow control
The f or()
The second line sets a ”seed” value.
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 13 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
In the first line, we set up a vector to hold the results.
with R
Flow control
The f or()
The second line sets a ”seed” value.
loop
The if ()
statement The f or() loop in lines 3-6 first updates x, then divides it by
The while()
loop
Newton’s
method for root
231 and stores the result in the result vector.
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 13 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
In the first line, we set up a vector to hold the results.
with R
Flow control
The f or()
The second line sets a ”seed” value.
loop
The if ()
statement The f or() loop in lines 3-6 first updates x, then divides it by
The while()
loop
Newton’s
method for root
231 and stores the result in the result vector.
finding
The repeat
loop, and the
break and
This is done for entries 1 to 1000.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 13 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming We can plot these numbers, and it appears that RANDU works
with R
Flow control
The f or()
well
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 14 / 111
One thousand values from the RANDU algorithm.

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 15 / 111
Example 4.3

Lecture 4:
Programming
with R

Professor
Amany E. Aly The Fibonacci sequence is a famous sequence in mathematics.
Programming The first two elements are defined as [1,1].
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 16 / 111
Example 4.3

Lecture 4:
Programming
with R

Professor
Amany E. Aly The Fibonacci sequence is a famous sequence in mathematics.
Programming The first two elements are defined as [1,1].
with R
Flow control
The f or()
loop
Subsequent elements are defined as the sum of the preceding
The if ()
statement
The while()
two elements.
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 16 / 111
Example 4.3

Lecture 4:
Programming
with R

Professor
Amany E. Aly The Fibonacci sequence is a famous sequence in mathematics.
Programming The first two elements are defined as [1,1].
with R
Flow control
The f or()
loop
Subsequent elements are defined as the sum of the preceding
The if ()
statement
The while()
two elements.
loop
Newton’s
method for root
finding
For example, the third element is 2 (= 1+1), the fourth el-
The repeat
loop, and the
break and
ement is 3 (= 1+2), the fifth element is 5 (= 2+3), and so
next
statements
Managing
on.
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 16 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 17 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
To obtain the first 12 Fibonacci numbers in R, we can use
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 18 / 111
Example 4.4

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
Suppose a car dealer promotes two options for the purchase of
with R
Flow control
The f or()
a new $20,000 car. The first option is for the customer to pay
loop
The if ()
statement
up front and receive a $1000 rebate. The second option is ”0%-
The while()
loop
Newton’s
interest financing” where the customer makes 20 monthly pay-
method for root
finding
The repeat
ments of $1000 beginning in one month’s time.
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 19 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly Because of option 1, the effective price of the car is really $19,000,
Programming
so the dealer really is charging some interest rate i for option 2.
with R
Flow control
The f or()
We can calculate this value using the formula for the present value
loop
The if ()
statement
of an annuity:
The while()

1 − (1 − i)−20
loop  
Newton’s
method for root
finding 19000 = 1000 .
The repeat
loop, and the
break and
i
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 20 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
By multiplying both sides of this equation by i and dividing by
with R
Flow control
19,000, we get the form of a f ixed − point problem:
The f or()
loop

1 − (1 − i)−20
The if ()
 
statement
The while()
loop
i=
Newton’s
method for root
19
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 21 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming By taking an initial guess for i and plugging it into the right side
with R
Flow control
The f or()
of this equation, we can get an ”updated” value for i on the
loop
The if ()
statement
left. For example,if we start with i = 0.006, then our update is
−20
i = 1−(1−0.006)
The while()
loop
Newton’s
method for root 19 = 0.00593.
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 22 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
By plugging this updated value into the right-hand side of the
with R
Flow control
equation again, we get a new update:
The f or()
loop

1 − (1 − 0.00593)−20
The if ()
 
statement
The while()
loop
i= = 0.00586.
Newton’s
method for root
19
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 23 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
This kind of fixed-point iteration usually requires many iterations
The f or()
loop
The if ()
before we can be confident that we have the solution to the fixed-
statement
The while()
loop
point equation.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 24 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
Here is R code to work out the solution after 1000 iterations:
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 25 / 111
Example 4.5

Lecture 4:
Programming
with R

Professor
Amany E. Aly The f or() loop can be used in the programming of graphics. The
Programming
with R
next figure illustrates its use in the repeated pushing of viewports,
Flow control
The f or()
in the context of grid graphics. In this case, 100 viewports are
loop
The if ()
statement pushed, each has height and width which are 90% of the height
The while()
loop
Newton’s and width of the previous viewport, and each is positioned slightly
method for root
finding
The repeat
loop, and the
off-center, at the point (0.46, 0.5) relative to the previously pushed
break and
next
statements
viewport.
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 26 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
Also, each viewport has been rotated by a growing number of de-
Programming
with R grees, starting at 1, and increasing by 1, at each iteration. Circles
Flow control
The f or()
loop
having diameter equal to the height of the current viewport are
The if ()
statement
The while()
drawn at each iteration. The resulting pattern indicates the kind
loop
Newton’s
method for root of complex results that can be obtained from the grid approach
finding
The repeat
loop, and the
break and
to graphics, using only a few lines of code.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 27 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 28 / 111
Demonstration of rotation of viewports using a for loop.

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 29 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
Solve Exercises from 1 to 7 on pages 98 and 99.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 30 / 111
The if () statement

Lecture 4:
Programming
with R

Professor
Amany E. Aly
The if () statement allows us to control which statements are
Programming
with R
Flow control
executed, and sometimes this is more convenient.
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 31 / 111
The if () statement

Lecture 4:
Programming
with R

Professor
Amany E. Aly
The if () statement allows us to control which statements are
Programming
with R
Flow control
executed, and sometimes this is more convenient.
The f or()
loop
The if ()
Syntax:
statement

if (condition) {commands when TRUE}


The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 31 / 111
The if () statement

Lecture 4:
Programming
with R

Professor
Amany E. Aly
The if () statement allows us to control which statements are
Programming
with R
Flow control
executed, and sometimes this is more convenient.
The f or()
loop
The if ()
Syntax:
statement

if (condition) {commands when TRUE}


The while()
loop
Newton’s
method for root
finding
The repeat
if (condition) {commands when TRUE} else {commands when FALSE}
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 31 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
This statement causes a set of commands to be invoked if
with R
Flow control condition evaluates to T RU E.
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 32 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
This statement causes a set of commands to be invoked if
with R
Flow control condition evaluates to T RU E.
The f or()
loop
The if ()
statement
The else part is optional, and provides an alternative set of
The while()
loop
Newton’s
commands which are to be invoked in case the logical variable
method for root
finding
The repeat
is F ALSE.
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 32 / 111
Lecture 4:
Programming
with R

Professor
Be careful how you type this. If you put the else part on a new
Amany E. Aly
line, you may see something like this:
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 33 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
R executed the first three lines before you entered the else.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 34 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
R executed the first three lines before you entered the else.
with R
Flow control This meant it wasn’t expecting an else, and it signalled an
The f or()
loop
The if ()
statement
error.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 34 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
R executed the first three lines before you entered the else.
with R
Flow control This meant it wasn’t expecting an else, and it signalled an
The f or()
loop
The if ()
statement
error.
The while()
loop
Newton’s
To avoid this problem, a common convention for typing if...else
method for root
finding
The repeat
loop, and the
is to put the else on the same line as the previous closing brace:
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 34 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 35 / 111
Lecture 4:
Programming
with R

Professor
This way it is very clear that the statement is incomplete until
Amany E. Aly
the very end.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 36 / 111
Lecture 4:
Programming
with R

Professor
This way it is very clear that the statement is incomplete until
Amany E. Aly
the very end.
Programming
with R
Flow control
R also allows numerical values to be used as the value of
The f or()
loop
The if ()
condition.
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 36 / 111
Lecture 4:
Programming
with R

Professor
This way it is very clear that the statement is incomplete until
Amany E. Aly
the very end.
Programming
with R
Flow control
R also allows numerical values to be used as the value of
The f or()
loop
The if ()
condition.
statement
The while()
loop These are converted to logical values using the rule that zero
Newton’s
method for root
finding
The repeat
becomes F ALSE, and any other value becomes T RU E.
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 36 / 111
Lecture 4:
Programming
with R

Professor
This way it is very clear that the statement is incomplete until
Amany E. Aly
the very end.
Programming
with R
Flow control
R also allows numerical values to be used as the value of
The f or()
loop
The if ()
condition.
statement
The while()
loop These are converted to logical values using the rule that zero
Newton’s
method for root
finding
The repeat
becomes F ALSE, and any other value becomes T RU E.
loop, and the
break and
next
statements
Missing values are not allowed for the condition, and will trig-
Managing
complexity
through
ger an error.
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 36 / 111
Example 4.6

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 37 / 111
Example 4.6

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
Since x > 2 is T RU E, y is assigned 2 × 3 = 6.
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 37 / 111
Example 4.6

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
Since x > 2 is T RU E, y is assigned 2 × 3 = 6.
method for root
finding
The repeat
loop, and the
If it hadn’t been true, y would have been assigned the value
break and
next
statements of 3 × x.
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 37 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
The if () statement is often used inside user-defined functions.
loop
The if ()
statement
The following is a typical example.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 38 / 111
Example 4.7

Lecture 4:
Programming
with R

Professor
Amany E. Aly
The correlation between two vectors of numbers is often calculated
Programming
using the cor() function. It is supposed to give a measure of linear
with R
Flow control
association. We can add a scatterplot of the data as follows.
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 39 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
We can apply this function to two vectors without plotting by
Programming
with R
Flow control
typing
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 40 / 111
Example 4.8

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The function that follows is based on the sieve of Eratosthenes,
The f or()
loop
The if ()
the oldest known systematic method for listing prime numbers up
statement
The while()
loop
to a given value n.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 41 / 111
Lecture 4:
Programming The idea is as follows:
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 42 / 111
Lecture 4:
Programming The idea is as follows:
with R

Professor
Begin with a vector of numbers from 2 to n.
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 42 / 111
Lecture 4:
Programming The idea is as follows:
with R

Professor
Begin with a vector of numbers from 2 to n.
Amany E. Aly
Beginning with 2, eliminate all multiples of 2 which are larger
Programming
with R than 2.
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 42 / 111
Lecture 4:
Programming The idea is as follows:
with R

Professor
Begin with a vector of numbers from 2 to n.
Amany E. Aly
Beginning with 2, eliminate all multiples of 2 which are larger
Programming
with R than 2.
Flow control
The f or()
loop Then move to the next number remaining in the vector, in this
The if ()
statement
The while()
loop
case, 3.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 42 / 111
Lecture 4:
Programming The idea is as follows:
with R

Professor
Begin with a vector of numbers from 2 to n.
Amany E. Aly
Beginning with 2, eliminate all multiples of 2 which are larger
Programming
with R than 2.
Flow control
The f or()
loop Then move to the next number remaining in the vector, in this
The if ()
statement
The while()
loop
case, 3.
Newton’s
method for root
finding
Now, remove all multiples of 3 which are larger than 3.
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 42 / 111
Lecture 4:
Programming The idea is as follows:
with R

Professor
Begin with a vector of numbers from 2 to n.
Amany E. Aly
Beginning with 2, eliminate all multiples of 2 which are larger
Programming
with R than 2.
Flow control
The f or()
loop Then move to the next number remaining in the vector, in this
The if ()
statement
The while()
loop
case, 3.
Newton’s
method for root
finding
Now, remove all multiples of 3 which are larger than 3.
The repeat
loop, and the
break and
Proceed through all remaining entries of the vector in this way.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 42 / 111
Lecture 4:
Programming The idea is as follows:
with R

Professor
Begin with a vector of numbers from 2 to n.
Amany E. Aly
Beginning with 2, eliminate all multiples of 2 which are larger
Programming
with R than 2.
Flow control
The f or()
loop Then move to the next number remaining in the vector, in this
The if ()
statement
The while()
loop
case, 3.
Newton’s
method for root
finding
Now, remove all multiples of 3 which are larger than 3.
The repeat
loop, and the
break and
Proceed through all remaining entries of the vector in this way.
next
statements
Managing
The entry for 4 would have been removed in the first round,
complexity
through
functions
What are
leaving 5 as the next entry to work with after 3;
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 42 / 111
Lecture 4:
Programming The idea is as follows:
with R

Professor
Begin with a vector of numbers from 2 to n.
Amany E. Aly
Beginning with 2, eliminate all multiples of 2 which are larger
Programming
with R than 2.
Flow control
The f or()
loop Then move to the next number remaining in the vector, in this
The if ()
statement
The while()
loop
case, 3.
Newton’s
method for root
finding
Now, remove all multiples of 3 which are larger than 3.
The repeat
loop, and the
break and
Proceed through all remaining entries of the vector in this way.
next
statements
Managing
The entry for 4 would have been removed in the first round,
complexity
through
functions
What are
leaving 5 as the next entry to work with after 3;
functions?
Scope of
variables
All multiples of 5 would be removed at the next step, and so
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 42 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 43 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
Here are some examples of the use of this function:
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 44 / 111
Understanding the code

Lecture 4:
Programming
with R
The purpose of the function is to provide all prime numbers up to
Professor the given value n. The basic idea of the program is contained in
Amany E. Aly
the lines:
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 45 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
The sieve object holds all the candidates for testing.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 46 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
The sieve object holds all the candidates for testing.
Programming
with R Initially, all integers from 2 through n are stored in this vector.
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 46 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
The sieve object holds all the candidates for testing.
Programming
with R Initially, all integers from 2 through n are stored in this vector.
Flow control
The f or()
loop
The if ()
The primes object is set up initially empty, eventually to con-
statement
The while()
loop
tain all of the primes that are less than or equal to n.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 46 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
The sieve object holds all the candidates for testing.
Programming
with R Initially, all integers from 2 through n are stored in this vector.
Flow control
The f or()
loop
The if ()
The primes object is set up initially empty, eventually to con-
statement
The while()
loop
tain all of the primes that are less than or equal to n.
Newton’s
method for root
finding The composite numbers in sieve are removed, and the primes
The repeat
loop, and the
break and
next
are copied to primes.
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 46 / 111
Lecture 4:
Programming
with R Each integer i from 2 through n is checked in sequence to see
Professor
Amany E. Aly whether it is still in the vector.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 47 / 111
Lecture 4:
Programming
with R Each integer i from 2 through n is checked in sequence to see
Professor
Amany E. Aly whether it is still in the vector.
Programming
with R
The any() function returns a T RU E if at least one of the
Flow control
The f or()
loop
logical vector elements in its argument is T RU E.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 47 / 111
Lecture 4:
Programming
with R Each integer i from 2 through n is checked in sequence to see
Professor
Amany E. Aly whether it is still in the vector.
Programming
with R
The any() function returns a T RU E if at least one of the
Flow control
The f or()
loop
logical vector elements in its argument is T RU E.
The if ()
statement
The while()
In the case that i is still in the sieve vector, it must be a prime,
loop
Newton’s
method for root since it is the smallest number that has not been eliminated
finding
The repeat
loop, and the
break and
yet.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 47 / 111
Lecture 4:
Programming
with R Each integer i from 2 through n is checked in sequence to see
Professor
Amany E. Aly whether it is still in the vector.
Programming
with R
The any() function returns a T RU E if at least one of the
Flow control
The f or()
loop
logical vector elements in its argument is T RU E.
The if ()
statement
The while()
In the case that i is still in the sieve vector, it must be a prime,
loop
Newton’s
method for root since it is the smallest number that has not been eliminated
finding
The repeat
loop, and the
break and
yet.
next
statements
Managing
complexity
All multiples of i are eliminated, since they are necessarily
through
functions
What are
composite, and i is appended to primes
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 47 / 111
Lecture 4:
Programming
with R The expression (sieve %%i) == 0 would give T RU E for all
Professor
Amany E. Aly elements of sieve which are multiples of i;
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 48 / 111
Lecture 4:
Programming
with R The expression (sieve %%i) == 0 would give T RU E for all
Professor
Amany E. Aly elements of sieve which are multiples of i;
Programming
with R
since we want to eliminate these elements and save all other
Flow control
The f or()
loop
elements, we can negate this using !(sieve %%i) == 0 or
The if ()
statement
The while()
sieve %%i = 0.
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 48 / 111
Lecture 4:
Programming
with R The expression (sieve %%i) == 0 would give T RU E for all
Professor
Amany E. Aly elements of sieve which are multiples of i;
Programming
with R
since we want to eliminate these elements and save all other
Flow control
The f or()
loop
elements, we can negate this using !(sieve %%i) == 0 or
The if ()
statement
The while()
sieve %%i = 0.
loop
Newton’s
method for root
Then we can eliminate all multiples of i from the sieve vector
finding
The repeat
loop, and the
break and
using
next
statements
Managing
sieve < −sieve[(sieve%%i)! = 0]
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 48 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
Note that this eliminates i as well, but we have already saved it
with R
Flow control in primes.
The f or()
loop
The if ()
If the supplied argument n is less than 2, then the function output
statement
The while()
loop would be meaningless. To avoid this, we cause an error to be
Newton’s
method for root
finding
The repeat
triggered if a value smaller than 2 is supplied by the user
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 49 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
Solve Exercises from 1 to 5 on page 102.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 50 / 111
The while() loop

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Sometimes we want to repeat statements, but the pattern of rep-
Flow control
The f or()
loop
etition isn’t known in advance. We need to do some calculations
The if ()
statement
The while()
and keep going as long as a condition holds. The while() state-
loop
Newton’s
method for root
ment accomplishes this.
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 51 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
while (condition) {statements}
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 52 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
while (condition) {statements}
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The condition is evaluated.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 52 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
while (condition) {statements}
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The condition is evaluated.
The while()
loop
Newton’s
method for root
If it evaluates to F ALSE, nothing more is done.
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 52 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
while (condition) {statements}
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The condition is evaluated.
The while()
loop
Newton’s
method for root
If it evaluates to F ALSE, nothing more is done.
finding
The repeat
loop, and the
break and
If it evaluates to T RU E the statements are executed.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 52 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
while (condition) {statements}
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The condition is evaluated.
The while()
loop
Newton’s
method for root
If it evaluates to F ALSE, nothing more is done.
finding
The repeat
loop, and the
break and
If it evaluates to T RU E the statements are executed.
next
statements
Managing
complexity
condition is evaluated again , and the process is repeated.
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 52 / 111
Example 4.9

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Suppose we want to list all Fibonacci numbers less than 300. We
Flow control
The f or()
loop
don’t know beforehand how long this list is, so we wouldn’t know
The if ()
statement
The while()
how to stop the f or() loop at the right time, but a while() loop
loop
Newton’s
method for root
is perfect:
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 53 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 54 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly Increasing the length of a vector element by element as in
Programming Fibonacci
with R
Flow control
The f or()
< −c(F ibonacci, F ib2) in a f or() or while() loop should be
loop
The if ()
statement
avoided if the number of such operations is likely to be large.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 55 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly Increasing the length of a vector element by element as in
Programming Fibonacci
with R
Flow control
The f or()
< −c(F ibonacci, F ib2) in a f or() or while() loop should be
loop
The if ()
statement
avoided if the number of such operations is likely to be large.
The while()
loop
Newton’s
method for root
R will have to keep allocating new vectors, each one element
finding
The repeat
loop, and the
longer than the last one, and this will slow the process down
break and
next
statements substantially.
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 55 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
Solve Exercises 1 to 5 on page 104.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 56 / 111
Newton’s method for root finding

Lecture 4:
Programming
with R

Professor
Amany E. Aly Newton’s method is a popular numerical method to find a root
Programming of an algebraic equation f (x) = 0
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 57 / 111
Newton’s method for root finding

Lecture 4:
Programming
with R

Professor
Amany E. Aly Newton’s method is a popular numerical method to find a root
Programming of an algebraic equation f (x) = 0
with R
Flow control
The f or()
If f (x) has derivative f 0 (x), then the following iteration should
loop
The if ()
statement converge to a root of the above equation if started close e-
The while()
loop
Newton’s nough to the root.
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 57 / 111
Newton’s method for root finding

Lecture 4:
Programming
with R

Professor
Amany E. Aly Newton’s method is a popular numerical method to find a root
Programming of an algebraic equation f (x) = 0
with R
Flow control
The f or()
If f (x) has derivative f 0 (x), then the following iteration should
loop
The if ()
statement converge to a root of the above equation if started close e-
The while()
loop
Newton’s nough to the root.
method for root
finding
The repeat
loop, and the
x0 = initial guess
xn − xn−1 − ff0(x n−1 )
break and
next
statements
Managing (xn−1 )
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 57 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
The idea is based on the Taylor approximation
with R

f (xn ) ≈ f (xn−1 ) − (xn − xn−1 )f 0 (xn−1 )


Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
Newton’s method is equivalent to setting f (xn ) = 0 and solving
method for root
finding
The repeat
for xn .
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 58 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
We hope that the solution xn should give a close approximation
with R
Flow control to the root.
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 59 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
We hope that the solution xn should give a close approximation
with R
Flow control to the root.
The f or()
loop
The if ()
statement
There are many examples where xn will fail to converge to a
The while()
loop
Newton’s
root unless xn−1 is already sufficiently close, and some where
method for root
finding
The repeat
it will fail regardless of the starting value.
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 59 / 111
Example 4.10

Lecture 4:
Programming
with R

Professor
Amany E. Aly Suppose f (x) = x3 + 2x2 − 7. Then, if x0 is close enough to one
Programming
with R
of the three roots of this equation,
Flow control
The f or()
loop x3n−1 + 2x2n−1 − 7
The if ()
statement xn − xn−1 −
The while()
loop 3x2 + 4x
Newton’s
method for root
finding
The repeat
will converge to a root.
loop, and the
break and
next
statements
An R version of this could be implemented as follows:
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 60 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 61 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
We start with x equal to x0 , and evaluate f (x0 ).
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 62 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
We start with x equal to x0 , and evaluate f (x0 ).
Programming Then, as long as |f (xi )| is more than 0.000001, we update x
with R
Flow control
The f or()
using Newton’s method.
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 62 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
We start with x equal to x0 , and evaluate f (x0 ).
Programming Then, as long as |f (xi )| is more than 0.000001, we update x
with R
Flow control
The f or()
using Newton’s method.
loop
The if ()
statement Notice that we don’t need a variable i.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 62 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
We start with x equal to x0 , and evaluate f (x0 ).
Programming Then, as long as |f (xi )| is more than 0.000001, we update x
with R
Flow control
The f or()
using Newton’s method.
loop
The if ()
statement Notice that we don’t need a variable i.
The while()
loop
Newton’s
method for root
Because Newton’s method is a recursive formula where the
finding
The repeat
loop, and the
break and
new value can be calculated from the old one, there is no need
next
statements
Managing
to know which iteration we are on when doing the updates.
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 62 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
Solve Exercises from 1 to 7 on pages 105 and 106.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 63 / 111
The repeat loop, and the break and next statements

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming Sometimes we don’t want a fixed number of repetitions of a


with R
Flow control
The f or()
loop, and we don’t want to put the test at the top of the loop
loop
The if ()
statement
the way it is in a while() loop.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 64 / 111
The repeat loop, and the break and next statements

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming Sometimes we don’t want a fixed number of repetitions of a


with R
Flow control
The f or()
loop, and we don’t want to put the test at the top of the loop
loop
The if ()
statement
the way it is in a while() loop.
The while()
loop
Newton’s
method for root
In this situation we can use a repeat loop. This loop repeats
finding
The repeat
loop, and the
until we execute a break statement.
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 64 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly Syntax
Programming repeat statements
with R
Flow control
The f or()
This causes the statements to be repeated endlessly. The state-
loop
The if ()
statement
ments should normally include a break statement, typically in the
The while()
loop
Newton’s
form
method for root
finding
The repeat
if (condition) break
loop, and the
break and
next
statements
but this is not a requirement of the syntax.
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 65 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
The break statement causes the loop to terminate immedi-
with R
Flow control
ately.
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 66 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
The break statement causes the loop to terminate immedi-
with R
Flow control
ately.
The f or()
loop
The if ()
statement
break statements can also be used in f or() and while() loops.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 66 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
The break statement causes the loop to terminate immedi-
with R
Flow control
ately.
The f or()
loop
The if ()
statement
break statements can also be used in f or() and while() loops.
The while()
loop
Newton’s
The next statement causes control to return immediately to
method for root
finding
The repeat
loop, and the
the top of the loop; it can also be used in any loop.
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 66 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly The repeat loop and the break and next statements are used
Programming relatively infrequently.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 67 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly The repeat loop and the break and next statements are used
Programming relatively infrequently.
with R
Flow control
The f or()
It is usually easier to understand code when the test is clearly
loop
The if ()
statement displayed at the top, and when that is the only exit from the
The while()
loop
Newton’s
method for root
loop (as in the f or and while loops).
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 67 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly The repeat loop and the break and next statements are used
Programming relatively infrequently.
with R
Flow control
The f or()
It is usually easier to understand code when the test is clearly
loop
The if ()
statement displayed at the top, and when that is the only exit from the
The while()
loop
Newton’s
method for root
loop (as in the f or and while loops).
finding
The repeat
loop, and the However, sometimes these statements help to make programs
break and
next
statements
Managing
clearer.
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 67 / 111
Example 4.11

Lecture 4:
Programming
with R
We can repeat the Newton’s algorithm example using a repeat
Professor loop:
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
This version removes the need to duplicate the line that calculates
Scope of
variables
Professor
f.
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 68 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
Solve Exercises 1 and 2 page 107 .
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 69 / 111
What are functions?

Lecture 4:
Programming
with R

Professor Functions are self-contained units of R code with a well-defined


Amany E. Aly
purpose.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 70 / 111
What are functions?

Lecture 4:
Programming
with R

Professor Functions are self-contained units of R code with a well-defined


Amany E. Aly
purpose.
Programming
with R
Flow control In general, functions take inputs, do calculations (possibly
The f or()
loop
The if () printing intermediate results, drawing graphs, calling other
statement
The while()
loop functions, etc.), and produce outputs.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 70 / 111
What are functions?

Lecture 4:
Programming
with R

Professor Functions are self-contained units of R code with a well-defined


Amany E. Aly
purpose.
Programming
with R
Flow control In general, functions take inputs, do calculations (possibly
The f or()
loop
The if () printing intermediate results, drawing graphs, calling other
statement
The while()
loop functions, etc.), and produce outputs.
Newton’s
method for root
finding
The repeat
If the inputs and outputs are well defined, the programmer
loop, and the
break and
next
statements
can be reasonably sure whether the function works or not:
Managing
complexity
through
and once it works, can move on to the next problem.
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 70 / 111
Example 4.12

Lecture 4:
Programming
with R

Professor
Amany E. Aly Suppose payments of R dollars are deposited annually into a bank
Programming
with R
account which earns constant interest i per year. What is the
Flow control
The f or()
accumulated value of the account at the end of n years, supposing
loop
The if ()
statement deposits are made at the end of each year?
The while()
loop
Newton’s The total amount at the end of n years is
n
R(1 + i)n−1 + ... + R(1 + i) + R = R (1+i)i −1
method for root
finding
The repeat
loop, and the
break and
next
statements
An R function to calculate the amount of an annuity is
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 71 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop If $400 is deposited annually for 10 years into an account bearing
The if ()
statement
The while()
loop
5% annual interest, we can calculate the accumulated amount
Newton’s
method for root
finding
using
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 72 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
R is somewhat unusual among computer languages in that func-
The f or()
loop
The if ()
tions are objects that can be manipulated like other more common
statement
The while()
loop
objects such as vectors, matrices, and lists.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 73 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
The definition of a function normally has the following structure:
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 74 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
The definition of a function normally has the following structure:
Programming
with R
Flow control the word f unction
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 74 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
The definition of a function normally has the following structure:
Programming
with R
Flow control the word f unction
The f or()
loop
The if ()
statement
a pair of round parentheses() which enclose the argument
The while()
loop
Newton’s
list. The list may be empty.
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 74 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
The definition of a function normally has the following structure:
Programming
with R
Flow control the word f unction
The f or()
loop
The if ()
statement
a pair of round parentheses() which enclose the argument
The while()
loop
Newton’s
list. The list may be empty.
method for root
finding
The repeat
loop, and the
a single statement, or a sequence of statements enclosed in
break and
next
statements curly braces{}.
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 74 / 111
Lecture 4:
Programming Like other R objects, functions are usually named.
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 75 / 111
Lecture 4:
Programming Like other R objects, functions are usually named.
with R

Professor You should choose the names of your functions to succinctly


Amany E. Aly
describe the action of the function.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 75 / 111
Lecture 4:
Programming Like other R objects, functions are usually named.
with R

Professor You should choose the names of your functions to succinctly


Amany E. Aly
describe the action of the function.
Programming
with R
Flow control
For example, var() computes variances, and median() com-
The f or()
loop
The if ()
putes medians.
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 75 / 111
Lecture 4:
Programming Like other R objects, functions are usually named.
with R

Professor You should choose the names of your functions to succinctly


Amany E. Aly
describe the action of the function.
Programming
with R
Flow control
For example, var() computes variances, and median() com-
The f or()
loop
The if ()
putes medians.
statement
The while()
loop
The name is important: if you choose a name that you can’t
Newton’s
method for root
finding
remember, you will waste time looking it up later.
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 75 / 111
Lecture 4:
Programming Like other R objects, functions are usually named.
with R

Professor You should choose the names of your functions to succinctly


Amany E. Aly
describe the action of the function.
Programming
with R
Flow control
For example, var() computes variances, and median() com-
The f or()
loop
The if ()
putes medians.
statement
The while()
loop
The name is important: if you choose a name that you can’t
Newton’s
method for root
finding
remember, you will waste time looking it up later.
The repeat
loop, and the
break and
If you choose a name that is misleading about the action of
next
statements
Managing
complexity
the function (e.g. if we had named our annuityAmt function
through
functions
What are
annuityRate), you will find your programs extremely hard to
functions?
Scope of
variables
understand.
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 75 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
When R executes a function definition, it produces an object with
The f or()
loop
The if ()
three parts: the header, the body, and a reference to the environ-
statement
The while()
loop
ment in which the definition was executed.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 76 / 111
Lecture 4:
Programming
with R

Professor
The first two items in the function definition are used to create
Amany E. Aly
the header.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 77 / 111
Lecture 4:
Programming
with R

Professor
The first two items in the function definition are used to create
Amany E. Aly
the header.
Programming
with R
Flow control
The header to an R function describes the inputs, or
The f or()
loop
The if ()
”arguments.”
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 77 / 111
Lecture 4:
Programming
with R

Professor
The first two items in the function definition are used to create
Amany E. Aly
the header.
Programming
with R
Flow control
The header to an R function describes the inputs, or
The f or()
loop
The if ()
”arguments.”
statement
The while()
loop For example, the header of our Eratosthenes function above
Newton’s
method for root
finding
The repeat
is f unction(n).
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 77 / 111
Lecture 4:
Programming
with R

Professor
The first two items in the function definition are used to create
Amany E. Aly
the header.
Programming
with R
Flow control
The header to an R function describes the inputs, or
The f or()
loop
The if ()
”arguments.”
statement
The while()
loop For example, the header of our Eratosthenes function above
Newton’s
method for root
finding
The repeat
is f unction(n).
loop, and the
break and
next
statements
This tells us that the function takes one argument named n,
Managing
complexity
through
which specifies the upper limit for the sieve.
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 77 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
The header on annuityAmt is f unction(n, R, i), telling us
Flow control
The f or()
that it takes three arguments named n, R, and i.
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 78 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
The header on annuityAmt is f unction(n, R, i), telling us
Flow control
The f or()
that it takes three arguments named n, R, and i.
loop
The if ()
statement
The while()
Functions may take any number of arguments.
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 78 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
The header on annuityAmt is f unction(n, R, i), telling us
Flow control
The f or()
that it takes three arguments named n, R, and i.
loop
The if ()
statement
The while()
Functions may take any number of arguments.
loop
Newton’s
method for root
finding
Again, choose their names to indicate their function.
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 78 / 111
Lecture 4:
Programming
with R
To reduce the burden on the user of a function, we may give
Professor
Amany E. Aly
default values to some arguments: if the user doesn’t specify
Programming
the value, the default will be used.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 79 / 111
Lecture 4:
Programming
with R
To reduce the burden on the user of a function, we may give
Professor
Amany E. Aly
default values to some arguments: if the user doesn’t specify
Programming
the value, the default will be used.
with R
Flow control For example, we could have used the header
The f or()
loop
The if ()
statement
The while() annuityAmt < −f unction(n, R = 1, i = 0.01)
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 79 / 111
Lecture 4:
Programming
with R
To reduce the burden on the user of a function, we may give
Professor
Amany E. Aly
default values to some arguments: if the user doesn’t specify
Programming
the value, the default will be used.
with R
Flow control For example, we could have used the header
The f or()
loop
The if ()
statement
The while() annuityAmt < −f unction(n, R = 1, i = 0.01)
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements To indicate that if a user called annuityAmt(24) without
Managing
complexity
through
functions
specifying R and i, then we should act as though R = 1 and
What are
functions?
Scope of
i = 0.01.
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 79 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
The second part of a function is the body.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 80 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
The second part of a function is the body.
with R
Flow control
The f or()
This is a single statement, or sequence of statements in curly
loop
The if ()
statement
braces.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 80 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
The second part of a function is the body.
with R
Flow control
The f or()
This is a single statement, or sequence of statements in curly
loop
The if ()
statement
braces.
The while()
loop
Newton’s
method for root
They specify what computations are to be carried out by the
finding
The repeat
loop, and the
function.
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 80 / 111
Lecture 4:
Programming In the original Eratosthenes example, the body was
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 81 / 111
Lecture 4:
Programming
with R At some point in the body of the function there is normally
Professor
Amany E. Aly a statement like return(primes) which specifies the output
Programming value of the function.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 82 / 111
Lecture 4:
Programming
with R At some point in the body of the function there is normally
Professor
Amany E. Aly a statement like return(primes) which specifies the output
Programming value of the function.
with R
Flow control
The f or() In R all functions produce a single output.
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 82 / 111
Lecture 4:
Programming
with R At some point in the body of the function there is normally
Professor
Amany E. Aly a statement like return(primes) which specifies the output
Programming value of the function.
with R
Flow control
The f or() In R all functions produce a single output.
loop
The if ()
statement
The while()
In some other languages functions may produce no output, or
loop
Newton’s
method for root
multiple outputs.
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 82 / 111
Lecture 4:
Programming
with R At some point in the body of the function there is normally
Professor
Amany E. Aly a statement like return(primes) which specifies the output
Programming value of the function.
with R
Flow control
The f or() In R all functions produce a single output.
loop
The if ()
statement
The while()
In some other languages functions may produce no output, or
loop
Newton’s
method for root
multiple outputs.
finding
The repeat
loop, and the
break and
If there is no return() statement, then the value of the last
next
statements
Managing
statement executed is returned.
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 82 / 111
Lecture 4:
Programming
with R At some point in the body of the function there is normally
Professor
Amany E. Aly a statement like return(primes) which specifies the output
Programming value of the function.
with R
Flow control
The f or() In R all functions produce a single output.
loop
The if ()
statement
The while()
In some other languages functions may produce no output, or
loop
Newton’s
method for root
multiple outputs.
finding
The repeat
loop, and the
break and
If there is no return() statement, then the value of the last
next
statements
Managing
statement executed is returned.
complexity
through
functions
What are
This is how annuityAmt returns its value.
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 82 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly The third part of a function is the hardest part to understand,
Programming because it is the least concrete: the environment of the func-
with R
Flow control
The f or()
tion.
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 83 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly The third part of a function is the hardest part to understand,
Programming because it is the least concrete: the environment of the func-
with R
Flow control
The f or()
tion.
loop
The if ()
statement
The while()
We won’t give a complete description here, but will limit our-
loop
Newton’s
method for root
selves to the following circular definition: the environment is
finding
The repeat
loop, and the
a reference to the environment in which the function was de-
break and
next
statements fined.
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 83 / 111
Lecture 4:
Programming
with R In our Eratosthenes function, we made use of two quite d-
Professor
Amany E. Aly ifferent sorts of objects: n, sieve, primes and i were all
Programming defined locally within the function.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 84 / 111
Lecture 4:
Programming
with R In our Eratosthenes function, we made use of two quite d-
Professor
Amany E. Aly ifferent sorts of objects: n, sieve, primes and i were all
Programming defined locally within the function.
with R
Flow control
The f or()
loop
There is no ambiguity about what they mean.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 84 / 111
Lecture 4:
Programming
with R In our Eratosthenes function, we made use of two quite d-
Professor
Amany E. Aly ifferent sorts of objects: n, sieve, primes and i were all
Programming defined locally within the function.
with R
Flow control
The f or()
loop
There is no ambiguity about what they mean.
The if ()
statement
The while()
But seq, c, any, return, and stop are not defined there:
loop
Newton’s
method for root they were part of the R environment where Eratosthenes
finding
The repeat
loop, and the
break and
was defined.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 84 / 111
Lecture 4:
Programming
with R In our Eratosthenes function, we made use of two quite d-
Professor
Amany E. Aly ifferent sorts of objects: n, sieve, primes and i were all
Programming defined locally within the function.
with R
Flow control
The f or()
loop
There is no ambiguity about what they mean.
The if ()
statement
The while()
But seq, c, any, return, and stop are not defined there:
loop
Newton’s
method for root they were part of the R environment where Eratosthenes
finding
The repeat
loop, and the
break and
was defined.
next
statements
Managing
complexity
They are all functions, and the local variables were not: this
through
functions
What are
is commonly the case, but it is by no means necessary.
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 84 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
Within the Eratosthenes function, we could have defined a
Programming
with R new function. Its environment would include n, sieve, primes,
Flow control
The f or()
loop
and i.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 85 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
Within the Eratosthenes function, we could have defined a
Programming
with R new function. Its environment would include n, sieve, primes,
Flow control
The f or()
loop
and i.
The if ()
statement
The while()
loop
For example, we might want to make the removal of multiples
Newton’s
method for root
finding
of the prime values clearer by putting that operation into a
The repeat
loop, and the
break and
small function called noM ultiples:
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 85 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 86 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
The noM ultiples function defines j in its header, so j is a local
loop
The if ()
statement
variable, and it finds sieve in its environment
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 87 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
Solve Exercises from 1 to 3 on page 111.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 88 / 111
Scope of variables

Lecture 4:
Programming
with R

Professor
Amany E. Aly
The ”scope” of a variable tells us where the variable would be
Programming
with R recognized.
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 89 / 111
Scope of variables

Lecture 4:
Programming
with R

Professor
Amany E. Aly
The ”scope” of a variable tells us where the variable would be
Programming
with R recognized.
Flow control
The f or()
loop
The if ()
For example, variables defined within functions have local s-
statement
The while()
loop
cope, so they are only recognized within the function.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 89 / 111
Scope of variables

Lecture 4:
Programming
with R

Professor
Amany E. Aly
The ”scope” of a variable tells us where the variable would be
Programming
with R recognized.
Flow control
The f or()
loop
The if ()
For example, variables defined within functions have local s-
statement
The while()
loop
cope, so they are only recognized within the function.
Newton’s
method for root
finding
The repeat
A variable could be created with the same name in a different
loop, and the
break and
next
function but there is no risk of a clash.
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 89 / 111
Example 4.13

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
In this example we create two functions f and g, both with local
The f or()
loop
The if ()
variables named x. g is called by f and modifies its instance of x
statement
The while()
loop
without affecting the x in f .
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 90 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 91 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
In R, scope is controlled by the environment of functions.
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 92 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
In R, scope is controlled by the environment of functions.
with R
Flow control
The f or()
Variables defined at the console have global scope, meaning
loop
The if ()
statement
they are visible in any user-defined function.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 92 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
In R, scope is controlled by the environment of functions.
with R
Flow control
The f or()
Variables defined at the console have global scope, meaning
loop
The if ()
statement
they are visible in any user-defined function.
The while()
loop
Newton’s
method for root
Variables defined in a function are visible in the function, and
finding
The repeat
loop, and the
in functions defined within it.
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 92 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
Using local variables rather than globals helps tremendously
Programming
with R in programming, because while you are writing the function,
Flow control
The f or()
loop
you don’t need to worry about some other part of the program
The if ()
statement
The while()
changing the values of your variables.
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 93 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
Using local variables rather than globals helps tremendously
Programming
with R in programming, because while you are writing the function,
Flow control
The f or()
loop
you don’t need to worry about some other part of the program
The if ()
statement
The while()
changing the values of your variables.
loop
Newton’s
method for root
finding
You can predict their values from the code you write, and be
The repeat
loop, and the
break and
sure that once you get it correct, you can trust it.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 93 / 111
Lecture 4:
Programming
with R We have mentioned that function arguments can have default
Professor
Amany E. Aly values.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 94 / 111
Lecture 4:
Programming
with R We have mentioned that function arguments can have default
Professor
Amany E. Aly values.
Programming
with R
In fact, the default values can refer to any local variables in
Flow control
The f or()
loop
the function.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 94 / 111
Lecture 4:
Programming
with R We have mentioned that function arguments can have default
Professor
Amany E. Aly values.
Programming
with R
In fact, the default values can refer to any local variables in
Flow control
The f or()
loop
the function.
The if ()
statement
The while()
This works because the arguments aren’t evaluated until they
loop
Newton’s
method for root
finding
are first used; at that point, you need to make sure the default
The repeat
loop, and the
break and
value exists.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 94 / 111
Lecture 4:
Programming
with R We have mentioned that function arguments can have default
Professor
Amany E. Aly values.
Programming
with R
In fact, the default values can refer to any local variables in
Flow control
The f or()
loop
the function.
The if ()
statement
The while()
This works because the arguments aren’t evaluated until they
loop
Newton’s
method for root
finding
are first used; at that point, you need to make sure the default
The repeat
loop, and the
break and
value exists.
next
statements
Managing
complexity
If the argument is never used, the default will never be evalu-
through
functions
What are
ated.
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 94 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
Packages have their own scope (known as their ”namespace”),
loop
The if ()
statement
but the details of namespaces are beyond the scope of this text.
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 95 / 111
Returning multiple objects

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
R functions always return a single object. But sometimes you
The f or()
loop
The if ()
want to return more than one thing! The trick here is to return
statement
The while()
loop
them in a list() or vector.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 96 / 111
Example 4.14

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R For example, our annuityAmt() function simply calculated the
Flow control
The f or()
loop
amount of the annuity at the end of n years. But we might also
want to know the present value, which is (1 + i)−n times the
The if ()
statement
The while()
loop
Newton’s
method for root
amount. We return both in this function:
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 97 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 98 / 111
Using S3 classes to control printing

Lecture 4:
Programming
with R

Professor In the annuityV alues() example, the display of the results


Amany E. Aly
wasn’t very pleasing.
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 99 / 111
Using S3 classes to control printing

Lecture 4:
Programming
with R

Professor In the annuityV alues() example, the display of the results


Amany E. Aly
wasn’t very pleasing.
Programming
with R
Flow control To produce a nicer looking display, we could use a number of
The f or()
loop
The if () calls to the cat() or print() functions instead of returning the
statement
The while()
loop list of values.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 99 / 111
Using S3 classes to control printing

Lecture 4:
Programming
with R

Professor In the annuityV alues() example, the display of the results


Amany E. Aly
wasn’t very pleasing.
Programming
with R
Flow control To produce a nicer looking display, we could use a number of
The f or()
loop
The if () calls to the cat() or print() functions instead of returning the
statement
The while()
loop list of values.
Newton’s
method for root
finding
The repeat
But then the function would be less useful in other calcula-
loop, and the
break and
next
statements
tions: other functions might want to do further calculations
Managing
complexity
through
on the results, without printing them.
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 99 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
A general solution to this problem is to declare the type of the
Programming
with R return value, and to tell R how to print things like that.
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 100 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
A general solution to this problem is to declare the type of the
Programming
with R return value, and to tell R how to print things like that.
Flow control
The f or()
loop This is a kind of object oriented programming.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 100 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
A general solution to this problem is to declare the type of the
Programming
with R return value, and to tell R how to print things like that.
Flow control
The f or()
loop This is a kind of object oriented programming.
The if ()
statement
The while()
loop
In fact, there are several ways to do this, and a full discussion
Newton’s
method for root
finding is beyond the scope of this text, but in this section we will
The repeat
loop, and the
break and
next
show one of the simplest ways: using S3 methods.
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 100 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
Besides having values, objects in R may have named attributes.
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 101 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
Flow control
Besides having values, objects in R may have named attributes.
The f or()
loop
The if ()
The S3 class is (in the simplest case) a character string named
statement
The while()
loop class containing the class of the object.
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 101 / 111
Example 4.15

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
We can assign the class to our annuity values with code like this:
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 102 / 111
Lecture 4:
Programming
with R To make use of it, we need to define one or more methods. We’ll
Professor
Amany E. Aly define a print-method:
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 103 / 111
Understanding the code

Lecture 4:
Programming
with R
In the S3 system, there are ”generic functions” like print()
Professor
Amany E. Aly and methods like print.annuity().
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 104 / 111
Understanding the code

Lecture 4:
Programming
with R
In the S3 system, there are ”generic functions” like print()
Professor
Amany E. Aly and methods like print.annuity().
Programming
with R By convention, the method is named with the name of the
Flow control
The f or()
loop
generic function, a dot, then the name of the class.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 104 / 111
Understanding the code

Lecture 4:
Programming
with R
In the S3 system, there are ”generic functions” like print()
Professor
Amany E. Aly and methods like print.annuity().
Programming
with R By convention, the method is named with the name of the
Flow control
The f or()
loop
generic function, a dot, then the name of the class.
The if ()
statement
The while()
loop
When R prints the value, it sees that the values object is of
Newton’s
method for root
finding
class ”annuity”, and calls the method for that class,
The repeat
loop, and the
break and
print.annuity().
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 104 / 111
Understanding the code

Lecture 4:
Programming
with R
In the S3 system, there are ”generic functions” like print()
Professor
Amany E. Aly and methods like print.annuity().
Programming
with R By convention, the method is named with the name of the
Flow control
The f or()
loop
generic function, a dot, then the name of the class.
The if ()
statement
The while()
loop
When R prints the value, it sees that the values object is of
Newton’s
method for root
finding
class ”annuity”, and calls the method for that class,
The repeat
loop, and the
break and
print.annuity().
next
statements
Managing
complexity
When we extract the PV component from the list, we no longer
through
functions
What are
have an ”annuity” object and it prints as a number.
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 104 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly The arguments to a print method need to be compatible with
Programming
the generic function; print.classname(x, ...) is generally a
with R
Flow control
The f or()
good choice.
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 105 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly The arguments to a print method need to be compatible with
Programming
the generic function; print.classname(x, ...) is generally a
with R
Flow control
The f or()
good choice.
loop
The if ()
statement
By convention print methods should return the value being
The while()
loop
Newton’s
printed, but they need to do it in a way so that it doesn’t print
method for root
finding
The repeat
loop, and the
again, or you’d get an infinite loop.
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 105 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly The arguments to a print method need to be compatible with
Programming
the generic function; print.classname(x, ...) is generally a
with R
Flow control
The f or()
good choice.
loop
The if ()
statement
By convention print methods should return the value being
The while()
loop
Newton’s
printed, but they need to do it in a way so that it doesn’t print
method for root
finding
The repeat
loop, and the
again, or you’d get an infinite loop.
break and
next
statements The invisible() function marks a result so that it won’t print.
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 105 / 111
The magrittr pipe operator

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
The magrittr package defines the ”pipe operator” % > %,
Flow control
The f or()
loop
and many other packages also make these available.
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 106 / 111
The magrittr pipe operator

Lecture 4:
Programming
with R

Professor
Amany E. Aly

Programming
with R
The magrittr package defines the ”pipe operator” % > %,
Flow control
The f or()
loop
and many other packages also make these available.
The if ()
statement
The while() The idea behind % > % is fairly simple: rather than typing a
loop
Newton’s
method for root
finding
function call as f (x, y), it is typed as x % > % f (y).
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 106 / 111
Lecture 4:
Programming
with R

Professor
We can read this as ”start with x, and use f (y) to modify it.” This
Amany E. Aly
sometimes (as in Example 3.16 using leaflet) leads to more read-
Programming
with R
able code than the equivalent standard notation. In that example
Flow control
The f or()
loop
we wrote
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 107 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly
instead of the equivalent
Programming
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
Using the pipe makes the successive calculations more obvious.
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 108 / 111
Lecture 4:
Programming
with R

Professor The pipe operator is particularly helpful when used with func-
Amany E. Aly
tions whose main purpose is to modify their first argument,
Programming
with R
Flow control
and the tidyverse packages described in Chapter 5 were de-
The f or()
loop
The if ()
signed with this in mind.
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 109 / 111
Lecture 4:
Programming
with R

Professor The pipe operator is particularly helpful when used with func-
Amany E. Aly
tions whose main purpose is to modify their first argument,
Programming
with R
Flow control
and the tidyverse packages described in Chapter 5 were de-
The f or()
loop
The if ()
signed with this in mind.
statement
The while()
loop However, any function can be used with % > % : if the object
Newton’s
method for root
finding that is being ”piped in” isn’t the first argument of the function,
The repeat
loop, and the
break and
next
the special placeholder can be used to mark where it should
statements
Managing
complexity
appear.
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 109 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly For example, the base graphics code to draw the boxplot in Figure
Programming
with R
3.9 was
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 110 / 111
Lecture 4:
Programming
with R

Professor
Amany E. Aly But if library(magrittr) had been used to make the pipe operator
Programming available, it could have been written as
with R
Flow control
The f or()
loop
The if ()
statement
The while()
loop
Newton’s
method for root
finding
The repeat
loop, and the
break and
next
statements
Managing
complexity
through
functions
What are
functions?
Scope of
variables
Professor
ReturningAmany E. Aly ( Department of Mathematics, Faculty of Science,
LectureHelwan
4: Programming
University,with
Ain R
Helwan, Cairo, Egypt.) 28 / 10 / 2024 111 / 111

You might also like