2-Lecture-FoP-intro To C++
2-Lecture-FoP-intro To C++
Introduction to C++
Dr Ayesha Zeb
Email: [email protected]
Lecture Contents
• Programming languages
• Components of programming languages
• Programming languages rules
• Basic parts of a program
• Software development cycle
• Flowcharts and Pseudocodes
• Errors in Programs
Programming Languages
• Syntax
• Semantics
Components of a language
Syntax
#include <iostream>
int main()
{
cout << "Hello, World!";
return 0;
}
Tokens
• A token is the smallest element of a C++ program that is
meaningful to the compiler.
Token Type Description/Purpose Examples
Keywords Words with special int, double, for, auto
meaning to the compiler
Identifiers Names of things that are cout, std, x, myFunction
not built into the language
int main()
{
cout << "Hello World!";
return 0;
}
C++ HelloWorld
Lines beginning with # are preprocessor commands, they change what code
is actually being compiled.
#include tells the preprocessor to dump in the contents of another file, here
it’s iostream, which defines the procedures for input/output
//EC 100 Algorithms and Computing
//A first program
//Hello World – displays “Hello World!”
//on the screen
#include <iostream>
int main()
{
cout << "Hello World!";
return 0;
}
C++ HelloWorld
A namespace is generally a prefix that is applied to all the names in a certain
set. Think of them as toolboxes that allow us to use different tools. Formally
we can refer to these toolboxes as “classes” and the “tools” as objects.
The 100
//EC command using
Algorithms and tells the compiler to allow
Computing all names in the “std”
//A first program
namespace
//Hello World to be usable
– displays without
“Hello their prefix
World!”
//on the screen
#include <iostream>
int main()
{
cout << "Hello World!";
return 0;
}
C++ HelloWorld
int main()
{
cout << "Hello World!";
return 0;
Without
} the line “using namespace std;” we would have had to write
std::cout<<“Hello World!”;
:: is called the scope resolution operator – it tells the compiler that the scope
of cout is in the namespace std
C++ HelloWorld
int main() { … } defines the code that should execute when the program starts
up. The curly braces represent grouping of multiple commands into a block
#include <iostream>
int main()
{
cout << "Hello World!";
return 0;
}
C++ HelloWorld
cout<<“Hello
//EC World!”; tellsand
100 Algorithms the compiler
Computingto perform with cout.
//A first program
return 0; indicates that the program should tell the operating system it has
//Hello World – displays “Hello World!”
finished successfully.
//on the screen
#include <iostream>
int main()
{
cout << "Hello World!";
return 0;
}
C++ HelloWorld
Every statement ends with a semicolon (;)
//EC 100 Algorithms and Computing
; indicates the end of a line in C++
//A
Do notfirst program
forget the ;
//Hello World – displays “Hello World!”
//on the screen
#include <iostream>
int main()
{
cout << "Hello World!";
return 0;
}
A basic program
#include <iostream>
int main()
{
cout << "Hello, World!";
return 0;
}
Descrip
tive
comme
Include nts
files
Telling the compiler to use
main() the prefix std where required
function
Label the parts
Program Algorithm
Maintenance Development
Program
Coding
Documentation
Testing
and
Debugging
Software Development Cycle
is called an algorithm.
Algorithms
• Sequence Matters!
1 Make the cookie batter.
– Mix the dry ingredients.
– Cream the butter and sugar.
– Beat in the eggs.
– Stir in the dry ingredients.
• What is an Algorithm?
– The steps to be taken in order to solve a problem form an
algorithm.
– It may also be called designing the solution.
– Hundreds of algorithms for different problems have been
developed and made available for use, such as sorting
algorithms and searching algorithms.
Developing an Algorithm
• Gather Requirements
– Identify inputs and outputs
• Step wise refinement
– State how you can convert the input to the output
– Break the entire process into small steps
– Test each small step before moving on to the next one
Developing an Algorithm
Symbol Description
TERMINAL - To start or end a flowchart
INPUT / OUTPUT - Used with Read, Input, Print and other I/O
commands.
PROCESSING - Used for operations done inside the computer.
Such as calculations, storing and moving of data.
DECISION - Used to ask a question in programming. Questions
are Yes/No format (Used with the If Statement).
Variables Algorithm
Step 4- Print S,
Step 5- Stop
Flowchart example
Start
Read X,Y
Flowchart
S= X+Y
Print S
Stop
Errors in programs
Syntax Error!!
But you still understood the meaning (no semantic error)
Errors in programs
Semantic Error!!
But syntactically correct
Errors in programs
• Syntax errors
• Semantic errors
• Runtime errors
Syntax errors
• cout<<“Hello World!”
• cout<<Hello World!”;
Semantic Errors
http://www.computerhistory.org/
C++ HelloWorld – A First Program
#include <iostream>