SlideShare a Scribd company logo
C++ Basics
Inam Ul Haq
Lecturer in Computer Science
University of Education Okara Campus
inam@ue.edu.pk, inam.bth@gmail.com
Subject: Programming Language for Mathematics
Class: BS(Hons) Mathematics
Header Files
// header files are preprocessor directives that are included to call built-in
functions to be used in the program
// e.g. for keywords: cin and cout, the header file iostream.h is used
#include <iostream.h>
Void main() {
//variable declaration
//read values input from user
//computation and print output to user
return 0;
}
Note: After you write a C++ program you compile it; that is, you run a
program called compiler that checks whether the program follows the C++
syntax:
• if it finds errors, it lists them
• If there are no errors, it translates the C++ program into a program in machine
language which you can execute
UniversityofEducationOkara
Campus
2
Notes
• // is used for comments (this is not executed)
• indentation is for the convenience of the reader; compiler ignores all spaces and new line ;
the delimiter for the compiler is the semicolon
• E.g.
• Void main()
• {
• Clrscr();
• Int a,b;
• If(a==b)
• {
• Cout<<“a is equal to b”<<endl;
• all statements ended by semicolon
• Lower vs. upper case matters!!
• Void is different than void
• Main is different that main
UniversityofEducationOkara
Campus
3
Hello world program
When learning a new language, the first program people
usually write is one that salutes the world :)
Here is the Hello world program in C++.
#include <iostream.h>
Void main() {
cout << “Hello world!”;
getch();
}
UniversityofEducationOkara
Campus
4
Variable declaration
Syntax: <variable-name> <type>
e.g:
• int n, average; //integer
• Double m; //real number
• char c, my-character//character
UniversityofEducationOkara
Campus
5
Input statements
cin >> variable-name;
Meaning: read the value of the variable called <variable-
name> from the user
Example:
cin >> a;
cin >> b >> c;
cin >> x;
cin >> my-character;
UniversityofEducationOkara
Campus
6
Output statements
cout << variable-name;
Meaning: print the value of variable <variable-name> to the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl;
Meaning: print a new line
Example:
cout << a;
cout << b << c;
cout << “This is my character: “ << my-character << “ he he he”
<< endl;
UniversityofEducationOkara
Campus
7
If statements
if (condition) {
S1;
}
else {
S2;
}
S3;
condition
S1 S2
S3
True False
UniversityofEducationOkara
Campus
8
Boolean conditions
..are built using
• Comparison operators
== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
• Boolean operators
&& and
|| or
! not
UniversityofEducationOkara
Campus
9
Examples
Assume we declared the following variables:
int a = 2, b=5, c=10;
Here are some examples of boolean conditions we can
use:
• if (a == b) …
• if (a != b) …
• if (a <= b+c) …
• if(a <= b) && (b <= c) …
• if !((a < b) && (b<c)) …
UniversityofEducationOkara
Campus
10
If Statement
#include <iostream.h>
void main() {
int a,b,c;
cin >> a >> b >> c;
if (a <=b) {
cout << “min is “ << a << endl;
}
else {
cout << “ min is “ << b << endl;
}
cout << “happy now?” << endl;
}
UniversityofEducationOkara
Campus
11
While Loop
while (condition) {
S1;
}
S2;
condition
S1
S2
True False
UniversityofEducationOkara
Campus
12
While example
//read 100 numbers from the user and output their sum
#include <iostream.h>
void main() {
int i, sum, x;
sum=0;
i=1;
while (i <= 100) {
cin >> x;
sum = sum + x;
i = i+1;
}
cout << “sum is “ << sum << endl;
}
UniversityofEducationOkara
Campus
13
For Loop
• for ( initialization; conidition; increment/decrement)
• {
• Statement 1’
• Statement 2;
}
UniversityofEducationOkara
Campus
14
For Loop Example
• #include <iostream>
• int main ()
• {
• for( int a = 10; a < 20; a = a + 1 )
• {
• cout << "value of a: " << a << endl;
• }
• return 0;
• }
UniversityofEducationOkara
Campus
15
Do While Loop
• do
{
     block of code;
statement 1;
statement 2;
     }
while (condition); //it is called test condition
UniversityofEducationOkara
Campus
16
Do While Loop Example
• #include <iostream> 
• #include <conio.h>
• int main () 
• { 
• int a = 10;
• do 
•     { 
•        cout << "value of a: " << a << endl; 
•        a = a + 1; 
•     }
•   while( a < 20 ); 
• return 0; 
• }
UniversityofEducationOkara
Campus
17
Exercise
• Write a program that  asks the user
• Do you want to use this program? (y/n)
• If the user says ‘y’ then the program terminates
• If the user says ‘n’ then the program asks
• Are you really sure you do not want to use this
program? (y/n)
• If the user says ‘n’ it terminates, otherwise it prints 
again the message
• Are you really really sure you do not want to use this
program? (y/n)
• And so on,  every time adding one more “really”. 
UniversityofEducationOkara
Campus
18

More Related Content

PPT
C++basics
PPT
C++basics
PPTX
Hello world! Intro to C++
PDF
CP Handout#8
DOCX
C program report tips
PPT
Beginner C++ easy slide and simple definition with questions
PDF
CP Handout#9
PDF
CP Handout#5
C++basics
C++basics
Hello world! Intro to C++
CP Handout#8
C program report tips
Beginner C++ easy slide and simple definition with questions
CP Handout#9
CP Handout#5

What's hot (20)

PDF
CP Handout#6
PPTX
Testing lecture after lec 4
PPT
Beginner C++ easy slide and simple definition with questions
PPTX
Lecture 1
PDF
CP Handout#7
PPTX
C programming language
PDF
Continuations in Ruby (Anton Vasiljev)
PDF
CP Handout#10
PDF
Learning the C Language
PDF
Hands-on Introduction to the C Programming Language
PPT
Introduction to c language by nitesh
PDF
180 daraga cpp course session-1
PPT
Lecture 3 c++
PPTX
C programming
DOCX
Ppl home assignment_unit2
PDF
Computer programming chapter ( 4 )
PDF
Mid term sem 2 1415 sol
PPT
C and C++ Industrial Training Jalandhar
PDF
Anton Vasiljev: Continuations in Ruby.
PDF
Reduce course notes class xii
CP Handout#6
Testing lecture after lec 4
Beginner C++ easy slide and simple definition with questions
Lecture 1
CP Handout#7
C programming language
Continuations in Ruby (Anton Vasiljev)
CP Handout#10
Learning the C Language
Hands-on Introduction to the C Programming Language
Introduction to c language by nitesh
180 daraga cpp course session-1
Lecture 3 c++
C programming
Ppl home assignment_unit2
Computer programming chapter ( 4 )
Mid term sem 2 1415 sol
C and C++ Industrial Training Jalandhar
Anton Vasiljev: Continuations in Ruby.
Reduce course notes class xii
Ad

Similar to C++ Basics (20)

PPT
c++basics.ppt
PPT
c++basics.ppt
PPT
c++basics.ppt
PPT
c++basiccs.ppt
PPT
C++basics
PPT
C++ basics
ODP
OpenGurukul : Language : C++ Programming
PPTX
Cs1123 6 loops
PDF
4. programing 101
PPTX
Introduction& Overview-to-C++_programming.pptx
PPTX
Introduction to C++ programming language
PPTX
Oop object oriented programing topics
PPTX
C++ loop
PPTX
Lecture 1 Introduction C++
PPTX
lesson 2.pptx
PPTX
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
PPTX
Computer programming
PPTX
Switch case and looping kim
PPTX
Computational Physics Cpp Portiiion.pptx
c++basics.ppt
c++basics.ppt
c++basics.ppt
c++basiccs.ppt
C++basics
C++ basics
OpenGurukul : Language : C++ Programming
Cs1123 6 loops
4. programing 101
Introduction& Overview-to-C++_programming.pptx
Introduction to C++ programming language
Oop object oriented programing topics
C++ loop
Lecture 1 Introduction C++
lesson 2.pptx
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
Computer programming
Switch case and looping kim
Computational Physics Cpp Portiiion.pptx
Ad

More from university of education,Lahore (20)

PPT
Activites and Time Planning
PPT
Classical Encryption Techniques
PPT
Activites and Time Planning
PPTX
OSI Security Architecture
PPTX
Network Security Terminologies
PPT
Project Scheduling, Planning and Risk Management
PPTX
Software Testing and Debugging
PPTX
PPT
Enterprise Application Integration
PPTX
PPTX
Itertaive Process Development
PPTX
Computer Aided Software Engineering Nayab Awan
PPTX
Lect 2 assessing the technology landscape
PPTX
system level requirements gathering and analysis
Activites and Time Planning
Classical Encryption Techniques
Activites and Time Planning
OSI Security Architecture
Network Security Terminologies
Project Scheduling, Planning and Risk Management
Software Testing and Debugging
Enterprise Application Integration
Itertaive Process Development
Computer Aided Software Engineering Nayab Awan
Lect 2 assessing the technology landscape
system level requirements gathering and analysis

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Classroom Observation Tools for Teachers
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Basic Mud Logging Guide for educational purpose
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
master seminar digital applications in india
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Classroom Observation Tools for Teachers
Anesthesia in Laparoscopic Surgery in India
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
01-Introduction-to-Information-Management.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Basic Mud Logging Guide for educational purpose
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Supply Chain Operations Speaking Notes -ICLT Program
human mycosis Human fungal infections are called human mycosis..pptx
Pre independence Education in Inndia.pdf
master seminar digital applications in india
Microbial diseases, their pathogenesis and prophylaxis
O7-L3 Supply Chain Operations - ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf

C++ Basics

  • 1. C++ Basics Inam Ul Haq Lecturer in Computer Science University of Education Okara Campus inam@ue.edu.pk, inam.bth@gmail.com Subject: Programming Language for Mathematics Class: BS(Hons) Mathematics
  • 2. Header Files // header files are preprocessor directives that are included to call built-in functions to be used in the program // e.g. for keywords: cin and cout, the header file iostream.h is used #include <iostream.h> Void main() { //variable declaration //read values input from user //computation and print output to user return 0; } Note: After you write a C++ program you compile it; that is, you run a program called compiler that checks whether the program follows the C++ syntax: • if it finds errors, it lists them • If there are no errors, it translates the C++ program into a program in machine language which you can execute UniversityofEducationOkara Campus 2
  • 3. Notes • // is used for comments (this is not executed) • indentation is for the convenience of the reader; compiler ignores all spaces and new line ; the delimiter for the compiler is the semicolon • E.g. • Void main() • { • Clrscr(); • Int a,b; • If(a==b) • { • Cout<<“a is equal to b”<<endl; • all statements ended by semicolon • Lower vs. upper case matters!! • Void is different than void • Main is different that main UniversityofEducationOkara Campus 3
  • 4. Hello world program When learning a new language, the first program people usually write is one that salutes the world :) Here is the Hello world program in C++. #include <iostream.h> Void main() { cout << “Hello world!”; getch(); } UniversityofEducationOkara Campus 4
  • 5. Variable declaration Syntax: <variable-name> <type> e.g: • int n, average; //integer • Double m; //real number • char c, my-character//character UniversityofEducationOkara Campus 5
  • 6. Input statements cin >> variable-name; Meaning: read the value of the variable called <variable- name> from the user Example: cin >> a; cin >> b >> c; cin >> x; cin >> my-character; UniversityofEducationOkara Campus 6
  • 7. Output statements cout << variable-name; Meaning: print the value of variable <variable-name> to the user cout << “any message “; Meaning: print the message within quotes to the user cout << endl; Meaning: print a new line Example: cout << a; cout << b << c; cout << “This is my character: “ << my-character << “ he he he” << endl; UniversityofEducationOkara Campus 7
  • 8. If statements if (condition) { S1; } else { S2; } S3; condition S1 S2 S3 True False UniversityofEducationOkara Campus 8
  • 9. Boolean conditions ..are built using • Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal • Boolean operators && and || or ! not UniversityofEducationOkara Campus 9
  • 10. Examples Assume we declared the following variables: int a = 2, b=5, c=10; Here are some examples of boolean conditions we can use: • if (a == b) … • if (a != b) … • if (a <= b+c) … • if(a <= b) && (b <= c) … • if !((a < b) && (b<c)) … UniversityofEducationOkara Campus 10
  • 11. If Statement #include <iostream.h> void main() { int a,b,c; cin >> a >> b >> c; if (a <=b) { cout << “min is “ << a << endl; } else { cout << “ min is “ << b << endl; } cout << “happy now?” << endl; } UniversityofEducationOkara Campus 11
  • 12. While Loop while (condition) { S1; } S2; condition S1 S2 True False UniversityofEducationOkara Campus 12
  • 13. While example //read 100 numbers from the user and output their sum #include <iostream.h> void main() { int i, sum, x; sum=0; i=1; while (i <= 100) { cin >> x; sum = sum + x; i = i+1; } cout << “sum is “ << sum << endl; } UniversityofEducationOkara Campus 13
  • 14. For Loop • for ( initialization; conidition; increment/decrement) • { • Statement 1’ • Statement 2; } UniversityofEducationOkara Campus 14
  • 15. For Loop Example • #include <iostream> • int main () • { • for( int a = 10; a < 20; a = a + 1 ) • { • cout << "value of a: " << a << endl; • } • return 0; • } UniversityofEducationOkara Campus 15
  • 16. Do While Loop • do {      block of code; statement 1; statement 2;      } while (condition); //it is called test condition UniversityofEducationOkara Campus 16
  • 17. Do While Loop Example • #include <iostream>  • #include <conio.h> • int main ()  • {  • int a = 10; • do  •     {  •        cout << "value of a: " << a << endl;  •        a = a + 1;  •     } •   while( a < 20 );  • return 0;  • } UniversityofEducationOkara Campus 17
  • 18. Exercise • Write a program that  asks the user • Do you want to use this program? (y/n) • If the user says ‘y’ then the program terminates • If the user says ‘n’ then the program asks • Are you really sure you do not want to use this program? (y/n) • If the user says ‘n’ it terminates, otherwise it prints  again the message • Are you really really sure you do not want to use this program? (y/n) • And so on,  every time adding one more “really”.  UniversityofEducationOkara Campus 18