SlideShare a Scribd company logo
1 of 14
C++ BASICS
CSCI 107
A C++ PROGRAM
//include headers; these are modules that include functions that you may use in your
//program; we will almost always need to include the header that
// defines cin and cout; the header is called iostream.h
#include <iostream.h>
int main() {
//variable declaration
//read values input from user
//computation and print output to user
return 0;
}
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
NOTES
• what follows after // on the same line is considered comment
• indentation is for the convenience of the reader; compiler ignores all spaces and
new line ; the delimiter for the compiler is the semicolon
• all statements ended by semicolon
• Lower vs. upper case matters!!
• Void is different than void
• Main is different that main
THE INFAMOUS
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>
int main() {
cout << “Hello world!”;
return 0;
}
VARIABLE DECLARATION
type variable-name;
Meaning: variable <variable-name> will be a variable of type <type>
Where type can be:
• int //integer
• double //real number
• char //character
Example:
int a, b, c;
double x;
int sum;
char my-character;
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;
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;
IF STATEMENTS
if (condition) {
S1;
}
else {
S2;
}
S3;
condition
S1 S2
S3
True False
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
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)) …
IF EXAMPLE
#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;
}
WHILE STATEMENTS
while (condition) {
S1;
}
S2;
condition
S1
S2
True False
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;
}
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”.

More Related Content

What's hot

Connecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindConnecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with Embind
Chad Austin
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
aeden_brines
 
Fix: static code analysis into our project
Fix: static code analysis into our project Fix: static code analysis into our project
Fix: static code analysis into our project
noelchris3
 
180 daraga cpp course session-1
180 daraga cpp course session-1180 daraga cpp course session-1
180 daraga cpp course session-1
Moustafa Ghoniem
 
05 control structures 2
05 control structures 205 control structures 2
05 control structures 2
Jomel Penalba
 

What's hot (20)

Connecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindConnecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with Embind
 
Computer programming k 12
Computer programming k 12Computer programming k 12
Computer programming k 12
 
C# 6.0
C# 6.0C# 6.0
C# 6.0
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
 
Introduction to C++,Computer Science
Introduction to C++,Computer ScienceIntroduction to C++,Computer Science
Introduction to C++,Computer Science
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
C++ basics
C++ basicsC++ basics
C++ basics
 
GCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programsGCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programs
 
Fix: static code analysis into our project
Fix: static code analysis into our project Fix: static code analysis into our project
Fix: static code analysis into our project
 
180 daraga cpp course session-1
180 daraga cpp course session-1180 daraga cpp course session-1
180 daraga cpp course session-1
 
selection structures
selection structuresselection structures
selection structures
 
05 control structures 2
05 control structures 205 control structures 2
05 control structures 2
 
Chp1(c 2 c++)
Chp1(c 2 c++)Chp1(c 2 c++)
Chp1(c 2 c++)
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...
 
Class7
Class7Class7
Class7
 
Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)
 
Output
OutputOutput
Output
 
人力
人力人力
人力
 

Viewers also liked

How did you use media technologies in the planning and research, construction...
How did you use media technologies in the planning and research, construction...How did you use media technologies in the planning and research, construction...
How did you use media technologies in the planning and research, construction...
Madzipan
 
Scott Moore relevant accomplishments bio
Scott Moore relevant accomplishments bioScott Moore relevant accomplishments bio
Scott Moore relevant accomplishments bio
Scott Moore
 
La integración de españa
La integración de españaLa integración de españa
La integración de españa
jorgeghistoria
 
Los gobiernos democráticos
Los gobiernos democráticosLos gobiernos democráticos
Los gobiernos democráticos
jorgeghistoria
 
Franquismo (1939 1957)
Franquismo (1939 1957)Franquismo (1939 1957)
Franquismo (1939 1957)
jorgeghistoria
 
Criteris geo juny2015
Criteris geo juny2015Criteris geo juny2015
Criteris geo juny2015
Txema Gs
 

Viewers also liked (16)

How did you use media technologies in the planning and research, construction...
How did you use media technologies in the planning and research, construction...How did you use media technologies in the planning and research, construction...
How did you use media technologies in the planning and research, construction...
 
Evaluation
EvaluationEvaluation
Evaluation
 
Tralukya Hazarika
Tralukya HazarikaTralukya Hazarika
Tralukya Hazarika
 
