0% found this document useful (0 votes)
5 views

Programming Approach - 4 - Programming Approach

Uploaded by

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

Programming Approach - 4 - Programming Approach

Uploaded by

adele.a.scolaro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Programming Approach

FUNDAMENTALS OF SCRIPTING
What is Programming?
• A set of instructions that specifies a computation (algorithm)
• A solution to a problem
• Your job
• Instruct the computer to do things

• Basically, writing software (computer programs) is describing how to do something


• In its simplest form, it is a lot like writing down the steps it takes to do something - a process,
a procedure
Scripting Language vs
Programming Language
• Scripting languages are also programming languages
• A particular trait of scripting languages is that they do not require the compilation step and
are rather interpreted
• For example, a C program needs to be compiled before being executed
• On the other hand a script written in languages such as Python or JavaScript need not be
compiled and can be run immediately, with the interpreter processing the code line by line
• Generally compiled programs run faster than interpreted ones because they are first
converted into native machine code
Typical Applications and Features of
Scripting Languages
• To automate certain tasks in a system
• Extracting information from a dataset
• Less code intensive as compared to traditional programming languages
• Usually easier to write and debug
Typical Applications and Features of
Programming Languages
• More complex systems implemented from scratch
• More difficult to write and debug
• More lines of code
• Can interface directly with the hardware, thus running faster
Programming Structures
• Every program you write is made up of three basic building blocks:
• Sequence
• Selection
• Repetition

Selection Repetition Sequence


Sequence
• Normally the computer executes our code in order, one line at a time from the
top to the bottom of our programs
• It will start at line 1, then execute line 2 then line 3 and so on till it reaches the
last line of your program
• We don't need to do anything extra for this to happen
Selection
• Sometimes we want some lines of code to be run only if a condition is met,
otherwise we want the computer to ignore these lines and jump over them or
execute something else
• This is achieved using if statements
if x == y:
print("x is equal to y")
else:
print("x and y are not equal")
Repetition
• Also known as Iteration
• Sometimes we want the computer to execute the same lines of code several
times
• This is done using a loops, of which there are two types in Python: for loops and
while loops
• Loops are convenient because they enable us not to have to copy the same lines
of code many times
Errors
• In the computer world, programming errors are also known as known as bugs
• The process of tracking bugs down is called debugging
• Three kinds of errors can occur in a program:
• Syntax errors
• If a program contains syntax error, it will not pass compilation
• Semantic (logical) errors
• If a program contains only semantic errors, it means that it can pass compilation, but does not do what it meant to
do
• Runtime errors
• Runtime errors occur while the program is running, causing the program to crash if not handled properly
Syntax Errors
• The statement is not well-formed and the interpreter cannot parse it
• In the example below there is a missing double quote before the last bracket
Semantic Errors
• That's when the Python interpreter is able to parse the code, it's
actually able to run it, but it doesn't produce the thing that the
programmer intended
• In the example below we were expecting to get “One half as a
percentage is 50%” as an answer
Runtime Errors
• Runtime errors occur when the interpreter is able to parse the program,
however while the program is running, some illegal operation happens
• In the example below, we are trying to divide by zero which does not
make sense (well, most of the time)
• This will usually crash the program or raise an exception if exception
handling is being used in the program
x = 6
y = 0
print(x/y)

You might also like