You are on page 1of 24

CHAPTER 1: Programming

Fundamentals
CSEB113 PRINCIPLES of
PROGRAMMING
by
Badariah Solemon
BS (May 2013)

Topics
1. Computer Programs and Programming

Introduction
Why Do Engineering Undergraduates Need to
Take Programming Course?

2. Programming Languages

Machine Language ; Assembly Language; Highlevel Language

3. The C Language

Your first C Program


C Programming Environment

BS (May 2013)

Topic 1
COMPUTER PROGRAMS AND
PROGRAMMING
BS (May 2013)

Elements of a Computer
3
1

2
Applicatio
n
software

1
system
softwar
e

Hardware

Software

BS (May 2013)

Computer and Software


Computers can do a lot of exciting tasks.
However, computers must be given instructions in the
form of computer programs before they can perform
actions and make decisions.

Software/program a set of instructions stored


inside a computer that allows the user to do a
particular thing.
Operating
Systems

Utility
Programs

Windows 8

Language
Translation
Compiler

Disk Cleanup

System Software
*Complete this list

BS (May 2013)

Microsoft Word

scanf( "%s", astring );


for ( i = 0; i < 10; ++i )
{
if ( astring[i] == 'a' )
{
printf( "You entered
an a!\n" );
}
}

Application
Software

Embedded Systems
Computer programs that are embedded
into hardware to control its operations.
Example:
Telecommunication systems
Consumer electronics
Missiles and satellite
Smart cards
Medical equipment (e.g., electronic stetescope) and
medical imaging (e.g. MRI, CT)
Transportation systems
sscanf( "%s", astring );

sscanf( "%s", astring );


for ( i = 0; i < 10; ++i )
{
if ( astring[i] == 'a' )
{
printf( "You entered an
a!\n" );
}

BS (May 2013)

What is Programming?
Computer programming is:
not only about writing computer programs
the iterative process of designing, writing,
testing, debugging, and maintaining the
source code of computer programs
This source code is written in one or more
programming languages (such as C, C++, C#,
Java, Python, Smalltalk, etc.)

often shortened to programming,


scripting, or coding

BS (May 2013)

Why Should I Know


Programming?

Everybody in this country should learn


how to program a computer because
it teaches you how to think Steve Jobs

Watch Me

Source: http://www.youtube.com/watch?v=sGyH9tBie4M

BS (May 2013)

Why Take Programming


Course?
Programming is not only for computer science people
only.
Writing computer programs to solve complicated
engineering problems and to control mechanical
devices is a basic skill all engineers must master.
Introductory programming course isn't trying to teach
you to program so that you can be a programmer.
Instead, such course just want you to think like
programmers because in the future you will run into
problems that are too long and complex to do by hand.
In those cases, you'll have to use MATLAB or Mapple or mathworks or
some other math related application that allows you to solve these
problems.
Unfortunately, if you can't code your problem into the application, it won't
do you any good.

BS (May 2013)

Topic 2
PROGRAMMING LANGUAGES
BS (May 2013)

10

What is a Programming
Language?1

Technically, it is an artificial language designed


to communicate instructions to, or to control
the behavior of a machine, particularly a
computer.
It is different from our everyday-language
(natural language -NL)
NL : Depends on circumstances; the context one
word can have many meaning depending on the
situation (e.g., OPERATE)

PL: Very specific (one word means one thing


context free) since to 'talk' to a computer; to instruct
a computer; our commands must be 100% clear and
correct.
BS (May 2013)

11

What is a Programming
Language?2

Conceptually, programming language is a


framework within which we organize our
ideas about data and procedures.
Data: 'objects' or things we want to manipulate
Procedures: 'descriptions' or 'rules' or
processes that define how to manipulate
data
Example:
A simple program that determine sum of two
numbers
Data ?
Procedures?

BS (May 2013)

12

Types of Programming
Languages
printf (Hello);
total = quiz + assignment;

High-Level
Language

printf(Total = %d, total);

Assembly
Language

LOAD
LOAD A,
A, 9999
9999
LOAD
LOAD B,
B, 8282
8282
SUB
SUB BB
MOV
MOV C,
C, AA

Machine
Language

1101 0001 1000 0101 1000 0110


0100 1100 0101 1110 0101 1010

BS (May 2013)

13

1. Machine Language
The only language that is directly understandable by a
computers processor.
Certainly difficult for humans to understand

Consists of binary codes: 0 and 1.


Example:
1101
0100

0001
1100

1000
0101

0101
1110

1000
0101

0110
1010

1111
0001

0001
1100

is the language into which all programs must be


converted before they can be run
is generally machine dependent, as it varies from
processor to processor (e.g., Intel, AMD and ARM
processors)
is NOT portable
program may sometimes have to be completely rewritten to work
on different type of processors

Programming in machine language is very slow and


tedious since it is hard to memorize all the instructions
and mistakes can happen very easily
BS (May 2013)

14

2. Assembly Language
Uses a mnemonic code to represent each machine
LOAD A, 9999
operation
in words and numbers

LOAD B, 8282
SUB B
MOV C, A

An intermediary language that can be understood by


00000000
LOAD A, 9999
00010101
LOAD B, 8282
humans
00010110
Assembler
SUB B
but is still quite difficult to use

MOV C, A

00110101
01110111

Cannot be processed directly by a computer, must be


converted to machine language using assemblers
Although easier to use than machine language,
programmer is required to have a complete understanding
of the computer hardware in order to program it.
To do simple tasks such as printing out a message,
programmer needs to write a substantial amount of code.
Processor dependent and not portable.
BS (May 2013)

15

3. High-level Languages1
Use more English words so easier to program in these
languages. Example:
printf (Hello);
total = quiz + test + assignment;

The programming structure is problem oriented


does not need to know how the computer actually executes the
instructions.

Processor independent - the same code can be run on


different processors.
Programs written in these languages must be translated
into executable machine language using
Compilers translate instructions to create executable form
Interpreters translate and execute instructions one after
another
BS (May 2013)

16

3. High-level Languages2
Example:
Java

PHP
Visual Basic

Objective-C
C++
C#

Python
Perl
Javascript

Ruby
Visual
Basic.NET
LISP
Pascal
Delphi

Ada
MATLAB
Lua
Bash
FORTRAN

All have set of rules called syntax


must be followed
describe the form of a valid program and to get an
accurate translation into machine language

BS (May 2013)

17

Topic 3
THE C LANGUAGE
BS (May 2013)

18

The C Language1
It was developed by Dennis Ritchie in the early
1970s at Bell Laboratories (now a part of
Lucent Technologies, Inc).
A high-level language that is highly portable.
C combines the power of high-level languages
with the power of assembly languages.
It has influenced the newly created high-level
language name Objective-C
which is the main programming language used by
Apple for the OS X and iOS operating systems and
their respective APIs, Cocoa and Cocoa Touch
frameworks.
BS (May 2013)

19

The C Language2
C is a highly imperative programming
language. This means:
It expresses what the program should
accomplish by prescribing how to do it in terms
of sequences of actions to be taken.
It was designed to be compiled using a
relatively straightforward compiler.

The language has become available on a


very wide range of platforms, from
embedded microcontrollers to
supercomputers.

BS (May 2013)

20

Your First C Program


A C program (also called C source code):
/**********************************************************
Author: Badariah Solemon
Date: 01/01/2013
Description: A program that prints the famous
Hello World message on the computer screen
**********************************************************/
#include <stdio.h>
void main (void)
{
printf(****************\n);
printf( Hello World\n);
printf(****************\n);
}

It must be translated into machine


language. Once the machine language is
executed, the program
produces an
****************
Hello World
****************
output:
BS (May 2013)

21

C Programming
Environment
A modern C programming often made easy by the

use of integrated development environments


(IDEs).
IDEs that support C language include Microsoft
Visual Studio, Eclipse, and etc.
Ms Visual Studio 2010 Express or Ms Visual Studio
Express 2012 is free!
Highly recommended for your personal use
You must register to obtain a free product key for
ongoing use

BS (May 2013)

22

Visual Studio C++ Express

BS (May 2013)

23

Summary
Computers must be given instructions in the form of
computer programs before they can perform actions and
make decisions.
Programming is the iterative process of designing, writing,
testing, debugging, and maintaining the source code of
computer programs.
Conceptually, programming language is a framework within
which we organize our ideas about data (object we want to
manipulate) and procedures (how to manipulate the data).
In general, programming languages may be categorized as
1) machine language (also called machine code), 2)
assembly language, and 3) high-level languages.
C is a highly imperative programming language.
A modern C programming often made easy by the use of
integrated development environments (IDEs).
BS (May 2013)

24

You might also like