“EMBARAZOS EN LA ADOLESCENCIA “
“EMBARAZOS EN LA ADOLESCENCIA ““EMBARAZOS EN LA ADOLESCENCIA “
“EMBARAZOS EN LA ADOLESCENCIA “
 
Scott Moore relevant accomplishments bio
Scott Moore relevant accomplishments bioScott Moore relevant accomplishments bio
Scott Moore relevant accomplishments bio
 
AttesterIngmar
AttesterIngmarAttesterIngmar
AttesterIngmar
 
Susan Rep SS 2013
Susan Rep SS 2013Susan Rep SS 2013
Susan Rep SS 2013
 
Lookbook ss13 lmc-updated showrooms
Lookbook ss13 lmc-updated showroomsLookbook ss13 lmc-updated showrooms
Lookbook ss13 lmc-updated showrooms
 
La integración de españa
La integración de españaLa integración de españa
La integración de españa
 
Los gobiernos democráticos
Los gobiernos democráticosLos gobiernos democráticos
Los gobiernos democráticos
 
Franquismo (1939 1957)
Franquismo (1939 1957)Franquismo (1939 1957)
Franquismo (1939 1957)
 
Propuestas y proyectos de intervencion docente
Propuestas y proyectos de intervencion docentePropuestas y proyectos de intervencion docente
Propuestas y proyectos de intervencion docente
 
Gestão de Varejo Ambiental no Varejo e Supermercados
Gestão de Varejo Ambiental no Varejo e SupermercadosGestão de Varejo Ambiental no Varejo e Supermercados
Gestão de Varejo Ambiental no Varejo e Supermercados
 
Supportive Periodontal Treatment
Supportive Periodontal TreatmentSupportive Periodontal Treatment
Supportive Periodontal Treatment
 
Criteris geo juny2015
Criteris geo juny2015Criteris geo juny2015
Criteris geo juny2015
 
Anthony Cronin
Anthony CroninAnthony Cronin
Anthony Cronin
 

Similar to C++basics

c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
EPORI
 
introductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdfintroductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdf
SameerKhanPathan7
 

Similar to C++basics (20)

c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
 
c++basiccs.ppt
c++basiccs.pptc++basiccs.ppt
c++basiccs.ppt
 
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
 
c++basics.ppt
c++basics.pptc++basics.ppt
c++basics.ppt
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
introductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdfintroductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdf
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
Programming using c++ tool
Programming using c++ toolProgramming using c++ tool
Programming using c++ tool
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************
 
Lesson 2 starting output
Lesson 2 starting outputLesson 2 starting output
Lesson 2 starting output
 
C++ Chapter 3
C++ Chapter 3C++ Chapter 3
C++ Chapter 3
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 

More from amna izzat (6)

1 introduction ddbms
1 introduction ddbms1 introduction ddbms
1 introduction ddbms
 
Byte division
Byte divisionByte division
Byte division
 
Avl trees
Avl treesAvl trees
Avl trees
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
Selection sort
Selection sortSelection sort
Selection sort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

C++basics

  • 2. A C++ PROGRAM //include headers; these are modules that include functions that you may use in your //program; we will almost always need to include the header that // defines cin and cout; the header is called iostream.h #include <iostream.h> int main() { //variable declaration //read values input from user //computation and print output to user return 0; } 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
  • 3. NOTES • what follows after // on the same line is considered comment • indentation is for the convenience of the reader; compiler ignores all spaces and new line ; the delimiter for the compiler is the semicolon • all statements ended by semicolon • Lower vs. upper case matters!! • Void is different than void • Main is different that main
  • 4. THE INFAMOUS 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> int main() { cout << “Hello world!”; return 0; }
  • 5. VARIABLE DECLARATION type variable-name; Meaning: variable <variable-name> will be a variable of type <type> Where type can be: • int //integer • double //real number • char //character Example: int a, b, c; double x; int sum; char my-character;
  • 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;
  • 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;
  • 8. IF STATEMENTS if (condition) { S1; } else { S2; } S3; condition S1 S2 S3 True False
  • 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
  • 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)) …
  • 11. IF EXAMPLE #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; }
  • 12. WHILE STATEMENTS while (condition) { S1; } S2; condition S1 S2 True False
  • 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; }
  • 14. 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”.