SlideShare une entreprise Scribd logo
1  sur  12
C++ Basics
Variables, Identifiers,
Assignments, Input/Output
Variables
variable can hold a number or a data of other types, it always holds
something. A variable has a name
the data held in variable is called value
variables are implemented as memory locations and assigned
certain memory address. The exact address depends on computer
and compiler.
we think as though the memory locations are actually labeled with
variable names
12.5
32
'c'
y
Temperature
Letter
1001
1002
1003
1004
1005
1006
1007
-Number 1008
1009
2
Identifiers
identifier – name of a variable or any other named construct
identifier must start with a letter or underscore symbol (_), the rest of the
characters should be letters, digits or underscores
the following are legal identifiers:
x x1 x_1 _abc sum RateAveragE
the following are not legal identifiers. Why?
13 3X %change data-1 my.identifier a(3)
C++ is case sensitive:
MyVar and myvar are different identifiers
3
Identifier Style
careful selection of identifiers makes your program clearer
identifiers should be
short enough to be reasonable to type (single word is norm)
– standard abbreviations are acceptable
long enough to be understandable
two styles of identifiers
C-style - terse, use abbreviations and underscores to separate the words,
never use capital letters for variables
Camel Case - if multiple words: capitalize, do not use underscores
– variant: first letter lowercased
pick style and use consistently
ex: Camel Case 1 C-style Camel Case 2
Min min min
Temperature temperature temperature
CameraAngle camera_angle cameraAngle
CurrentNumberPoints cur_point_nmbr currentNumberPoints
4
Keywords
keywords are identifiers reserved as part of the language
int, return, float, double
cannot be used by the programmer to name things
consist of lowercase letters only
have special meaning to the compiler
5
Keywords (cont.)
asm do if return typedef
auto double inline short typeid
bool dynamic_cast int signed typename
break delete long sizeof union
case else mutable static unsigned
catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned
6
Variable Declarations
before use, every variable in C++ program needs to be declared
type – the kind of data stored in a variable
declaration – introduces a variable to a compiler
a variable declaration specifies
type
name
declaration syntax:
two commonly used numeric types are:
int - whole positive or negative numbers:
1,2, -1,0,-288, etc.
double - positive or negative numbers with fractional part:
1.75, -0.55
example declarations:
int numberOfBars;
double weight, totalWeight;
type id, id, ..., id;
list of one or
more identifiers
7
Declaration Location,
Initial Value
a variable should be declared as close to its use as possible
variable contains a value after it is declared
until assigned, this value is arbitrary
8
Assignment
assignment statement is an order to the computer to set the value of the
variable on the left hand side of equal sign to what is written on the right
hand side
it looks like a math equation, but it is not
example:
numberOfBars = 37;
totalWeight = oneWeight;
totalWeight = oneWeight * numberOfBars;
numberOfBars = numberOfBars + 3;
variable = value;
9
Output
to do input/output, at the beginning of your program insert
#include <iostream>
using std::cout; using std::endl;
C++ uses streams for input an output
stream – a sequence of data to be read (input stream) or a sequence of data
generated by the program to be output (output stream)
variable values as well as strings of text can be output to the screen using cout
(console output):
cout << numberOfBars;
cout << ”candy bars”;
cout << endl;
<< is called insertion operator, it inserts data into the output stream, anything within
double quotes will be output literally (without changes) - ”candy bars taste
good”
note the space before letter “ c” - the computer does not insert space on its own
keyword endl tells the computer to start the output from the next line
10
More Output
several insertion operators can be stacked together in a single
statement:
cout << numberOfBars << ”candy barsn”;
symbol n at the end of the string serves the same purpose as endl
arithmetic expressions can be used with the output statement:
cout << ”The total cost is $” << (price + tax);
11
Input
cin - (stands for Console INput) – stream used to give variables user-input values
need to add the following to the beginning of your program
using std::cin;
when the program reaches the input statement it just pauses until the user types
something and presses <Enter> key
therefore, it is beneficial to precede the input statement with some explanatory output
called prompt:
cout << ”Enter the number of candy bars”;
cout << ”and weight in ounces.n”;
cout << ”then press returnn”;
cin >> numberOfBars >> oneWeight;
>> is extraction operator
dialog – collection of program prompts and user responses
input operator (similar to output operator) can be stacked
input token – sequence of characters separated by white space (spaces, tabs,
newlines)
the values typed are inserted into variables when <Enter> is pressed, if more values
needed - program waits, if extra typed - they are used in next input statements if
needed
12

Contenu connexe

Tendances (20)

Enums in c
Enums in cEnums in c
Enums in c
 
Function in c
Function in cFunction in c
Function in c
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Strings
StringsStrings
Strings
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
While loop
While loopWhile loop
While loop
 
Control structure
Control structureControl structure
Control structure
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
user defined function
user defined functionuser defined function
user defined function
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
C functions
C functionsC functions
C functions
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 

Similaire à Declaration of variables

02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic Manzoor ALam
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptDeepak Singh
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docxPinkiVats1
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++Rokonuzzaman Rony
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxjaggernaoma
 
Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions khawajasharif
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingSherwin Banaag Sapin
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2Don Dooley
 

Similaire à Declaration of variables (20)

keyword
keywordkeyword
keyword
 
keyword
keywordkeyword
keyword
 
L03vars
L03varsL03vars
L03vars
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Pengaturcaraan asas
Pengaturcaraan asasPengaturcaraan asas
Pengaturcaraan asas
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
 
