Lec-1-Introduction To The Course

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

National University of Modern Languages (NUML)

PROGRAMMING FUNDAMENTALS
LECTURE 1
Computer
• A machine which can perform
calculations and make logical decisions
billions of time faster as compared to
human .
• Todays’s fastest computer can perform
thousands of trillions instruction PER
SECOND !

Session 1 2
Applications of Programming
Robotics:
Applications of Programming
Simulation:
Applications of Programming
Scripting:
Programming Languages
Pick up the red pen and
place it on the table

Walk 500 meter on First Ave and


then turn right

Very precise instructions that will result in a single, expected outcome.


Programming Languages
• A programming language “tells” the computer
precisely what to do

• Instructions for a specific task are encoded in a


language

• A computer understands these instructions and


performs the task
Programming Languages
Walk 10 feet in a straight line

– Cammina per 10 piedi in linea


retta
– Camine 10 pies en línea recta
– Пройдите 10 футов по прямой
– Siúil 10 troigh i líne dhíreach
Programming Languages
• Three types of programming
languages:
– Machine
– Assembly
– High Level

• Assembly and High Level


languages are used more often

• Some examples of high level


languages: C++, C#, Java, Python
Where We Are in Computers?
•At the very basic level computers use the concept
of an electrical pulse.
- Low voltage is represented as 0
- High voltage is represented as 1
• To instruct a computer we need ask the computer
in the language of 0s and 1s commonly known as
machine language.
• For instance 73 in a number in natural language in
the language of 0s and 1s, it becomes 1001001

Session 1 5
Machine Language: Our First
Interaction with the Computer
• machine language.
10110011 00011001
01111010 11010001 10010100
10011111 00011001
01011100 11010001 10010000
10111011 11010001 10010110
– Finding an average of two numbers
• Not very intuitive way of working
• Not possible for humans to achieve a lot using machine
language

Session 1 6
One Step Beyond - Assembly Language
•One level above machine language is assembly
language
MOV 0, SUM
MOV NUM, AC
ADD SUM, AC
STO SUM, TOT

• More understandable but still very difficult for


many of us.
• An assembler translates assembly language into
machine language.

Session 1 7
Another Step - High-level
Languages
•High-level languages is another level above
machine language.
X = (Y + Z) / 2
• Much more understandable.
• A compiler translates high-level language

into assembly language.

Session 1 8
What is Programming?
• When we say “programming” we are
actually referring to the science of
transforming our intentions in a high-level
programming language.

Session 1 10
Back-end Programming Languages
Top Programming Languages
Top Languages wrt Jobs
Programming Languages
• Programming Language
– A set of rules, symbols, and special words used to
construct a computer program.
• Machine Language
– The binary representation of the instructions that a computer's hardware can
perform.
• Assembly Language
– A low-level programming language in which a mnemonic is used to represent
each of the machine language instructions for a specific computer.
• High-Level Language
– A computer language that is more understandable and closer to standard
notations than assembly language. It is more close to plain English. C/C++ are
high-level languages.
Programming language rules
• Rules of Syntax which specify how valid instructions are written in
the language.
– They deal with the structure of an instruction

• Rules of Semantics which determine the meaning of the


instructions (what the computer will do in response to the given
instructions).
– They deal with the content of an instruction
Bugs and Debugging
• Programming errors are called bugs
• The process of tracking bugs and correcting them is
called debugging.
• Three kinds of errors can occur in a program:
– syntax errors
– semantic errors
– runtime errors

• It is useful to distinguish between them in order to track them


down more quickly
Syntax errors
• Most languages including c++ can only
execute a program if the program is
syntactically correct; otherwise, the process
fails and returns an error message.
– Syntax refers to the structure of a program and
the rules about that structure.
– For example, in English, a sentence must begin
with a capital letter and end with a period.
this sentence contains a syntax error.
So does this one
Semantic Errors
• Semantic error is an error in the content of a code.
• If there is a semantic error in your program, it will run
successfully, i.e. the computer will not generate any
error messages, but it will not do the right thing.
• The problem is that the program you wrote is not the
program you wanted to write. The meaning of the
program (its semantics) is wrong.
• Identifying semantic errors can be tricky because it
requires you to work backward by looking at the
output of the program and trying to figure out what it
is doing.
Runtime Errors
• The third type of error is a runtime error, so
called because the error does not appear until
you run the program.
• These errors are also called
exceptions because they usually indicate that
something exceptional (and bad) has
happened.
• Runtime errors are rare in the simple
programs so it might be a while before you
encounter one.
Types of Program
• Source Program
– A program written in a human readable version,
which you will write.
• Object Program
– The machine language version of a source
program in 0s and 1s.
• EXE Program
– It is an executable program
Basics of a Typical C++ Environment
Program is created in
Editor Disk
Phases of C++ Programs: the editor and stored
on disk.

1. Edit Preprocessor Disk Preprocessor program


processes the code.
2. Preprocess Compiler creates
Compiler Disk object code and stores
3. Compile it on disk.
Linker links the object
4. Link Linker Disk code with the libraries,
Creates an executable
5. Load Primary
Memory
file and stores it on disk
Loader
6. Execute
Loader puts program
in memory.
Disk ..
..
..

Primary
Memory
CPU
CPU takes each
instruction and
executes it, possibly
storing new data
..
.. values as the program
..
executes.
• Functions • Directives
– Function name – Preprocessor
– Braces and the Directives #
function body – Header Files
– Main function – The ‘using’ Directive
• Program Statements – comments
• Output using cout
A basic program
// ------------------------------------------------------------
/ *hello.cpp is a demonstration program
Welcome to C++*/ Comments
Preprocessor Directives/
Header File
#include <iostream>
using namespace std;
The using Directive

void main ( )
{
cout << "Hello C++ World ! \n";
}
A basic program
// ------------------------------------------------------------
/ *hello.cpp is a demonstration program
Welcome to C++*/
#include <iostream>
using namespace std; The main() function

void main ( )
{ Escape Sequence
cout << "Hello C++ World ! \n";
End of a statement
}
String Constant

Insertion Operator

Standard output stream


A basic program
• Programs typically contain the following
elements:
– Descriptive comments (double forward slash // or /* … */)
– Include files
– Functions including exactly one main function

• The main function


– Controls the execution of the program
Output Statements
cout << “Hello C++ World”;
•cout is an object in c++, predefined to display
the standard output stream
•<< insertion operator
– It directs the contents of the variable on its right
to the object on its left
•The output of this program
– Hello C++ World
Output
Escape Sequences

Escape Sequence Character


\a Bell (beep)
\b Backspace
\n Newline
\r Return
\t Tab
\\ Backslash
\‘ Single quotation mark
\“ Double quotation marks
Acknowledgements
1. Deitel and Deitel: C++ How to Program, 7th Edition, Prentice Hall Publications
Material in these slides has been taken from, the following resources

2. Robert Lafore: Object-Oriented Programming in C++, Fourth Edition,


December 2001,Sams Publishing .
3. A Structured Programing Approach Using C++ by Behrouz A. Forouzan
4. www.cplusplus.com

33

You might also like