0% found this document useful (0 votes)
181 views

Intro To Computer Programming by SHAYAN AHMAD KHATTAK 4th Semester BS PHYSICS UOP

The document provides an introduction to computer programming, explaining the basic components of a computer system including the input, output, CPU, memory, and different types of computer languages from machine to high level languages. It also covers basic programming concepts like variables, data types, and how to write a simple program in C++ to calculate the mean of three numbers by prompting the user for input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views

Intro To Computer Programming by SHAYAN AHMAD KHATTAK 4th Semester BS PHYSICS UOP

The document provides an introduction to computer programming, explaining the basic components of a computer system including the input, output, CPU, memory, and different types of computer languages from machine to high level languages. It also covers basic programming concepts like variables, data types, and how to write a simple program in C++ to calculate the mean of three numbers by prompting the user for input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

INTRODUCTION TO COMPUTER

PROGRAMMING

BY

“SHAYAN AHMAD KHATTAK”


components of computer system:
the computer system consist of following components
• input unit
• output unit
• CPU (central processing unit)
• ALU (arithmetic logical unit)
• main memory
• secondary memory

1)INPUT UNIT:
Input unit receives DATA/PROGRAM from the input devices for execution/processing.
e.g~ mouse , keyboard , scanner , CD-ROM , DVD-drive etc.

2)OUTPUT UNIT:
output unit takes information from computer & send it to the output devices.
e.g~ speakers , monitor , printer etc .

IN REAL WORLD WE INTERACT/COMMUNICATE WITH COMPUTER WITH THE HELP OF INPUT & OUTPUT
DEVICES.
3)CPU( CENTRAL PROCESSING UNIT ):

CPU also known as “micro-processor” is called the brain of computer . It is the most important component of the
computer which runs programs on computer ,without CPU no software could run on computer.

4)ALU(ARITHMETIC LOGICAL UNIT):


ALU is the part of CPU which performs all the numerical & logical operation like addition, subtraction , multiplication,
division , comparison (>,< )etc.

5)MAIN MEMORY or RANDOM ACCESS MEMORY(RAM):


Main memory or random access memory is directly connected with the CPU .All the programs are loaded in the main
memory before they are executed .When computer is turned off , everything in the main memory is lost that is why it is
also called “VOLATILE MEMOMRY”.
Main memory is an ordered sequence of cell called “memory cells”. Each cell has a unique location in main memory
called “address of the cell” .
Main memory stores everything as sequence of 0s and 1s.

6)SECONDARY MEMORY:
It is the type of memory used to store programs ,DATA ,Softwares etc & the data or information is not lost if the
computer is turned off ,that’s why it is also called “long term memory”.
e.g ~ flash drives ,hard disks etc.
PRIMARY MEMORY SECONDARY MEMORY
High cost Low cost
Low capacity High capacity
volatile Non-volatile , permanent
Rapid access Slower access

COMPUTER LANGUAGES:
In order to communicate with computer we use one of several languages , in the beginning there were~
• MACHINE LANGUAGE (1ST generation)
• ASSEMBLY LANGUAGE (2nd generation)
• COMPILER LANGUAGE(3rd generation)

1)MACHINE LANGUAGE:
Machine language is low level programming language , It uses the sequence of 0s & 1S.
Machine language is a tough and technical language.
For example:~ If we want to write 120 in the computer system its representation was 1111000.

2)ASSEMBLY LANGUAGE:
Assembly language is something more easy & handy than low level language & less than high
level language , often called intermediate language. Assembly language use numbers, symbols & abbreviations
instead of 0s & 1s.
For example:~ for addition , multiplication & subtraction we uses symbols like add, sub ,mul etc.
3) COMPILER LANGUAGE (HIGH LEVEL LANGUAGE ):

Compiler or high level language is a Human friendly language, more like English language ,seems
more natural & easy as compare to machine & assembly language .
High level language uses compliers to translate High level language program codes into low level (machine) language
executable program .
Compiler translate the whole program first than execute the object program. Compiler
e.g~ C , C++ , PYTHON , JAVA , SMALL TALK , FORTRAN, PASCAL etc. High level language
into
machine language
PROGRAMMING:
A “program” is a set of instructions in a proper sequence that causes the computer to perform any
specific task .

