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

Introduction to

Programming
Course Learning Outcomes
1. Use and understand fundamentals of programming such as
variables, data types and operators.-------------------------------------------
--PLO-2
2. Understand, debug & use conditional , selection and iterative
statements according to the specific problems------------------------------
----------------PLO-3
3. Familiarize students with the basics of functions, function calls, built
in functions ----------------PLO-3
4. Have the ability to write error free computer programs using a C++
complier------------------PLO-5
Marks Distribution

▪ Quiz 10% 3- 4 quizzes


▪ Assignments 10% 2- 3 assignments
▪ OHTs 30%
▪ Final 50%
Tentative Time Line
▪ Exams
▪ 1st OHT 12-17 Nov 2018
▪ 2nd OHT 24-29 Dec 2018
▪ Final 28 Jan – 2 Feb 2019
▪ QUIZZES
▪ 3 / 4 Quizzes
▪ Assignment
▪ 2 to 3 assignments
BOOKS
▪ Introduction to Programming with C++ by Y. Daniel Liang
▪ Jumping into C++ by Allex Allain
▪ Object Oriented Programming By Robert Lafore
▪ How to Program C++ by Dietel & Dietel
6

Computer Languages
▪ In order to communicate with the computer we use one of
several programming languages.

▪ In the beginning, there were…

▪ First generation ‑ Machine Language


▪ Second generation ‑ Assembly Languages
▪ Third generation ‑ Compiler Languages
7

First generation ‑ Machine


Language
▪ Each type of computer has its own machine language, the only language
it can understand (machine dependent).
▪ Most machine languages consist of binary codes for both data and
instructions, e.g., to add overtime pay to base pay we would need a
series of binary codes such as, perhaps:

0010 0000 0000 0100


0100 0000 0000 0101
0011 0000 0000 0110
Second generation ‑ Assembly 8

Languages
▪ Uses English‑like abbreviations to represent the
machine‑language instructions.
▪ Uses a translator program called an assembler to convert each
instruction from the assembly language instruction to the
corresponding machine language binary code e.g., perhaps:

▪ LOAD BASEPAY
▪ ADD OVERPAY
▪ STORE GROSSPAY
Third generation ‑ Compiler 9

Languages
▪ High‑level, machine independent, closer to English‑language , more
natural. Each high-level language statement translates to several low-
level language statements, e.g.:
GROSSPAY = BASEPAY + OVERPAY
▪ Use compilers to translate from the high‑level language into machine
language.
▪ A compiler is a translator program that transforms high-level program
code into a low-level machine-level executable program.
▪ Compilers translate the whole program first, then execute the object
program.
▪ High-level languages are more English-like, easier to code, more costly to
run, less flexible. e.g., FORTRAN, BASIC, COBOL, PL/1, ALGOL, APL,
Pascal, SIMSCRIPT, Smalltalk, C, C++, Java, Python.
High‑level Language Translators 10

compiler
source object
or
program program
interpreter

Compilers Interpreters

Slower translation Faster translation


Entire program One instruction or macro at a time
Faster execution Slower execution
More efficient execution Less efficient execution
Good for commercial Good for program development
applications
11

Program
▪ A program is a set of instructions in proper
sequence, that causes a computer to perform a
particular task.
▪ Modern programs are projects composed of many
of individual program modules that have to be
linked together in order to be run.
▪ Software == one or more programs
Writing C++ program
▪ A programmer uses a text editor to create or modify
files containing C++ code.
▪ Code is also known as source code.
▪ A file containing source code is called a source file.
▪ After a C++ source file has been created, the
programmer must invoke the C compiler before the
program can be executed (run).
Sample. Compiler converts
cpp Sample.cpp is your C++ code human readable
#include<iostream. language to a
typed into a text file using a
h> language which is
text editor.. It is a human
void main() understandable by
readable the operating system/
{ hardware
cout<<“hello”;
Examples of C/C++
} compilers of today:
C++ Converts sample.
cpp into sample.
Visual C++
GCC/G++
Compiler exe DJGPP (open source
for windows like GCC)
Borland C
Turbo
The compiler takes Sample.cpp
10110111000100100
as an input and converts into 011010010010011
machine readable executable. 100001111001010
Computer runs or executes this 100010011110001
executable 001000000001000
Sample.
exe
Stage 1: Preprocessing

▪ Performed by a software program called the preprocessor



Stages of Compilation
Modifies the source code (in RAM) according to preprocessor directives (preprocessor
commands) embedded in the source code
▪ Ignores comments and white space from the code.
▪ The source code as stored on disk is not modified.
Stages of Compilation
Stage 2: Compilation

▪ Performed by a program called the compiler


▪ Translates the preprocessor-modified source code into object code (machine code)
▪ Checks for syntax errors and warnings
▪ Saves the object code to a disk file.
▪ If any compiler errors are received, no object code file will be generated.
▪ An object code file will be generated if only warnings, not errors, are received.
Stage 3: Linking

▪ Combines the program object code with other object code to produce the
3 Stages of Compilation
executable file.
▪ The other object code can come from the Run-Time Library, other libraries, or
object files that you have created.
▪ Saves the executable code to a disk file
▪ If any linker errors are received, no executable file will be generated.
Editor

Program Development
Source File gm.cpp

Preprocessor
Modified Source Code in RAM

Compiler
Program Object Code File gm.obj

Linker

Executable File gm.exe


Anatomy of a C++ Program
● header comments

● preprocessor directives (if any)

● void main ( )
● {
● statement(s)

● }
A Simple C++ Program
● #include <iostream.h> //This is a preprocessor directive
● void main ( ) //this tells the starting point of your program
● {
● cout << “Hello World” <<endl ; //print the text on monitor
● }
Comments
▪ A comment is descriptive text used to help the program reader understand its
content.
▪ Single line- Any code description in a single line is expressed using single line
comments. //
▪ Multi line- All comments must begin with the characters /* and end with the
characters */
▪ The program header comment always comes first.
Preprocessor Directives
▪ Lines that begin with a # are called preprocessor
directives
▪ Example: the #include <iostream.h> directive causes the
preprocessor to include a copy of the input/output
stream header file iostream.h at this point in the code.
▪ This header file includes information about the cout
function that is used in this program.
iostream.h
▪ When we write our programs, there are libraries of functions to
help us so that we do not have to write the same code over and
over.
▪ Some of the functions are very complex and long. Not having to
write them ourselves make it easier and faster to write programs.
▪ Using the functions will also make it easier to learn to program!
void main ( )
▪ Every program must have a function called
main. This is where program execution
begins.
▪ main() is placed in the source code file as the
first function for readability.
▪ The reserved word “void” indicates that main(
) function returns nothing.
▪ The parentheses following the reserved word
“main” indicate that it is a function.
cout << “Hello, World!” <<endl ;
▪ This line is a C++ statement.
▪ Notice that this line ends with a semicolon. All statements in
C++ end with a semicolon.

You might also like