Chapter2
Chapter2Chapter2
Chapter2
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docx
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Computer Programming- Lecture 3
Computer Programming- Lecture 3Computer Programming- Lecture 3
Computer Programming- Lecture 3
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
 
Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
PSPC--UNIT-2.pdf
PSPC--UNIT-2.pdfPSPC--UNIT-2.pdf
PSPC--UNIT-2.pdf
 

Plus de Maria Stella Solon (9)

Webpage design lesson3
Webpage design lesson3Webpage design lesson3
Webpage design lesson3
 
Webdesign
WebdesignWebdesign
Webdesign
 
Basic web dev
Basic web devBasic web dev
Basic web dev
 
MIS Support to Management
MIS Support to ManagementMIS Support to Management
MIS Support to Management
 
Management Information System
Management Information SystemManagement Information System
Management Information System
 
Professional Ethics of IT
Professional Ethics of ITProfessional Ethics of IT
Professional Ethics of IT
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Dbms
DbmsDbms
Dbms
 
Basic c++
Basic c++Basic c++
Basic c++
 

Dernier

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
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.pptxDenish Jangid
 
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.pptxheathfieldcps1
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
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 ClassroomPooky Knightsmith
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Dernier (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
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
 
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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
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
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Declaration of variables

  • 2. Variables variable can hold a number or a data of other types, it always holds something. A variable has a name the data held in variable is called value variables are implemented as memory locations and assigned certain memory address. The exact address depends on computer and compiler. we think as though the memory locations are actually labeled with variable names 12.5 32 'c' y Temperature Letter 1001 1002 1003 1004 1005 1006 1007 -Number 1008 1009 2
  • 3. Identifiers identifier – name of a variable or any other named construct identifier must start with a letter or underscore symbol (_), the rest of the characters should be letters, digits or underscores the following are legal identifiers: x x1 x_1 _abc sum RateAveragE the following are not legal identifiers. Why? 13 3X %change data-1 my.identifier a(3) C++ is case sensitive: MyVar and myvar are different identifiers 3
  • 4. Identifier Style careful selection of identifiers makes your program clearer identifiers should be short enough to be reasonable to type (single word is norm) – standard abbreviations are acceptable long enough to be understandable two styles of identifiers C-style - terse, use abbreviations and underscores to separate the words, never use capital letters for variables Camel Case - if multiple words: capitalize, do not use underscores – variant: first letter lowercased pick style and use consistently ex: Camel Case 1 C-style Camel Case 2 Min min min Temperature temperature temperature CameraAngle camera_angle cameraAngle CurrentNumberPoints cur_point_nmbr currentNumberPoints 4
  • 5. Keywords keywords are identifiers reserved as part of the language int, return, float, double cannot be used by the programmer to name things consist of lowercase letters only have special meaning to the compiler 5
  • 6. Keywords (cont.) asm do if return typedef auto double inline short typeid bool dynamic_cast int signed typename break delete long sizeof union case else mutable static unsigned catch enum namespace static_cast using char explicit new struct virtual class extern operator switch void const false private template volatile const_cast float protected this wchar_t continue for public throw while default friend register true union delete goto reinterpret_cast try unsigned 6
  • 7. Variable Declarations before use, every variable in C++ program needs to be declared type – the kind of data stored in a variable declaration – introduces a variable to a compiler a variable declaration specifies type name declaration syntax: two commonly used numeric types are: int - whole positive or negative numbers: 1,2, -1,0,-288, etc. double - positive or negative numbers with fractional part: 1.75, -0.55 example declarations: int numberOfBars; double weight, totalWeight; type id, id, ..., id; list of one or more identifiers 7
  • 8. Declaration Location, Initial Value a variable should be declared as close to its use as possible variable contains a value after it is declared until assigned, this value is arbitrary 8
  • 9. Assignment assignment statement is an order to the computer to set the value of the variable on the left hand side of equal sign to what is written on the right hand side it looks like a math equation, but it is not example: numberOfBars = 37; totalWeight = oneWeight; totalWeight = oneWeight * numberOfBars; numberOfBars = numberOfBars + 3; variable = value; 9
  • 10. Output to do input/output, at the beginning of your program insert #include <iostream> using std::cout; using std::endl; C++ uses streams for input an output stream – a sequence of data to be read (input stream) or a sequence of data generated by the program to be output (output stream) variable values as well as strings of text can be output to the screen using cout (console output): cout << numberOfBars; cout << ”candy bars”; cout << endl; << is called insertion operator, it inserts data into the output stream, anything within double quotes will be output literally (without changes) - ”candy bars taste good” note the space before letter “ c” - the computer does not insert space on its own keyword endl tells the computer to start the output from the next line 10
  • 11. More Output several insertion operators can be stacked together in a single statement: cout << numberOfBars << ”candy barsn”; symbol n at the end of the string serves the same purpose as endl arithmetic expressions can be used with the output statement: cout << ”The total cost is $” << (price + tax); 11
  • 12. Input cin - (stands for Console INput) – stream used to give variables user-input values need to add the following to the beginning of your program using std::cin; when the program reaches the input statement it just pauses until the user types something and presses <Enter> key therefore, it is beneficial to precede the input statement with some explanatory output called prompt: cout << ”Enter the number of candy bars”; cout << ”and weight in ounces.n”; cout << ”then press returnn”; cin >> numberOfBars >> oneWeight; >> is extraction operator dialog – collection of program prompts and user responses input operator (similar to output operator) can be stacked input token – sequence of characters separated by white space (spaces, tabs, newlines) the values typed are inserted into variables when <Enter> is pressed, if more values needed - program waits, if extra typed - they are used in next input statements if needed 12

Notes de l'éditeur

  1. Alt+8 to dsiplay in MSVS executable code