Intro To Computer Programming by SHAYAN AHMAD KHATTAK 4th Semester BS PHYSICS UOP
Intro To Computer Programming by SHAYAN AHMAD KHATTAK 4th Semester BS PHYSICS UOP
PROGRAMMING
BY
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.
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 .
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 ! && ||
or
• 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.
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.
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.
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.
class object
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++ postincrement
++a preincrement
a postdecrement
a predecrement
Simple arithmetic program:
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”
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.
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);