SlideShare a Scribd company logo
1 of 22
Lecture # 2
• Instructor: Mujtaba Shahani
C++ Comments
• Comments can be used to explain C++ code, and to
make it more readable. It can also be used to prevent
execution when testing alternative code. Comments can
be singled-lined or multi-lined.
• Single-line Comments
• Single-line comments start with two forward slashes (//).
• Any text between // and the end of the line is ignored by
the compiler (will not be executed).
• This example uses a single-line comment before a line of
code:
Single-line Comments
• #include <iostream>
• using namespace std;
• int main() {
• // This is a comment
• cout << "Hello World!";
• return 0;
• }
C++ Multi-line Comments
• Multi-line comments start with /* and ends with */.
• Any text between /* and */ will be ignored by the compiler:
• #include <iostream>
• using namespace std;
• int main() {
• /* The code below will print the words Hello World!
• to the screen, and it is amazing */
• cout << "Hello World!";
• return 0;
• }
Basic Data Types
Data Type Size Description
boolean 1 byte Stores true or false values
char 1 byte
Stores a single character/letter/number, or ASCII
values
int
2 or 4
bytes
Stores whole numbers, without decimals
float 4 bytes
Stores fractional numbers, containing one or more
decimals. Sufficient for storing 6-7 decimal digits
double 8 bytes
Stores fractional numbers, containing one or more
decimals. Sufficient for storing 15 decimal digits
The data type specifies the size and type of information the variable will store:
C++ Variables
• ariables are containers for storing data values.
• In C++, there are different types of variables (defined with
different keywords), for example:
• int - stores integers (whole numbers), without decimals,
such as 123 or -123
• double - stores floating point numbers, with decimals,
such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char
values are surrounded by single quotes
• string - stores text, such as "Hello World". String values
are surrounded by double quotes
• bool - stores values with two states: true or false
Declaring (Creating) Variables
• To create a variable, specify the type and assign it a value:
• Syntax
• type variableName = value;
• Where type is one of C++ types (such as int), and
variableName is the name of the variable (such as x or
myName). The equal sign is used to assign values to the
variable.
• To create a variable that should store a number, look at the
following example:
• Example
• Create a variable called myNum of type int and assign it the
value 15:
• int myNum = 15;
cout << myNum;
Display Variables, Add Variables
Together
• The cout object is used together with the << operator to display
variables.
• To combine both text and a variable, separate them with the <<
operator:
• Example
• int myAge = 35;
cout << "I am " << myAge << " years old.";
• To add a variable to another variable, you can use the +
operator:
• Example
• int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
Declare Many Variables
• To declare more than one variable of the same type, use
a comma-separated list:
• Example
• int x = 5, y = 6, z = 50;
cout << x + y + z;
• One Value to Multiple Variables
• You can also assign the same value to multiple variables
in one line:
• Example
• int x, y, z;
x = y = z = 50;
cout << x + y + z;
C++ Identifiers
• All C++ variables must be identified with unique names.
• These unique names are called identifiers.
• Identifiers can be short names (like x and y) or more
descriptive names (age, sum, totalVolume).
• Note: It is recommended to use descriptive names in
order to create understandable and maintainable code:
• Example
• // Good
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;
The general rules for naming variables
are:
• Names can contain letters, digits and underscores
• Names must begin with a letter or an underscore (_)
• Names are case sensitive (myVar and myvar are different
variables)
• Names cannot contain whitespaces or special characters
like !, #, %, etc.
• Reserved words (like C++ keywords, such as int) cannot
be used as names
Constants
• When you do not want others (or yourself) to change
existing variable values, use the const keyword (this will
declare the variable as "constant", which means
unchangeable and read-only):
• Example
• const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable
'myNum'
• You should always declare the variable as constant when
you have values that are unlikely to change:
• Example
• const int minutesPerHour = 60;
const float PI = 3.14;
C++ User Input
• You have already learned that cout is used to output
(print) values. Now we will use cin to get user input.
• cin is a predefined variable that reads data from the
keyboard with the extraction operator (>>).
• In the following example, the user can input a number,
which is stored in the variable x. Then we print the value
of x:
Example
int x;
cout << "Type a number: "; // Type a number and press
enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
C++ Operators
• Operators are used to perform operations on variables
and values.
• In the example below, we use the + operator to add
together two values:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
Arithmetic Operators
Operator Name Description Example
+ Addition
Adds together two
values
x + y
- Subtraction
Subtracts one value
from another
x - y
*
Multiplicatio
n
Multiplies two values x * y
/ Division
Divides one value by
another
x / y
% Modulus
Returns the division
remainder
x % y
++ Increment
Increases the value of
a variable by 1
++x
-- Decrement
Decreases the value of
a variable by 1
--x
Assignment Operators
• Assignment operators are used to assign values to
variables.
• In the example below, we use the assignment operator
(=) to assign the value 10 to a variable called x:
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
Comparison Operators
• Comparison operators are used to compare two values
(or variables). This is important in programming, because
it helps us to find answers and make decisions.
• The return value of a comparison is either 1 or 0, which
means true (1) or false (0). These values are known as
Boolean values, and you will learn more about them in
the Booleans and If..Else chapter.
A list of all comparison operators:
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Logical Operators
• As with comparison operators, you can also test for true
(1) or false (0) values with logical operators.
• Logical operators are used to determine the logic between
variables or values:
Logical Operators
Operator Name Description Example
&& Logical and
Returns true if both
statements are true
x < 5 && x <
10
|| Logical or
Returns true if one of
the statements is true
x < 5 || x < 4
! Logical not
Reverse the result,
returns false if the
result is true
!(x < 5 && x <
10)

More Related Content

Similar to Programming fundamental 02.pptx

Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxMamataAnilgod
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and VariablesIntro C# Book
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.pptFatimaZafar68
 
Basics of Python Programming
Basics of Python ProgrammingBasics of Python Programming
Basics of Python ProgrammingManishJha237
 
LESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxLESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxjoachimbenedicttulau
 
parts_of_python_programming_language.pptx
parts_of_python_programming_language.pptxparts_of_python_programming_language.pptx
parts_of_python_programming_language.pptxKoteswari Kasireddy
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageDr.Florence Dayana
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorialSURBHI SAROHA
 

Similar to Programming fundamental 02.pptx (20)

Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
FUNDAMENTAL OF C
FUNDAMENTAL OF CFUNDAMENTAL OF C
FUNDAMENTAL OF C
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 
JAVA LESSON-01.pptx
JAVA LESSON-01.pptxJAVA LESSON-01.pptx
JAVA LESSON-01.pptx
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
Basics of Python Programming
Basics of Python ProgrammingBasics of Python Programming
Basics of Python Programming
 
LESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxLESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptx
 
parts_of_python_programming_language.pptx
parts_of_python_programming_language.pptxparts_of_python_programming_language.pptx
parts_of_python_programming_language.pptx
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 

Recently uploaded

Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...lizamodels9
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Timedelhimodelshub1
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
Tech Startup Growth Hacking 101  - Basics on Growth MarketingTech Startup Growth Hacking 101  - Basics on Growth Marketing
Tech Startup Growth Hacking 101 - Basics on Growth MarketingShawn Pang
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024christinemoorman
 
Sales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessSales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessAggregage
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Servicediscovermytutordmt
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewasmakika9823
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...lizamodels9
 
RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechNewman George Leech
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...anilsa9823
 
Progress Report - Oracle Database Analyst Summit
Progress  Report - Oracle Database Analyst SummitProgress  Report - Oracle Database Analyst Summit
Progress Report - Oracle Database Analyst SummitHolger Mueller
 
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptx
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptxBanana Powder Manufacturing Plant Project Report 2024 Edition.pptx
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptxgeorgebrinton95
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.Aaiza Hassan
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...lizamodels9
 
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service PuneVIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service PuneCall girls in Ahmedabad High profile
 
(8264348440) 🔝 Call Girls In Mahipalpur 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Mahipalpur 🔝 Delhi NCR(8264348440) 🔝 Call Girls In Mahipalpur 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Mahipalpur 🔝 Delhi NCRsoniya singh
 

Recently uploaded (20)

Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Time
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
Tech Startup Growth Hacking 101  - Basics on Growth MarketingTech Startup Growth Hacking 101  - Basics on Growth Marketing
Tech Startup Growth Hacking 101 - Basics on Growth Marketing
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024
 
Sales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for SuccessSales & Marketing Alignment: How to Synergize for Success
Sales & Marketing Alignment: How to Synergize for Success
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Service
 
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service DewasVip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
 
RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman Leech
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
 
Progress Report - Oracle Database Analyst Summit
Progress  Report - Oracle Database Analyst SummitProgress  Report - Oracle Database Analyst Summit
Progress Report - Oracle Database Analyst Summit
 
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Mehrauli Delhi 💯Call Us 🔝8264348440🔝
 
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptx
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptxBanana Powder Manufacturing Plant Project Report 2024 Edition.pptx
Banana Powder Manufacturing Plant Project Report 2024 Edition.pptx
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
M.C Lodges -- Guest House in Jhang.
M.C Lodges --  Guest House in Jhang.M.C Lodges --  Guest House in Jhang.
M.C Lodges -- Guest House in Jhang.
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
 
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service PuneVIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Kirti 8617697112 Independent Escort Service Pune
 
(8264348440) 🔝 Call Girls In Mahipalpur 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Mahipalpur 🔝 Delhi NCR(8264348440) 🔝 Call Girls In Mahipalpur 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Mahipalpur 🔝 Delhi NCR
 

Programming fundamental 02.pptx

  • 1.
  • 2. Lecture # 2 • Instructor: Mujtaba Shahani
  • 3. C++ Comments • Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined. • Single-line Comments • Single-line comments start with two forward slashes (//). • Any text between // and the end of the line is ignored by the compiler (will not be executed). • This example uses a single-line comment before a line of code:
  • 4. Single-line Comments • #include <iostream> • using namespace std; • int main() { • // This is a comment • cout << "Hello World!"; • return 0; • }
  • 5. C++ Multi-line Comments • Multi-line comments start with /* and ends with */. • Any text between /* and */ will be ignored by the compiler: • #include <iostream> • using namespace std; • int main() { • /* The code below will print the words Hello World! • to the screen, and it is amazing */ • cout << "Hello World!"; • return 0; • }
  • 6. Basic Data Types Data Type Size Description boolean 1 byte Stores true or false values char 1 byte Stores a single character/letter/number, or ASCII values int 2 or 4 bytes Stores whole numbers, without decimals float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits The data type specifies the size and type of information the variable will store:
  • 7. C++ Variables • ariables are containers for storing data values. • In C++, there are different types of variables (defined with different keywords), for example: • int - stores integers (whole numbers), without decimals, such as 123 or -123 • double - stores floating point numbers, with decimals, such as 19.99 or -19.99 • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes • string - stores text, such as "Hello World". String values are surrounded by double quotes • bool - stores values with two states: true or false
  • 8. Declaring (Creating) Variables • To create a variable, specify the type and assign it a value: • Syntax • type variableName = value; • Where type is one of C++ types (such as int), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign values to the variable. • To create a variable that should store a number, look at the following example: • Example • Create a variable called myNum of type int and assign it the value 15: • int myNum = 15; cout << myNum;
  • 9. Display Variables, Add Variables Together • The cout object is used together with the << operator to display variables. • To combine both text and a variable, separate them with the << operator: • Example • int myAge = 35; cout << "I am " << myAge << " years old."; • To add a variable to another variable, you can use the + operator: • Example • int x = 5; int y = 6; int sum = x + y; cout << sum;
  • 10. Declare Many Variables • To declare more than one variable of the same type, use a comma-separated list: • Example • int x = 5, y = 6, z = 50; cout << x + y + z; • One Value to Multiple Variables • You can also assign the same value to multiple variables in one line: • Example • int x, y, z; x = y = z = 50; cout << x + y + z;
  • 11. C++ Identifiers • All C++ variables must be identified with unique names. • These unique names are called identifiers. • Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). • Note: It is recommended to use descriptive names in order to create understandable and maintainable code: • Example • // Good int minutesPerHour = 60; // OK, but not so easy to understand what m actually is int m = 60;
  • 12. The general rules for naming variables are: • Names can contain letters, digits and underscores • Names must begin with a letter or an underscore (_) • Names are case sensitive (myVar and myvar are different variables) • Names cannot contain whitespaces or special characters like !, #, %, etc. • Reserved words (like C++ keywords, such as int) cannot be used as names
  • 13. Constants • When you do not want others (or yourself) to change existing variable values, use the const keyword (this will declare the variable as "constant", which means unchangeable and read-only): • Example • const int myNum = 15; // myNum will always be 15 myNum = 10; // error: assignment of read-only variable 'myNum' • You should always declare the variable as constant when you have values that are unlikely to change: • Example • const int minutesPerHour = 60; const float PI = 3.14;
  • 14. C++ User Input • You have already learned that cout is used to output (print) values. Now we will use cin to get user input. • cin is a predefined variable that reads data from the keyboard with the extraction operator (>>). • In the following example, the user can input a number, which is stored in the variable x. Then we print the value of x:
  • 15. Example int x; cout << "Type a number: "; // Type a number and press enter cin >> x; // Get user input from the keyboard cout << "Your number is: " << x; // Display the input value
  • 16. C++ Operators • Operators are used to perform operations on variables and values. • In the example below, we use the + operator to add together two values: • Arithmetic operators • Assignment operators • Comparison operators • Logical operators
  • 17. Arithmetic Operators Operator Name Description Example + Addition Adds together two values x + y - Subtraction Subtracts one value from another x - y * Multiplicatio n Multiplies two values x * y / Division Divides one value by another x / y % Modulus Returns the division remainder x % y ++ Increment Increases the value of a variable by 1 ++x -- Decrement Decreases the value of a variable by 1 --x
  • 18. Assignment Operators • Assignment operators are used to assign values to variables. • In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x: Operator Example Same As = x = 5 x = 5 += x += 3 x = x + 3 -= x -= 3 x = x - 3 *= x *= 3 x = x * 3 /= x /= 3 x = x / 3 %= x %= 3 x = x % 3
  • 19. Comparison Operators • Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions. • The return value of a comparison is either 1 or 0, which means true (1) or false (0). These values are known as Boolean values, and you will learn more about them in the Booleans and If..Else chapter.
  • 20. A list of all comparison operators: Operator Name Example == Equal to x == y != Not equal x != y > Greater than x > y < Less than x < y >= Greater than or equal to x >= y <= Less than or equal to x <= y
  • 21. Logical Operators • As with comparison operators, you can also test for true (1) or false (0) values with logical operators. • Logical operators are used to determine the logic between variables or values:
  • 22. Logical Operators Operator Name Description Example && Logical and Returns true if both statements are true x < 5 && x < 10 || Logical or Returns true if one of the statements is true x < 5 || x < 4 ! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)