SlideShare a Scribd company logo
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

Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
WondimuBantihun1
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
Eric Chou
 
FUNDAMENTAL OF C
FUNDAMENTAL OF CFUNDAMENTAL OF C
FUNDAMENTAL OF C
KRUNAL RAVAL
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
MamataAnilgod
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
Thapar Institute
 
JAVA LESSON-01.pptx
JAVA LESSON-01.pptxJAVA LESSON-01.pptx
JAVA LESSON-01.pptx
StephenOczon1
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
Intro C# Book
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
marvellous2
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
FatimaZafar68
 
Basics of Python Programming
Basics of Python ProgrammingBasics of Python Programming
Basics of Python Programming
ManishJha237
 
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
joachimbenedicttulau
 
parts_of_python_programming_language.pptx
parts_of_python_programming_language.pptxparts_of_python_programming_language.pptx
parts_of_python_programming_language.pptx
Koteswari Kasireddy
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
sunila tharagaturi
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
Ralph Weber
 
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
Dr.Florence Dayana
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
AbdulRazaqAnjum
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
Connex
 
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 tutorial
SURBHI 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

Science Around Us Module 2 Matter Around Us
Science Around Us Module 2 Matter Around UsScience Around Us Module 2 Matter Around Us
Science Around Us Module 2 Matter Around Us
PennapaKeavsiri
 
2022 Vintage Roman Numerals Men Rings
2022 Vintage Roman  Numerals  Men  Rings2022 Vintage Roman  Numerals  Men  Rings
2022 Vintage Roman Numerals Men Rings
aragme
 
Cover Story - China's Investment Leader - Dr. Alyce SU
Cover Story - China's Investment Leader - Dr. Alyce SUCover Story - China's Investment Leader - Dr. Alyce SU
Cover Story - China's Investment Leader - Dr. Alyce SU
msthrill
 
GKohler - Retail Scavenger Hunt Presentation
GKohler - Retail Scavenger Hunt PresentationGKohler - Retail Scavenger Hunt Presentation
GKohler - Retail Scavenger Hunt Presentation
GraceKohler1
 
欧洲杯赌球-欧洲杯赌球买球官方官网-欧洲杯赌球比赛投注官网|【​网址​🎉ac55.net🎉​】
欧洲杯赌球-欧洲杯赌球买球官方官网-欧洲杯赌球比赛投注官网|【​网址​🎉ac55.net🎉​】欧洲杯赌球-欧洲杯赌球买球官方官网-欧洲杯赌球比赛投注官网|【​网址​🎉ac55.net🎉​】
欧洲杯赌球-欧洲杯赌球买球官方官网-欧洲杯赌球比赛投注官网|【​网址​🎉ac55.net🎉​】
valvereliz227
 
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
Lacey Max
 
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
Business storytelling: key ingredients to a story
Business storytelling: key ingredients to a storyBusiness storytelling: key ingredients to a story
Business storytelling: key ingredients to a story
Alexandra Fulford
 
Part 2 Deep Dive: Navigating the 2024 Slowdown
Part 2 Deep Dive: Navigating the 2024 SlowdownPart 2 Deep Dive: Navigating the 2024 Slowdown
Part 2 Deep Dive: Navigating the 2024 Slowdown
jeffkluth1
 
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
taqyea
 
Kirill Klip GEM Royalty TNR Gold Copper Presentation
Kirill Klip GEM Royalty TNR Gold Copper PresentationKirill Klip GEM Royalty TNR Gold Copper Presentation
Kirill Klip GEM Royalty TNR Gold Copper Presentation
Kirill Klip
 
Lundin Gold Corporate Presentation - June 2024
Lundin Gold Corporate Presentation - June 2024Lundin Gold Corporate Presentation - June 2024
Lundin Gold Corporate Presentation - June 2024
Adnet Communications
 
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
TIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup IndustryTIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup Industry
timesbpobusiness
 
CULR Spring 2024 Journal.pdf testing for duke
CULR Spring 2024 Journal.pdf testing for dukeCULR Spring 2024 Journal.pdf testing for duke
CULR Spring 2024 Journal.pdf testing for duke
ZevinAttisha
 
The APCO Geopolitical Radar - Q3 2024 The Global Operating Environment for Bu...
The APCO Geopolitical Radar - Q3 2024 The Global Operating Environment for Bu...The APCO Geopolitical Radar - Q3 2024 The Global Operating Environment for Bu...
The APCO Geopolitical Radar - Q3 2024 The Global Operating Environment for Bu...
APCO
 
