Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Fundamental
Programming
Group Members
Taimur Ali #22
Fouzia Parveen #23
Muhammad Anjum #24
Muhammad Hamza #26
M.Hassan Raza #27
Saifullah #40
Program
A program is a collection
of instructions that performs a
specific task when executed by
a computer.
Programming
.The process of developing
and implementing
various sets of instructions to
enable a computer to do a
certain task.
Programming
Language
A set of symbols, codes and words used to
write a program.
• Programming languages are artificial
languages created to tell the computer
what to do
• They consist of vocabulary and a set of
rules (grammar/syntax) to write
programs
Cont.…..
Different programming Languages are
Java
Ada
C & C++
Machine Language
COBOL
Forton
BASIC
C++ Language
Object oriented programming.
Developed by Bjarne Stroustrup in
early 1980s.
Designed for UNIX system
environment.
Structure of C++
Program
Consists of three main parts.
Preprocessor Directives
The Main( ) Function
C++ Statement
Preprocessor
Directives
The instructions that are
given to the compiler
before the beginning of
the actual program .
Cont.……
Normally start with a number sign “#” and the
key word “include” or “define”.
Used to include the Header files in the
program.
Example:
#include<iostream.h>
#include<Conio.h>
Header Files
A C++ source file that contains
the definitions of the library
functions
A header file is added if the function defined in
it is to be used in the program
The main( )
Function
It indicates the beginning of C++
program.
When a program is executed , the
control goes directly to the main( )
function.
C++ Statements
Statements are written within the
main function.
These statements are the body of
the Program.
Each statement ends with
semicolon” ; “
Example
1) #include<iostream.h>
2)Main ( )
3){
4)Cout<<“ hello Class”;
5)}
Some Basic
Definitions
Constants
A quantity that cannot
change it’s value during
execution is called
constant.
Types of Constants
Integer Constants
Floating Point Constants
Character Constants
String Constants
Variables
A quantity whose value may
change during execution is
called Variable.
Rules of Writing variable
names
First character must be in Alphabetic character.
Underscore can be used as first character.
Blank Spaces are not allowed.
Special Characters and reserved words can bot
be used.
A name declare for one data type can not be
used to declare other data type.
Data types
C++ has five basic Data types.
1)int Integer
2)float Floating Point
3)double Double Precision
4)char Characters
5)bool Boolean
Declaration of
variables
Assigning the name and data
type that a variable can hold is
called declaration of variables
Example:
float abc
Initialization of
Variables
When a known value is assigned
to a variable at the time of it’s
declaration, it is called
initialization of variable.
Examples:
int a=5 , b=10;
“Cout” object
Stands for “ console output”.
A part of “iostream” header
file.
Used to display output on
computer screen.
The “endl”
manipulator
Stands for end of line.
A predefined iostream
manipulator
“cin “ object
Stands for console input .
Also a part of iostream header
file.
Used to get input from
keyboard during execution of
the program.
Operators
An operator is a symbol that
tells the compiler to perform
specific mathematical or logical
manipulations.
Cont.…..
Different Operators are
Arithmetic Operators
Relational Operators
Logical Operators
Increment and Decrement Operators
Assignment Operators
Arithmetic Operator
The five arithmetical operations
supported by C++ are
Relational Operators
Logical Operator
There are following logical operators supported by C++
language
Assume variable A holds 1 and variable B holds 0, then:
Assignment Operators
Increment and
Decrement
Conditional
Statements
Also called selection
statements.
Used to execute set of
statements after testing a
condition.
“If” Statement
“if” statement is used to execute set of statements
after testing a condition.
syntax
if (<codition
>)
{
<statement1>;
<statement2>;
…
<statementN>;
}
Cont.….
Flowchart:
Example:
if ( grade >= 60 )
cout << "Passed";
“if-else” Condition
Another form of “ if statement” used for making two
way decisions.
Syntax:
if(Condition)
Statement1;
else
Statement2;
Cont.….
Flowchart:
Example:
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
“Nested if” Conditions
Syntax:
Cont.…
Flowchart:
Cont.…..
Example:
int examGrade, quizGrade;
if (examGrade < 60)
cout << “We have a problem” << endl;
if(quizGrade < 10)
cout << “We have a real problem” << endl;
else
cout << “Ok”;
“Nested if-else”
Conditions
Used for multi-way decision making.
Syntax:
if(Condition1)
Statement1;
else if(Condition2)
Statement2;
else if(Condition3)
Statement3;
else
Statement-n;
Cont.….
• Flowchart:
Cont.…..
Example:
if ( grade >= 90 )
cout << "A";
else if ( grade >= 80 )
cout << "B";
else if ( grade >= 70 )
cout << "C";
else if ( grade >= 60 )
cout << "D";
else
cout << "F";
The “Switch” Statement
used to control very complex conditional and branching operations.
Syntax:
switch(expression)
{
case constant1: statement1;
break;
case constant2: statement2;
break;
case constantN : statementN;
break;
default : statementDefault;
break;
}
Cont.……
Flowchart:
Cont.…..
Example:
#include <iostream>
int main ()
{
char grade = 'D';
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
Cont.……
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default : cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl
; return 0;
}
Cont.…..
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default : cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl
; return 0;
}
Break Statements
The break; statement terminates the
loop(for, while and do-while loop) and
switch statement immediately when it
appears.
“Nested if-else and Switch
Statements
Nested if-else Switch Statements
Becomes complicated Easy to understand for
for multiple selections. multiple selections.
Uses an independent Uses a single expression
expression for each for all cases, but each
case. case must have a
constant value of integer
type or character type.
Loop Statements
Loop is a control structure
that repeats a group of steps
in a program.
“while” loop
A while loop statement repeatedly executes a
target statement as long as a given condition is
true.
Syntax:
while(condition)
{ statement(s); }
Cont.…..
Flow chart:
Cont…..
Example:
#include <iostream>
int main ()
{
int a = 10;
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
“do while” loops
in case of do...while loop, body of loop is executed first
then only test expression is checked.
Syntax:
do
{
statement/s;
}
while (test expression);
Cont.….
Flowchart:
Cont….
Example:
#include <iostream>
int main()
{
float number, sum = 0.0;
do
{
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);
cout<<"Total sum = "<<sum;
return 0;
}
“for” Loop
The statements in the for loop repeat
continuously for a specific number of
times.
The while and do-while loops repeat
until a certain condition is
met. The for loop repeats until a specific
count is met.
Cont.…..
Syntax:
for(initial expression; test expression; update
expression)
{
body of loop;
}
Cont….
Flowchart:
Cont.……
Example:
#include <iostream>
using namespace std;
int main()
{
int i, n, factorial = 1;
cout<<"Enter a positive integer: ";
cin>>n;
for (i = 1; i <= n; ++i)
{ factorial *= i; // factorial = factorial * i;
}
cout<< "Factorial of "<<n<<" = "<<factorial; return 0;
}
Arrays
• An array is a sequence of objects of same data
types
One Dimensional Array
Examples:
type arrayName [ arraySize ];
Initialization of Arrays
Array’s elements can be initialized one
by one or using a single statement.
Example:
double balance[5] = {1000.0, 2.0, 3.4, 17.0,
50.0};
Multidimensional Array