Programming presentation
-
Upload
fiaz-khokhar -
Category
Software
-
view
9 -
download
1
Transcript of Programming presentation
A program written in a high-level language is called source code.
Source code is also called Source program.Source code of high-level language is save
with a specific extension.For example the program written in c++ save
with .ccp extension.
Source Code
#include<iostream> //preprocessor directivesUsing namespace std; //std stand for symbols
int main() //main function{
Cout<<“MY First c++ Program”;System(“pause”);
}
Example of source code is:
Compiler is a program that converts the instructions of a high level language into machine language as a whole.
High-level language[Source code]:Machine Language[ Object Code]:
Use the compiler to:Check the program that obey rules.Translate the program into machine
language.
Compiler
Source code Compiler Object code
Compiler can translate the programs of only those language for which it is written.
For example c++ compiler can translate only those program that are written in c++ language.
Note:
The process of linking the library files with object program is known as linking. These files accomplish different task such as input output.
A program that combines the object program with additional library files is known as linker.
This additional library files is used to create the executable file whose extension is .exe .
Linker
After compiling and linking the program file the new file is created that is known as executable file.
The process of running an executable file is known as executing.
The program must be loaded into main memory for execution.
A program that load the executable file into main memory is known as loader.
Executable File
A collection of rules for writing program in a programming language is known as syntax.
Basic structure of c++ program:#include<iostream>
using namespace std;int main()
{Cout<<“Program body”;System(“pause”);
}
C++ Syntax
Define a set of values and a set of operation on those values.
Category of c++ data types:1. Simple data type2. Structure data type3. Pointer data type Simple data type: A data type is called simple if variable of that type
can store only one value at a time. e.g. int a float b; Simple data type is building block of structure data
type.
Data types:
Integral: integers (numbers without a
decimal) e.g. 12,34 etcFloating-point:
decimal numbers e.g.12.34 ,45.5Enumeration type:
user-defined data typeSyntax of declare a variable and data
type:Datatype variable_name;
Category of simple data type
int data type:Integers in C++, as in mathematics, are
numbers such as the following:-6728, -67, 0, 78, 36782, +763Note the following two rules from these examples:1. Positive integers do not need a + sign in front
of them.2. No commas are used within an integer. Recall
that in C++, commas are used to separate items in a list. So 36,782 would be interpreted as two integers: 36 and 782
Three most commonly used data type in integer
Bool DATA TYPE:The data type bool has only two values : true
and false.The central purpose of this data type is to
manipulate logical (Boolean) expressions.Char DATA TYPE:The data type char is the smallest integral data
type. It is mainly used to represent characters—that
is, letters, digits, and special symbols.e.g. 'A','a','0','*','+','$','&',''
Contin.
To deal with decimal numbers, C++ provides the floating-point data type.
It is also called real type data. It include positive and negative values.
Types of Floating Point:1. Float2. Double 3. Long double
Floating-Point Data Types
Data type Size in Bytes Description
Float 4 3.4 x10^-38 to 3.4 x10^+38
Double 8 1.7x10^-308 to1.7x10^+308
Long double 10 1.7x10^-4932 to1.7x10^+4932
Contin.
Structure data typeEach data item is a collection of other data
items.E.g. array , structure , classes.Pointer data type:The values belonging to pointer data types
are the memory addresses of your computer.pointer variables store memory addresses.Syntax for pointer declaration:dataType*variable;
Contin.
Operator are the symbols that are used to perform certain operations on data.
C++ provides a variety of operators.1. Arithmetic Operators2. Relations Operations3. Logical Operations4. Assignments Operators5. Increment and decrement Operators6. Compound Assignment Operators
Operators
Arithmetic operator is a symbol that performs mathematical operation on data.
C++ provides many arithmetic operators.
Arithmetic Operator
Operators Symbol Descriptions
Addition + Adds two values
Subtraction - Subtracts one from another value
Multiplication * Multiplies two values
Division / Divides one by another value
Modulus % Give the remainder of division of tow integers
Example if A=10 and B=5 then arithmetic operator and results
Contin. operators
Operation Result
A+B 15
A-B 5
A*B 50
A/b 2
A%B 0
The relational operators are used to specify conditions in program.
A relational operators compare two values.Also called comparison operators.C++ provides the following six relational
operators with their description: Greater than[>]:Greater than operator returns true if the value
on left side of > greater than the value on the right side. Otherwise returns false.
Relation Operators
Less than [<]:Less than operator returns true if the value on left side of < less than the value on the right side. Otherwise returns false.
Equal to ==:Equal to operator returns true if the value on both side of = equal. Otherwise returns false.
Greater than or equal to [>=]:Greater than or equal to operator returns true if the value on left side of >= greater than or equal to the value on the right side. Otherwise returns false.
Contin.
Less than or equal to [<=]:Less than or equal to operator returns true if the value on left side of <= less than or equal to the value on the right side. Otherwise returns false.
Not Equal to !=:Not Equal to operator returns true if the value on left side of != is not equal to the value on the right . Otherwise returns false.
Contin.
Example if A=10 and B =5 the relational operator and their result
Contin>
Relational Expression
Result
A>B True
A<B False
A<=B False
A>=B False
A==B False
A!=B True
The Logical operators are used to evaluate compound condition.
Logical operators are as follows:1. AND Operators(&&)2. OR Operators(||)3. NOT Operators(!)1) AND Operators(&&)The symbols used for AND operator is (&&). It is used to evaluate two condition. It produce result true if both condition is true. It produce result false if any of the condition is false.
Logic operators
Contin. Table of AND operatorCondition 1 Operator Condition 2 Result
False && False False
False && True False
True && False False
True && True True
Example:Suppose we have tow variables A=100 and
B=50. the compound condition (A>10) &&(B>10) is true .it contain two conditions and both are true. So whole compound condition is true.
The compound condition (A>50) &&(B>50) is false. it contain two conditions one is true and one is false. So whole compound condition is false.
Contin.
2) OR Operators(||)The symbols used for OR operator is (||). It is used to evaluate two condition. It produce result true if any of the condition is true. It produce result false if both condition are false
Contin.
Condition 1 Operator Condition 2 Result
False || False False
False || True True
True || False True
True || True True
Example:Suppose we have tow variables A=100 and
B=50. the compound condition (A>50) ||(B>50) is true .it
contain two conditions and one condition is true. So whole compound condition is true.
The compound condition (A>500) ||(B>500) is false. it contain two conditions and both are false. So whole compound condition is false.
Contin.
3) NOT Operators(!) The symbol used for NOT is (!). It is used to reverse the result of a condition It produces true if the condition is false. It produces false if the condition is true.Example:Suppose we have tow variables A=100 and B=50.
the condition !(A==B) is true .The result of (A==B) is false but NOT operator converts it into true.
The condition !(A>B) is false .The result of (A>B) is true but NOT operator converts it into false.
Contin.
The assignment operator = is used in assignment statement to assign a value or computational result to a variable.
Syntax:Variable_name= expression;Variable_name it is the name of variable= it the assignment
operatorExpression it is any valid expression; statement terminator
Assignments Operators
The increment operator is used to increase the value of variable by one.
It is denoted by the symbol ++.The increment operator cannot increment the
value of constants and expressions.For example:Valid statements Invalid statementsA++,x++ 10++
(a+b)++,or ++(a+b)
Increment Operator
Forms of increment operator: PrefixIn prefix form ,the increment operator is
written before the variable as follows:++y; PostfixIn postfix form ,the increment operator is
written after the variable as follows:y++;
Contin.
Let A=++B and A=B++ are different:In prefix: A=++BIt increment the value of B by one.It assign the value of B to A.++B;A=B;In postfix: A=B++It assign the value of B to A.It increment the value of B by one.A=B;++B;
Difference between prefix and postfix by example
The decrement operator is used to decrease the value of variable by one.
It is denoted by the symbol --.The decrement operator cannot decrement
the value of constants and expressions.For example:Valid statements Invalid statementsA--,x-- 10--
(a+b)--,or --(a+b)
Decrement Operator
Forms of decrement operator: PrefixIn prefix form ,the decrement operator is
written before the variable as follows:--y; PostfixIn postfix form ,the decrement operator is
written after the variable as follows:y--;
Contin.
Let A=--B and A=B--are different:In prefix: A=--BIt decrement the value of B by one.It assign the value of B to A.--B;A=B;In postfix: A=B--It assign the value of B to A.It increment the value of B by one.A=B;--B;
Difference between prefix and postfix by example
That combine assignment operator with arithmetic operators.
We are used to perform mathematical operator more easily.
Syntax:Variable op=expressionE.g. N+=10; is equalvent to N=N+10;
Compound Assignment Operators
The syntax of cout and << is:
Called an output statementThe stream insertion operator is <<Expression evaluated and its value is printed
at the current cursor position on the screen
“Cout "and "cin"
cin is used with >> to gather input
The stream extraction operator is >>For example,
cin >>a;Causes computer to get a value of type intPlaces it in the variable a
Contin.
If statementIf else statementIf- else –if statementNested if statementSwitch case statement
Decision statements
If is a decision-making statement.It is the simplest form of decision statement.Syntax:If(condition){Statement1;Statement2;..StatementN;}
If statement
Condition is given as relational expression.If the condition is true the statement or set of
statement is executed.If the condition is false the statement or set of
statement is not executed.
Contin.
#include<iostream>using namespace std;int main(){
int a;cin>>a;if(a>40)
cout<<"you are pass";system("pause");
}
Example
It execute one block of statement when the condition it true and the other if the condition is false.
In any situation, one block is executed and the other Is skipped.Syntax:If(condition){Statement(s);}else{Statement(s);}
If else statement
#include<iostream>using namespace std;int main(){
int a;cin>>a;if(a>40)
cout<<"you are pass";else
cout<<"you are fail";system("pause");
}
Example
Use to choose one block of statements from many block of statements.
It is used when there are many option and only one block of statement should be executed on the basis of a condition.
If- else –if statement
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"enter a number";
cin>>a;
if(a>0)
cout<<"the number is positive";
else if(a<0)
cout<<"the number is negative";
else
cout<<"the number is zero";
system("pause");
}
Example
An if statement within an if statement is called nested if statement.
In structure, the enter into the inner if only when the outer condition is true.
If the condition is false the control moves to the outer part of the outer if and skipped the inner if statement.
Neasted if statement
It is an alternative of nested if statement. It can be used easily when there are many choices available and only one should be
executed.
Syntax:
Switch(expression)
{
case val 1:
Statements1;
break;
.
.
case val n:
Statements1;
break;
Default:
Statements1;
}
Switch case statement
For loop execute one or more statement for a specified number of times.
Syntax:For( intialization ; condition ;
increment/decrement ){Statement(s);}
For Loop
#include<iostream>using namespace std;int main(){ int n; for(n=1;n<=5;n++) cout<<n<<endl; system("pause");}
Example
While loop executes one or more statement while the given condition remain true.
It is useful where the number of iterations is not know in advance.
Condition become before the body of loop.Syntax:While(condition){Statement(s);}
While Loop
#include<iostream>using namespace std;int main(){
int n=1;while(n<=5){
cout<<"Pakistan"<<endl;n++;
}system("pause");
}
Example
Do while loop executes one or more statement while the given condition remain true.
Important where a statement must be executed at least once.
Condition become after the body of loop.SyntaxDo{Statement(s)}While(condition);
Do while Loop