Profiles of Iconic Fashion Personalities.pdf
Profiles of Iconic Fashion Personalities.pdfProfiles of Iconic Fashion Personalities.pdf
Profiles of Iconic Fashion Personalities.pdf
TTop Threads
 
Digital Transformation Frameworks: Driving Digital Excellence
Digital Transformation Frameworks: Driving Digital ExcellenceDigital Transformation Frameworks: Driving Digital Excellence
Digital Transformation Frameworks: Driving Digital Excellence
Operational Excellence Consulting
 
Zodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
Zodiac Signs and Food Preferences_ What Your Sign Says About Your TasteZodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
Zodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
my Pandit
 

Recently uploaded (20)

Science Around Us Module 2 Matter Around Us
Science Around Us Module 2 Matter Around UsScience Around Us Module 2 Matter Around Us
Science Around Us Module 2 Matter Around Us
 
2022 Vintage Roman Numerals Men Rings
2022 Vintage Roman  Numerals  Men  Rings2022 Vintage Roman  Numerals  Men  Rings
2022 Vintage Roman Numerals Men Rings
 
Cover Story - China's Investment Leader - Dr. Alyce SU
Cover Story - China's Investment Leader - Dr. Alyce SUCover Story - China's Investment Leader - Dr. Alyce SU
Cover Story - China's Investment Leader - Dr. Alyce SU
 
GKohler - Retail Scavenger Hunt Presentation
GKohler - Retail Scavenger Hunt PresentationGKohler - Retail Scavenger Hunt Presentation
GKohler - Retail Scavenger Hunt Presentation
 
欧洲杯赌球-欧洲杯赌球买球官方官网-欧洲杯赌球比赛投注官网|【​网址​🎉ac55.net🎉​】
欧洲杯赌球-欧洲杯赌球买球官方官网-欧洲杯赌球比赛投注官网|【​网址​🎉ac55.net🎉​】欧洲杯赌球-欧洲杯赌球买球官方官网-欧洲杯赌球比赛投注官网|【​网址​🎉ac55.net🎉​】
欧洲杯赌球-欧洲杯赌球买球官方官网-欧洲杯赌球比赛投注官网|【​网址​🎉ac55.net🎉​】
 
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
 
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
 
Business storytelling: key ingredients to a story
Business storytelling: key ingredients to a storyBusiness storytelling: key ingredients to a story
Business storytelling: key ingredients to a story
 
Part 2 Deep Dive: Navigating the 2024 Slowdown
Part 2 Deep Dive: Navigating the 2024 SlowdownPart 2 Deep Dive: Navigating the 2024 Slowdown
Part 2 Deep Dive: Navigating the 2024 Slowdown
 
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
 
Kirill Klip GEM Royalty TNR Gold Copper Presentation
Kirill Klip GEM Royalty TNR Gold Copper PresentationKirill Klip GEM Royalty TNR Gold Copper Presentation
Kirill Klip GEM Royalty TNR Gold Copper Presentation
 
Lundin Gold Corporate Presentation - June 2024
Lundin Gold Corporate Presentation - June 2024Lundin Gold Corporate Presentation - June 2024
Lundin Gold Corporate Presentation - June 2024
 
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
 
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
Dpboss Matka Guessing Satta Matta Matka Kalyan panel Chart Indian Matka Dpbos...
 
TIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup IndustryTIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup Industry
 
CULR Spring 2024 Journal.pdf testing for duke
CULR Spring 2024 Journal.pdf testing for dukeCULR Spring 2024 Journal.pdf testing for duke
CULR Spring 2024 Journal.pdf testing for duke
 
The APCO Geopolitical Radar - Q3 2024 The Global Operating Environment for Bu...
The APCO Geopolitical Radar - Q3 2024 The Global Operating Environment for Bu...The APCO Geopolitical Radar - Q3 2024 The Global Operating Environment for Bu...
The APCO Geopolitical Radar - Q3 2024 The Global Operating Environment for Bu...
 
Profiles of Iconic Fashion Personalities.pdf
Profiles of Iconic Fashion Personalities.pdfProfiles of Iconic Fashion Personalities.pdf
Profiles of Iconic Fashion Personalities.pdf
 
Digital Transformation Frameworks: Driving Digital Excellence
Digital Transformation Frameworks: Driving Digital ExcellenceDigital Transformation Frameworks: Driving Digital Excellence
Digital Transformation Frameworks: Driving Digital Excellence
 
Zodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
Zodiac Signs and Food Preferences_ What Your Sign Says About Your TasteZodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
Zodiac Signs and Food Preferences_ What Your Sign Says About Your Taste
 

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)