Week 9
Week 9
Week 8
CS 1101
1
Introduction to
Programming Languages
2
Programmers (or coders) are individuals who work
with one or more programming languages to write
code that is used as the basis for the applications
that you use on your computing device.
Program Sequence
A program is just a sequence of instructions for your
computer to perform.
For example, you might want to add two user-entered
numbers together and display the sum on the screen.
In plain English, the program might look something like
this:
1) Clear the current display.
2) Ask the user for the first number and store that number for
subsequent use.
3) Ask the user for the second number and store that number for
subsequent use.
4) Retrieve the two stored numbers and add them together.
5) Display the result.
Cont.
In designing a program, we have to consider how input, processing, and output are all
clearly defined.
1) Clear the current display.
2) Output to the screen the instructions for the operation.
3) Ask the user for the first number.
4) Verify that the entered value is a number:
a) If it is, proceed.
b) If it is not, remind the user what the valid range is and prompt again.
5) Store that number for subsequent use.
6) Ask the user for the second number.
7) Verify that the entered value is a number:
a) If it is, proceed.
b) If it is not, remind the user what the valid range is and prompt again.
8) Store that number for subsequent use.
9) Retrieve the two stored numbers and add them together.
10) Display the result.
Using a Flow Chart
As this restatement of the
program is getting
significantly more complex,
it might help to visualize it.
You could view the sequence
as a graphical flow chart to
help understand the
processes.
Features of Programming Code
■ The main routine calls some subroutines. Each routine is completed by an
"End Routine" statement. This means (for example) when the program reaches
the last step of the main routine, it closes rather than flowing through to try to
execute the first subroutine.
■ When a subroutine completes, it can return to the point in the main
routine from where it was called, and the main routine continues execution.
Note that when we use structures such as this, we have to be very careful not
to create infinite loops in the code by mistake.
■ There is a conditional statement (IF) that means part of the code only
executes when certain conditions are true or false.
■ There are variables to store data input by the user.
Features of Programming Code
■ There are functions (such as "sum" and "write") that we can assume are provided as
features of the programming language. We don't need to code how to add two numbers
together or write output to the display screen.
■ There is a user interface that the program interacts with (prompting for input and
displaying output).
■ It contains comments, preceded by the ' character. Comments are part of the programming
code that are not executed by the computer but that help the developer read and maintain
the code.
Programming Languages
The following pseudocode would add a value to the first element in the array:
Logons(0) = find LastLoggedOnUser and get FirstName
Programming Concepts- Containers
In the previous example, the array has a single dimension.
Arrays can also be multidimensional, which you can visualize as like a table with multiple
columns.
Note that all the elements must be the same data type.
For example, the following code creates a two-dimensional array: declare Logons(9,1) as
string
The following pseudocode would populate the first "row" in the array, with the first name
going into the first column and the logon time into the second:
Logons(0,0) = find LastLoggedOnUser and get FirstName
Logons(0,1) = find LastLoggedOnUser and get Time
One of the limitations of the array container type is that it cannot be resized. Most
programming languages support container types called vectors that can grow or shrink in
size as elements are added or removed.
Programming Concepts- Branches
Your program runs from the start to the end unless you instruct it to
deviate from this path.
One way of doing so is to create a branch; this is an instruction to your
computer to execute a different sequence of instructions.
You use branches to control the flow within your program.
For example, you might create a branch based on a condition. We saw
this in our earlier example when we verified a number had been entered
correctly. If it had, then something happened, and if it had not, then
something else happened. This is a conditional branch.
For example, in the following pseudocode, the value of a variable called
DisplayNumber is compared to 25. If DisplayNumber is greater than 25,
then a variable called Count is incremented by 1. If DisplayNumber is less
than 25, no action occurs and the variable Count remains the same.
If DisplayNumber > 25 Then
Count = Count+1
End If
Programming Concepts- Loops
Loops are similar to branches in as much as they deviate from the initial program path according to
some sort of logic condition.
However, with a loop, you instruct your computer to perform, or repeat, a task until a condition is
met.
For example, you might create a loop that continues until a certain amount of time has elapsed or
until a counter reaches a certain level. Then, a predetermined action might occur, depending upon
what you want. In the following example, the program loops around until the value of i is 5.
Then the program proceeds to the next statement.
For i = 1 to 5
print i
Next
As well as "For" structures, loops can also be implemented by "While" statements:
Do While i <= 100
i=i+1
print i
Loop
Programming Concepts- Operators
Looping and branching structures depend on logical tests to determine whether to continue the loop
or the branch to follow.
A logical test is one that resolves to a TRUE or FALSE value. You need to be familiar with basic
comparison operators:
■ == is equal to (returns TRUE if both conditions are the same).
■ != is not equal to.
■ < less than.
■ > greater than.
■ <= and >= less than or equal to and greater than or equal to.
You might also want to test more than one condition at the same time. The logical operators are as
follows:
■ AND—if both conditions are TRUE, then the whole statement is TRUE.
■ OR—if either condition is TRUE, then the whole statement is TRUE.
■ XOR—if either condition is TRUE but not both, then the whole statement is TRUE.
You can also use the negation operator NOT to reverse the truth value of any statement.
Programming Concepts- Procedures and
Functions
Both procedures and functions enable you to create segments of code that you will reuse.
In our earlier example, we used a function to check that the user had entered a number.
The key difference, in programming terms, between a procedure and a function is that the latter can return a value
to whatever called it, whereas a procedure cannot.
For example, the following is a procedure:
Verify subroutine {
verify user input is number
if FALSE, call Display Instructions subroutine
return
}
Whereas, the following is a function:
Verify function {
IsNumber = verify user input is number
return IsNumber
}
In the function, the value of IsNumber, which will either be TRUE or FALSE, is returned to the main program for
some further processing.
Programming Concepts- Comments
As noted above, it is important to use comments in code to assist with maintaining it.
A comment line is ignored by the compiler or interpreter.
A comment line is indicated by a special delimiter, such as double forward slash (//), hash
(#), or apostrophe (').
Object-Oriented Programming
Object-Oriented Programming
• A class serves as a prototype, or template, for individual
objects in the class.
• Classes contain properties that are the attributes or characteristics that
objects can hold as fields or private variables that other programs cannot
change or access directly. Properties can be used as another way to access a
field publicly.
• Classes also contain methods (also known as procedures, functions, routines,
or subroutines) that are the behaviors or operations that the object can
perform.
• For example, a method takes input, generates output, and manipulates data.
• Methods are used to make changes to attributes.
• Objects are the specific models built from the class
templates. These objects work with each other to make
the computer program function.
Object-Oriented Programming
Objects can have :
◦ attributes (fields)
◦ properties
◦ methods
Attributes
Attributes are values and data types that define the object.
The attributes are stored within the object as fields or private
variables.
Other programs cannot access or change the fields directly. They
must call a particular method (see below) to do that.
Methods
Methods define what you can do to an object.
For example, a Customer object might have a ChangeAddress method.
When another part of the program calls the ChangeAddress method, it also passes a string
(hopefully containing a well-formatted address) to the object because that is what the
method expects.
The object's code receives this string, checks that it can be used as a valid address, and
updates its Address field.
The object might then have another method (PrintAddress) allowing another part of the
program to find out what the current value of the Address field is.
Properties
Properties represent an alternative way of accessing a field
publicly.
Using a method might be regarded as quite a "heavyweight"
means of doing this, so properties allow external code to ask the
object to show or change the value of one of its fields.
Scripting Languages
We learned earlier that some of the interpreted
programming languages are scripting languages.
You can also make a distinction between the general
purpose interpreted scripted languages, such as
JavaScript or Perl and scripting languages, such as
Windows Command Prompt, Windows PowerShell, or
Linux Bash.
These languages support the automation and
configuration of a particular operating system.
Scripting Languages
A script is a smaller piece of code than a program.
A script is generally targeted at completing a specific task,
whether that task is based within a web-based application or is
used by a network administrator to perform a repetitive
administrative task.
While a program usually provides some sort of unique
functionality, anything a script does could usually be
performed manually by a user.
Writing scripts is a good place to learn the basics about
programming.
They are usually simpler to learn, require no compiling, and are
well documented on the Internet, should you require guidance
or samples.
Batch Files
Batch files are a collection of command-line
instructions that you store in a .CMD file.
You can run the file by calling its name from the
command line, or double-clicking the file in File
Explorer.
Generally, batch file scripts run from end to end and
are limited in terms of branching and user input.
Batch Files
VBScript
VBScript is a scripting language based on Microsoft’s
Visual Basic programming language.
It provides similar functionality to JavaScript. VBScript
is often used by network administrators to perform
repetitive administrative tasks.
With VBScript, you can run your scripts from either
the command line or from the Windows graphical
interface. Scripts that you write must be run within a
host environment.
Windows 10 provides Internet Explorer, IIS, and
Windows Script Host (WSH) for this purpose.
VBScript
Application Platforms and Deliver
Application Platforms and Delivery
Once you have written and tested your code, you
need to package it as an executable or script for
deployment to the hosts that will run the software.
There are many different ways of packaging and
deploying software applications.
Single-platform Software
Some software applications are written to run under
a particular operating system (and sometimes a
particular version or set of versions of an OS).
The software is written using a development
environment suitable for that OS.
The developer writes the application in program
code using a particular programming language.
When the application is compiled, it gets converted
to machine code that can run (or execute) in the
target OS environment.
Single-platform Software
This development model can be referred to as
single-platform software.
Because this model produces software that is
optimized for a particular platform, it can perform
better and be simpler to check for errors than
crossplatform software.
The drawback is that "porting" the software to a
different platform (from Windows OS to Android for
instance) can be very difficult.
Single-platform Software
Cross-platform Software
Software written for PCs and laptops ("desktop"
applications) is being supplemented with software written
for touch-based mobile devices and operating systems.
These are often referred to as apps. There is usually plenty
of interoperability between desktop and mobile platforms.
For example, you could create a document in Microsoft
Word on a PC and send it to someone to view on their
smartphone.
Cross-platform Software
Another trend is for applications to be accessed over the web, often
using a "cloud" deployment model.
When software is run from a web server as a web application, it can
usually support a wide range of different types of client operating
systems and devices. Usually the only client required is a compatible web
browser.
This sort of cross-platform development is complicated by the different
hardware interfaces for different computing devices.
The interfaces supported by keyboard and mouse input devices for PCs
and laptops do not always work well with the touchscreens used by
smartphones.
Cross-platform Software
The wide range of display sizes, orientations, and resolutions are another
factor making cross-platform design challenging.
There can be performance problems when software is written for a
"platform independent" environment, such as a Java virtual machine, rather
than code run natively by the OS.
Compatibility issues can also affect web applications as different browser
vendors can make slightly different interpretations of open standards that
result in applications not working correctly in particular browsers or browser
versions
Application Delivery Methods
A traditional PC-type software application is installed locally to the
computer's hard drive.
When launched it executes within the computer's memory and is processed
by the local CPU.
Any data files manipulated by the application can also be stored on the local
disk, though usually in a user folder rather than the application folder.
For security reasons ordinary users should not be able to modify application
folders.
A locally installed application such as this does not need network access to
run, though obviously the network has to be present if the application
makes use of network features.
Application Delivery Methods
For some enterprises, this locally installed model presents performance and
security issues.
Another option is for the application to be installed to a network server and
executed on that server.
Client workstations access the application using a remote terminal or
viewer.
The most successful example of this kind of application virtualization model
is Citrix XenApp.
Locating the application and its data files on a server is easier to secure and
easier to backup. This model also does not require that client hosts be able
to access the Internet.
The drawback is that if there is no local network connection or the local
network is heavily congested, users will not be able to use the application.
Integrated Development
Environments
IDE stands for integrated development environment,
which is a set of programming tools that work together to
make a programmer's job easier.
It's important to remember that an IDE can make it easier
to spot mistakes,
• Most IDEs include the following components:
Text or code editor
Project windows for convenience
Compiler
Debugger
Language-specific features
IDE Example
Application Programming Interface
The term API stands for application programming
interface.
Programmers use APIs to connect or communicate
with databases, operating systems, and other
services.
APIs define both protocols and tools for
programmers to build applications.
A web APIs is a specific type of API that governs the
access points of a web server that receives requests
and sends responses.
How Do APIs Work?
Who Uses APIs?
Ifyou want to build an app for a particular operating
system, such as Windows or macOS, that operating
system will already have a large number of APIs that
you can draw from in order to make things easier.
For example, let's say you're building an app and
want it to be able to take a photo using the phone's
camera. That isn't something you need to code from
scratch.
The operating system should already have an API for
this function which you can insert into your app and
save yourself a great deal of time and effort.
Questions?