• Before writing a program in any programming language its is better to first know the rules of the game.

• Modern programs are projects composed of many individual program modules that must be placed together in
proper sequence in order to be Run. Most developer system have their own “INTEGRATED DEVELOPMENT
ENVIRONMENT (IDE)” and programs are developed in phases(step by step) with in the IDE .

“writing a program in C++ programming environment”


1. (#include <iostream>)~ This statement tells the compiler to include iostream file . This file contain pre defined
input/output functions that we can use in our program.

2. (using namespace std;)~ A namespace is like a region , where we have functions, variables etc , This tell the
compiler to look into that particular region for all the variables ,functions etc.

3. ( int main() )~ As the name suggest this is the main function of our program & the execution of program begins with
this function , the int here is the return type which indicates to the compiler that this function will return an integer
value , that is the main reason we have a return 0 statement at the end of the main function.

4. (cout<<“ ”<<;)~ The “cout” object belongs to the iostream file & the purpose of this object is to display the content
between double quotes on the screen in the RUN program.

5. (return 0; )~ This statement return value 0 from the main() function, which indicates that the execution of the main
function is successful . The value 1 represents failed execution.

6. (Comments // )~ Two slash indicates that the rest of the line is comment , comments have no impact or effect on
the behavior of the program , comments are used by the programmer for short explanations about code or
program so it is easier for the other person to understand the logic of the programmer.
The statement begins with #
Is called pre-processor
directives
i.e #include <iostream>
Every programming language has facilities to: In C++
Read data from some input device cin >>
Write output information onto an output device cout <<
Perform arithmetic operations + - * /
Perform relational operations < == >
Perform logical operations ! && ||

Branch to a non-sequential instruction (w/wo structure) while

Store (and retrieve) data values to (and from) memory =


program for mean of 3 numbers:
VARIABLES:
Variables(object) must be declared as a certain type, e.g., int, float, char, … This declaration may appear
anywhere in the program, as long as it appears before the variable is first used. The declaration creates
the object.

For the following declaration,


int n;
The name of the object is n, the type [or class ] is ‘int’. The object does not yet have a value.

n = 66; //now it has a value

or

int n=66; //This declaration also gives a value to n


A Variable (object) has:

• A name

• A location in main memory. This is an address in primary memory where the value of the object is stored
while the program is executing.

• A type (class). A class defines the way the data item looks (e.g., char or int), how much space it takes up
in memory, and how it operates.

• A value. It may have a value stored in the named location. There are 3 ways to give a variable a value:

• int n=66;
• n=66;
• cin >> n;
IDENTIFIERS (names):

A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor punctuation
marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition,
variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ), but in some
cases these may be reserved for compiler specific keywords.

Some reserved key words~ int, float, void, else, if, bool, . Etc
Syntax errors – compile-time errors Logic errors – run-time errors
These errors are picked up by the These errors are generally not
compiler and we will usually get flagged by the system. We find
error messages about them. out about them by checking the
Syntax errors result from using the output to see whether it is correct
language incorrectly.

Ans is not correct


Which indicates
logic error

Zero error which


Indicates no syntax error
Declaring float along with
floating digit Now this seems good with no
error
What if we want to be able to calculate the mean of ANY three numbers? We need to input data to the
program

You can write ‘float’ instead of ‘int’ in


program but use floating values i.e
x=(a+b+c)/3.0 then
The main aim is to entering the value of
variable in different way i.e individually
DATA TYPE:
All variables use data-type during declaration to restrict the type of data to be stored.
Therefore, we can say that data types are used to tell the variables the type of data it can store.
Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on
the data-type with which it is declared. Every data type requires a different amount of memory.

TYPES
1)Primitive OR Pre-defined Data Types:
These data types are built-in or predefined data types and can be
used directly by the user to declare variables. example: int, char , float, bool etc.

2) Derived Data Types:


The data-types that are derived from the primitive or built-in datatypes are referred
to as Derived Data Types. These can be of four types namely:
• Function
• Array
• Pointer
• Reference

3)User Defined Data Types:


