Code Efficiency
What is
efficient code?
 A program can be written in may different ways.
 Although the different solutions achieve the same
result, programmers should try and write code that is
efficient.
 Efficient code is best explained in examples...
Repetition
This is notefficient
code.
It does the same
thing 5 times.
How could it be
made more
efficient?
Repetition
By using a loop the
code is more
efficient.
This is especially the
case when looping a
large number of
times.
1DArrays
This is notefficient
code.
It uses 6 variables to
storethenames of 6
users.
How could it be
made more
efficient?
1DArrays
A 1D array can be
used to store the 6
names.
This makes the code
more efficient.
Selection
This is notefficient
code.
It makes 4
comparisons every
time.
How could it be
made more
efficient?
Selection
By using elif and
else statements, the
code will not always
run all 4
comparisons
This makes the code
more efficient.
Logical
Operators
Splitting the if
statement into 2
like this is not the
best solution.
How else might it
be done?
Logical
Operators
It is betterpractice
to use and / or to
createa complex if
statement.
More readable code

N5 Code Efficiency