3
Most read
6
Most read
7
Most read
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

More Related Content

PPSX
C++ Programming Language
PPTX
Presentation on C++ Programming Language
PPT
friend function(c++)
PPTX
Operators and expressions in c language
PDF
Introduction to c++ ppt 1
PPTX
Handling of character strings C programming
PPT
Strings
C++ Programming Language
Presentation on C++ Programming Language
friend function(c++)
Operators and expressions in c language
Introduction to c++ ppt 1
Handling of character strings C programming
Strings

What's hot (20)

PPTX
The string class
PPTX
Pointers in c++
PPT
Basics of c++
PPT
Input and output in C++
PPTX
Python-Functions.pptx
PPTX
Strings in C
PPTX
Passing an Array to a Function (ICT Programming)
PPT
Operator Overloading
PPTX
Variables in C++, data types in c++
PPT
Constants in C Programming
PPTX
Parameter passing to_functions_in_c
PPTX
Chapter 05 classes and objects
PPTX
Unit 2. Elements of C
PPTX
Decision making and branching in c programming
PPTX
Operators and expressions
PPTX
Pass by value and pass by reference
PPT
Basics of c++ Programming Language
PPTX
Recursion in c++
PPTX
Polymorphism In c++
PPTX
Functions in c++
The string class
Pointers in c++
Basics of c++
Input and output in C++
Python-Functions.pptx
Strings in C
Passing an Array to a Function (ICT Programming)
Operator Overloading
Variables in C++, data types in c++
Constants in C Programming
Parameter passing to_functions_in_c
Chapter 05 classes and objects
Unit 2. Elements of C
Decision making and branching in c programming
Operators and expressions
Pass by value and pass by reference
Basics of c++ Programming Language
Recursion in c++
Polymorphism In c++
Functions in c++
Ad

Similar to Declaration of variables (20)

PPT
keyword
PPT
keyword
PPT
02a fundamental c++ types, arithmetic
PPT
C++ programming
PPT
CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt
PPT
intro to programming languge c++ for computer department
PPT
Pengaturcaraan asas
PDF
Chapter 3 - Variable Memory Concept
PPT
Chapter2
PPT
Elementary_Of_C++_Programming_Language.ppt
DOCX
c++ best code.docxhsdsdcvcdcdvdvdvdvdcdv
PPTX
Lesson 4 Basic Programming Constructs.pptx
PPTX
Introduction%20C.pptx
DOCX
C++ Tutorial.docx
PPTX
Object Oriented Programming with C++
PPT
Computer Programming- Lecture 3
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
PPTX
C++ lecture 01
PDF
Week 02_Development Environment of C++.pdf
keyword
keyword
02a fundamental c++ types, arithmetic
C++ programming
CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt
intro to programming languge c++ for computer department
Pengaturcaraan asas
Chapter 3 - Variable Memory Concept
Chapter2
Elementary_Of_C++_Programming_Language.ppt
c++ best code.docxhsdsdcvcdcdvdvdvdvdcdv
Lesson 4 Basic Programming Constructs.pptx
Introduction%20C.pptx
C++ Tutorial.docx
Object Oriented Programming with C++
Computer Programming- Lecture 3
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
C++ lecture 01
Week 02_Development Environment of C++.pdf
Ad

More from Maria Stella Solon (9)

PPT
Webpage design lesson3
PPT
PPT
Basic web dev
PPT
MIS Support to Management
PPTX
Management Information System
PPT
Professional Ethics of IT
PPTX
Database Management System
PPT
Webpage design lesson3
Basic web dev
MIS Support to Management
Management Information System
Professional Ethics of IT
Database Management System

Recently uploaded (20)

PPTX
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
PDF
GSA-Past-Papers-2010-2024-2.pdf CSS examination
PPTX
MMW-CHAPTER-1-final.pptx major Elementary Education
PDF
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
PDF
anganwadi services for the b.sc nursing and GNM
PPTX
Key-Features-of-the-SHS-Program-v4-Slides (3) PPT2.pptx
PPTX
GW4 BioMed Candidate Support Webinar 2025
PDF
FAMILY PLANNING (preventative and social medicine pdf)
PDF
Kalaari-SaaS-Founder-Playbook-2024-Edition-.pdf
PDF
Laparoscopic Imaging Systems at World Laparoscopy Hospital
PPTX
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
PPTX
Ppt obs emergecy.pptxydirnbduejguxjjdjidjdbuc
PPTX
UCSP Section A - Human Cultural Variations,Social Differences,social ChangeCo...
PPTX
Theoretical for class.pptxgshdhddhdhdhgd
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
PDF
V02-Session-4-Leadership-Through-Assessment-MLB.pdf
PPTX
Environmental Sciences and Sustainability Chapter 2
PDF
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
PDF
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
PDF
Unleashing the Potential of the Cultural and creative industries
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
GSA-Past-Papers-2010-2024-2.pdf CSS examination
MMW-CHAPTER-1-final.pptx major Elementary Education
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
anganwadi services for the b.sc nursing and GNM
Key-Features-of-the-SHS-Program-v4-Slides (3) PPT2.pptx
GW4 BioMed Candidate Support Webinar 2025
FAMILY PLANNING (preventative and social medicine pdf)
Kalaari-SaaS-Founder-Playbook-2024-Edition-.pdf
Laparoscopic Imaging Systems at World Laparoscopy Hospital
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
Ppt obs emergecy.pptxydirnbduejguxjjdjidjdbuc
UCSP Section A - Human Cultural Variations,Social Differences,social ChangeCo...
Theoretical for class.pptxgshdhddhdhdhgd
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
V02-Session-4-Leadership-Through-Assessment-MLB.pdf
Environmental Sciences and Sustainability Chapter 2
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
Unleashing the Potential of the Cultural and creative industries

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

Editor's Notes

  • #10: Alt+8 to dsiplay in MSVS executable code