These data types are defined by user itself. Like, defining a class in C++ or a
structure. C++ provides the following user-defined datatypes:
• Class
• Structure
• Union
• Enumeration
• Typedef defined DataType
IDENTIFIERS:
• Identifiers are words that a programmer uses to name things in a program.
• An Identifier can be made up of letters , digit or underscore character .
• An identifier can not begin with a digit.
• C++ is case sensitive , therefore NUM & num are different identifiers.
• Key words can not be identifiers.

DECLARATION:
Inform the compiler that it will need to set aside space in memory to hold an object of a
particular type (class) with a particular name.

1) Constant declaration: used to associate meaning full names with constant ~items that will never change
throughout the execution of program.

const float PI=12.34; .

2) Variable declaration: Used to associate identifiers of given type with memory cells used to store value of
this type. ~ the stored values in the data cells can be changeable.

Char letter;
float x,y;
3)Object declaration: variables can store data values and are called objects . Like variables these are used
to associate identifiers of given type with memory cells used to store values of this type~ the stored values in
data cells are changeable.

Ofstream cprn (“printfile.txt”)

class object

Data Type “int” & “char”:

The variable’s type (or class) tells the compiler how the variable’s values are to be stored and how they may
be used.

Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and
ranges from -2147483648 to 2147483647.

Character: Character data type is used for storing characters. Keyword used for character data type is char.
Characters typically requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255.
Character example:
Arithmetic operation:
Some built-in arithmetic operation are
• Addition +
• Subtraction 
• Multiplication *
• Division / (is integer division if operators are integers)
• Modulus % (remainder)

Increment/decrement operators:

a++; is the same as a = a + 1; and also the same as ++a;


a; is the same as a = a  1; and also the same as a;

a++ postincrement
++a preincrement
a postdecrement
a predecrement
Simple arithmetic program:

Used all arithmetic operators for


a&b
For operation you must set a value
‘c’ for which all operation are
performed using “a”&”b”
Also assign values to “a”& “b”
You can also give values in RUN program individually , for this you have to include “cin>>” statement and
not give values while writing a program .

An arithmetic operation of multiplication for “x” & “Y”


1 2 3

5
4

1
Use “float” instead of “int” for the answers in decimals

3
4

5
GPA calculator university of peshawar
IF ELSE PROGRAMS: if/else programs are those programs in which a programmer defines the condition that
if a result is likely to occur under certain condition, if not than the “else” statement is likely to occur.
NESTED IF (PROGRAM):
SWITCH STATEMENT: The switch statement execute one statement from multiple
conditions. It is like if-else-if ladder statement in C++.
Break statement: The break statement is used to break switch statement. It breaks
the flow of program at the given condition.
ARRAY: “An array is a set of consecutive memory locations used to store data”

• Each item in an array is called its element.


• The number of elements in an array is called its dimension.

Types: There are two types of arrays.


1) One or single dimension Array.
2) Two or multi dimension Array.

1): one/single dimension Array: one dimensional array is the list of variables having same data type .

e.g
2) Two/Multi dimensional Array: Two dimensional Array is “ Array of Arrays” having same Data type.

e.g
STRINGS: “ strings are objects that represent sequence of characters”.

With first character , a second character & so on . In C++ STRINGS are enclosed by single or double quotes.
e.g char Greeting[6]= {‘H’,‘E’,‘L’,‘L’,‘O’,‘\0’}

The string is actually a one dimensional array of a characters , which is terminated by a null character, ‘\0’.
LOOP:
Loop is used to execute a block of statements repeatedly until a particular condition is satisfied .

TYPES OF LOOP:
1. For loop
2. While loop
3. Do while loop

1) For loop: For loop is used to iterate a part of program several times. if the number of iteration is fixed, it is
recommended to use for loop instead of while or do-while loop.

for(initialization, condition, increment/decrement)


2) While loop: while loop is used to iterate a part of program several times . If the number of iteration is not
fixed. It is recommended to used ‘while loop” instead of “for loop” .

While(condition) { code }
DO-WHILE LOOP: “do-while” loop is used to iterate part of the program several times. if thenumber of
iteration is not fixed & you must have to execute the loop at-least once because the condition is checked
after loop body .

do{code}while(condition);

You might also like