You are on page 1of 18

Computer Programming

Lab Instructor: Junaid Rashid


Lab 2: Introduction to Programming
Recommended Books

 Deitel
& Deitel, “C++ How to Program”,
5th Edition (or any latest)
 Robert
Lafore, “Object-Oriented
Programming in C++”, 3rd/4th Edition
Agenda
 What is Programming
 Why we do Programming
 The Programming Process
 History of C++ language
 Compiler
 Interpreter
 Structure of C++ program
 Hello World Program
 Escape Sequences
Why we do Programming

 Programming helps you understand computers. The computer is only a tool. If


you learn how to write simple programs, you will gain more knowledge about
how a computer works.
The Programming Process

 Developing a program involves steps similar to any problem-solving task.


There are five main ingredients in the programming process: 

 Defining the problem


 Planning the solution
 Coding the program
 Testing the program
 Documenting the program
History of C++
 Developed in early 1980’s
 Developed by Bjarne Stroustrup
 Extension of C language
Basics of a Typical C++ Environment
Program is created in
Phases of C++ Programs: Editor Disk the editor and stored
on disk.

1. Edit Preprocessor Disk Preprocessor program


processes the code.
Compiler creates
2. Preprocess Compiler Disk object code and stores
it on disk.
Linker links the object
3. Compile Linker Disk code with the libraries,
creates a.out and
Primary stores it on disk

4. Link Loader
Memory

Loader puts program


5. Load Disk ..
in memory.
..
..

6. Execute Primary
Memory
CPU
  CPU takes each
instruction and
executes it, possibly
storing new data
..
.. values as the program 7
..
executes.
Compiler and Interpreter

Compiler: A compiler is a program that reads in as input a program and outputs


machine language code
Interpreter: An interpreter is a program that reads in as input a source program,
along with data for the program, and translates the source program instruction
by instruction
Difference b/w Compiler and Interpreter

Compiler Interpreter
 Interpreter Takes Single instruction as input .
 Compiler Takes Entire program as input  No Intermediate Object Code is Generated
 Intermediate Object Code is Generated  Conditional Control Statements are Executes
 Conditional Control Statements are Executes slower
faster  Memory Requirement is Less
 Memory Requirement : More  Every time higher level program is converted
 Program need not be compiled every time into lower level program
 Errors are displayed after entire program is  Errors are displayed for every instruction
checked interpreted (if any)
 Example : C Compiler
 Example : BASIC
Structure of C++ Program
Three main parts:
 Preprocessor Directives
 main() function
 C++ statements
Preprocessor Directives

The instructors that are given to the compiler before the beginning of the
actual program are called preprocessor directives or compiler directives
Preprocessor Directives (cont.)
 consists of instructions for the compiler
 includes header files
 starts with # sign
 keyword: include
Header Files
 C++ source file containing definitions of library functions
 Added in a program
 File name written in angle brackets < > or double quotes “ ”
 Example: #include<iostream>, #include<fstream>
main() Function
 Beginning of a C++ program
 Every C++ program has a main function
 main function is a unique function
 When program executes control transfers to main function
 Program is not compiled without a main function
 Syntax:
main()
{
Program statements
}
C++ Statements
 C++ statements are written under the main() function between curly braces
 C++ statements end with a semicolon ;
 C++ language is case sensitive
 C++ language written in lower case
Keywords
 include
 using namespace std
 main
 int
 cout
 endl
Hello World Program

#include <iostream>
using namespace std;
int main()
{
cout<<“Hello World”;
return 0;
}
Escape Sequence

 \a  Alert alarm
 \b  Backspace e.g
cout<< “Hello World\b”;
 Tab, e.g
 \t
cout<<“Hello \tWorld”;
 New line
 \n
 Comment e.g
 //
\\cout<<“Hello World”;
 Print double quotes e.g
 \” cout<<“ \”Hello World\” ”;
 Print Single quotes e.g
 \’ cout<<“ \’Hello World\’ ”;

You might also like