SlideShare a Scribd company logo
Programming Fundamentals
Chapter # 2
C++ Basics
Lecture # 2
Course Instructor: Fadia Ali
Outline
Variables
Names:
Identifiers
Variable
declarations
Syntax Assignment
statements
Uninitialized
variables
Output using
cout
Input using cin
Escape
sequences
Data types Type char Type bool
Class string
Arithmetic
operators and
expressions
Comment
Naming
constants
Variables
• Programmers use programming constructs known as variables to name and
store data.
• A C++ variable can hold a number or data of other types. Data held in a
variable is called its value;
• Number/data held by a C++ variable can be changed.
• A C++ variable is guaranteed to have some value in it, if only a garbage
number left in the computer’s memory by some previously run program.
• In programming languages, variables are implemented as memory
locations. The compiler assigns a memory location to each variable name in
the program. The value of the variable, in a coded form consisting of 0s and
1s, is kept in the memory location assigned to that variable.
• We do not know what addresses the compiler will choose for the variables in
our program.
Names: Identifiers
• Identifiers are used as names for variables and other items in a C++ program.
• To make your program easy to understand, you should always use meaningful
names for variables.
• An identifier must start with either a letter or the underscore symbol, and all the
rest of the characters must be letters, digits, or the underscore symbol.
• C++ is a case-sensitive language; that is, it distinguishes between uppercase
and lowercase letters in the spelling of identifiers.
• A C++ identifier can be of any length.
• There is a special class of identifiers, called keywords or reserved words, that
have a predefined meaning in C++ and that you cannot use as names for
variables or anything else.
• Examples:
• A, a_1, x123 (legal)
• 1ab, da%, 1-2, (not acceptable)
• Test, test, TEST (case-sensitive)
Variable declarations
• Every variable in a C++ program must be declared before the
variable can be used.
• When you declare a variable, you are telling the compiler—and,
ultimately, the computer—what kind of data you will be storing in the
variable, what size of memory location to use for the variable and
which code to use when representing the variable’s value as a string
of 0s and 1s.
• Each declaration ends with a semicolon (;).
• When there is more than one variable in a declaration, the variables
are separated by commas.
• The kind of data that is held in a variable is called its type and the
name for the type, such as int or double, is called a type name.
Syntax
• The syntax for a programming languages is the set of grammar rules for that
language.
• The syntax for variable declarations is as follows:
• Syntax
• Type_Name Variable_Name_1, Variable_Name_2, ...;
• Examples
• int count, sum, number_of_person;
• double distance;
Assignment Statements
• In an assignment statement, first the expression on the right-hand side of the
equal sign is evaluated, and then the variable on the left-hand side of the equal
sign is set equal to this value.
• In an assignment statement, the expression on the right-hand side of the equal
sign can simply be another variable or a constant.
• Syntax
• Variable = Expression;
• Examples
• sum=a; //variable
• distance = rate * time; //expression
• count=12; //constant
Uninitialized variables
• Variable that has not been given a value is said to be uninitialized.
• The value of an uninitialized variable is determined by whatever pattern of 0s and 1s
was left in its memory location by the last program that used that portion of memory.
• One way to avoid an uninitialized variable is to initialize variables at the same time they
are declared.
• You can initialize some, all, or none of the variables in a declaration that lists more than
one variable
• Examples:
• int count=0;
• double average=99.9;
• int a=10, b, c=0;
Output using cout
• The values of variables as well as strings of text may be output to the screen
using cout.
• The arrow notation << is often called the insertion operator.
• You do not need a separate copy of the word cout for each item output. You
can simply list all the items to be output preceding each item to be output with
the arrow symbols <<.
• Strings must be included in double quotes.
• Examples:
• cout<<“This is our first c++ program”;
• cout<<“The sum is”<<sum;
• cout<<“distance is”<<(time * speed);
Input using cin
• A cin statement sets variables equal to values typed in at the keyboard.
• Syntax
• cin >> Variable_1 >> Variable_2 >> ... ;
• Example
• cin >> number >> size;
• cin >> time;
Escape sequences
• The backslash, , preceding a character tells the compiler that the character
following the  does not have the same meaning as the character appearing by
itself. Such a sequence is called an escape sequence
• Examples
New line n
Horizontal tab t
Alert a
Backslash 
Double quote ”
Alternatively, you can start a new line by outputting endl.
cout<<“Enter a number:”<<endl;
Data types
• Integer types
• The types for whole numbers are called integer types.
• Floating point types
• The type for numbers with a decimal point are called floating-point types
CONTD…
• int
• The size of integer data types can vary from one machine to another.
On a 32-bit machine an integer might be 4 bytes while on a 64-bit
machine an integer might be 8 bytes. Here are some fixed width
integer types.
Type char
• Values of the type char, which is short for character, are single symbols
such as a letter, digit, or punctuation mark.
• A variable of type char can hold any single character on the keyboard e.g.,
’A' or a '+' or an 'a’.
• Note that uppercase and lowercase versions of a letter are considered
different characters.
• The text in double quotes that are output using cout are called string values.
• Be sure to notice that string constants are placed inside of double quotes,
while constants of type char are placed inside of single quotes.
Type bool
• Expressions of type bool are called Boolean after the English mathematician
George Boole, who formulated rules for mathematical logic.
• Boolean expressions evaluate to one of the two values, true or false.
• Boolean expressions are used in branching and looping statements.
Class string
• string class is used to process strings in a manner similar to the other data types.
• To use the string class we must first include the string library:
• #include <string>
• You declare variables of type string just as you declare variables of types int or double.
• string day;
• day = "Monday";
• You may use cin and cout to read data into strings.
• You can use ‘+’ operator between two strings to concatenate them.
• When you use cin to read input into a string variable, the computer only reads until it
encounters a whitespace character. Whitespace characters are all the characters that
are displayed as blank spaces on the screen, including the blank or space character,
the tab character, and the new-line character 'n’. This means that you cannot input a
string that contains spaces.
Arithmetic operators and expressions
• In a C++ program, you can combine variables and/or numbers using the
arithmetic operators + for addition, – for subtraction, * for multiplication, and /
for division.
• All of the arithmetic operators can be used with numbers of type int, numbers
of type double, and even with one number of each type.
• The % operation gives the remainder.
• The computer will follow rules called precedence rules that determine the
order in which the operators, such as + and *, are performed. These
precedence rules are similar to rules used in algebra and other mathematics
classes.
Comment
• In C++ the symbols // are used to indicate the start of a comment.
• All of the text between the // and the end of the line is a comment.
• The compiler simply ignores anything that follows // on a line.
• Anything between the symbol pair /* and the symbol pair */ is considered a
comment and is ignored by the compiler. Unlike the // comments, /* to */
comments can span several lines,
Naming constants
• When you initialize a variable inside a declaration, you can mark the variable
so that the program is not allowed to change its value. To do this, place the
word const in front of the declaration, as described below:
• Syntax
• const Type_Name Variable_Name = Constant;
• Examples
• const int MAX_TRIES = 3;
• const double PI = 3.14159;
ClassTask
1-Write a program should prompt the
user to enter the following strings:
 The first or last name of your instructor
 Your name
 A food
 A number between 100 and 120
THE END

More Related Content

Similar to lec 2.pptx

INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
MamataAnilgod
 
C language
C languageC language
C language
Rupanshi rawat
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
marvellous2
 
FUNDAMENTAL OF C
FUNDAMENTAL OF CFUNDAMENTAL OF C
FUNDAMENTAL OF C
KRUNAL RAVAL
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
Thapar Institute
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
Raghuveer Guthikonda
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
CSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageCSE 1201: Structured Programming Language
CSE 1201: Structured Programming Language
Zubayer Farazi
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
FatimaZafar68
 
C++ programming
C++ programmingC++ programming
C++ programming
Anshul Mahale
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
Aahwini Esware gowda
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 
Data type
Data typeData type
Data type
Isha Aggarwal
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
Rasan Samarasinghe
 
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
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
Abhinav Vatsa
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
RohanJoshi290109
 
Basics of C
Basics of CBasics of C
Basics of C
pksahoo9
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
sophoeutsen2
 
C tokens.pptx
C tokens.pptxC tokens.pptx
C tokens.pptx
NavyaParashir
 

Similar to lec 2.pptx (20)

INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
 
C language
C languageC language
C language
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
FUNDAMENTAL OF C
FUNDAMENTAL OF CFUNDAMENTAL OF C
FUNDAMENTAL OF C
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
CSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageCSE 1201: Structured Programming Language
CSE 1201: Structured Programming Language
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
C++ programming
C++ programmingC++ programming
C++ programming
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Data type
Data typeData type
Data type
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
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
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C
Basics of CBasics of C
Basics of C
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
C tokens.pptx
C tokens.pptxC tokens.pptx
C tokens.pptx
 

Recently uploaded

Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 

Recently uploaded (20)

Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 

lec 2.pptx

  • 1. Programming Fundamentals Chapter # 2 C++ Basics Lecture # 2 Course Instructor: Fadia Ali
  • 2. Outline Variables Names: Identifiers Variable declarations Syntax Assignment statements Uninitialized variables Output using cout Input using cin Escape sequences Data types Type char Type bool Class string Arithmetic operators and expressions Comment Naming constants
  • 3. Variables • Programmers use programming constructs known as variables to name and store data. • A C++ variable can hold a number or data of other types. Data held in a variable is called its value; • Number/data held by a C++ variable can be changed. • A C++ variable is guaranteed to have some value in it, if only a garbage number left in the computer’s memory by some previously run program. • In programming languages, variables are implemented as memory locations. The compiler assigns a memory location to each variable name in the program. The value of the variable, in a coded form consisting of 0s and 1s, is kept in the memory location assigned to that variable. • We do not know what addresses the compiler will choose for the variables in our program.
  • 4. Names: Identifiers • Identifiers are used as names for variables and other items in a C++ program. • To make your program easy to understand, you should always use meaningful names for variables. • An identifier must start with either a letter or the underscore symbol, and all the rest of the characters must be letters, digits, or the underscore symbol. • C++ is a case-sensitive language; that is, it distinguishes between uppercase and lowercase letters in the spelling of identifiers. • A C++ identifier can be of any length. • There is a special class of identifiers, called keywords or reserved words, that have a predefined meaning in C++ and that you cannot use as names for variables or anything else. • Examples: • A, a_1, x123 (legal) • 1ab, da%, 1-2, (not acceptable) • Test, test, TEST (case-sensitive)
  • 5. Variable declarations • Every variable in a C++ program must be declared before the variable can be used. • When you declare a variable, you are telling the compiler—and, ultimately, the computer—what kind of data you will be storing in the variable, what size of memory location to use for the variable and which code to use when representing the variable’s value as a string of 0s and 1s. • Each declaration ends with a semicolon (;). • When there is more than one variable in a declaration, the variables are separated by commas. • The kind of data that is held in a variable is called its type and the name for the type, such as int or double, is called a type name.
  • 6. Syntax • The syntax for a programming languages is the set of grammar rules for that language. • The syntax for variable declarations is as follows: • Syntax • Type_Name Variable_Name_1, Variable_Name_2, ...; • Examples • int count, sum, number_of_person; • double distance;
  • 7. Assignment Statements • In an assignment statement, first the expression on the right-hand side of the equal sign is evaluated, and then the variable on the left-hand side of the equal sign is set equal to this value. • In an assignment statement, the expression on the right-hand side of the equal sign can simply be another variable or a constant. • Syntax • Variable = Expression; • Examples • sum=a; //variable • distance = rate * time; //expression • count=12; //constant
  • 8. Uninitialized variables • Variable that has not been given a value is said to be uninitialized. • The value of an uninitialized variable is determined by whatever pattern of 0s and 1s was left in its memory location by the last program that used that portion of memory. • One way to avoid an uninitialized variable is to initialize variables at the same time they are declared. • You can initialize some, all, or none of the variables in a declaration that lists more than one variable • Examples: • int count=0; • double average=99.9; • int a=10, b, c=0;
  • 9. Output using cout • The values of variables as well as strings of text may be output to the screen using cout. • The arrow notation << is often called the insertion operator. • You do not need a separate copy of the word cout for each item output. You can simply list all the items to be output preceding each item to be output with the arrow symbols <<. • Strings must be included in double quotes. • Examples: • cout<<“This is our first c++ program”; • cout<<“The sum is”<<sum; • cout<<“distance is”<<(time * speed);
  • 10. Input using cin • A cin statement sets variables equal to values typed in at the keyboard. • Syntax • cin >> Variable_1 >> Variable_2 >> ... ; • Example • cin >> number >> size; • cin >> time;
  • 11. Escape sequences • The backslash, , preceding a character tells the compiler that the character following the does not have the same meaning as the character appearing by itself. Such a sequence is called an escape sequence • Examples New line n Horizontal tab t Alert a Backslash Double quote ” Alternatively, you can start a new line by outputting endl. cout<<“Enter a number:”<<endl;
  • 12. Data types • Integer types • The types for whole numbers are called integer types. • Floating point types • The type for numbers with a decimal point are called floating-point types
  • 13. CONTD… • int • The size of integer data types can vary from one machine to another. On a 32-bit machine an integer might be 4 bytes while on a 64-bit machine an integer might be 8 bytes. Here are some fixed width integer types.
  • 14. Type char • Values of the type char, which is short for character, are single symbols such as a letter, digit, or punctuation mark. • A variable of type char can hold any single character on the keyboard e.g., ’A' or a '+' or an 'a’. • Note that uppercase and lowercase versions of a letter are considered different characters. • The text in double quotes that are output using cout are called string values. • Be sure to notice that string constants are placed inside of double quotes, while constants of type char are placed inside of single quotes.
  • 15. Type bool • Expressions of type bool are called Boolean after the English mathematician George Boole, who formulated rules for mathematical logic. • Boolean expressions evaluate to one of the two values, true or false. • Boolean expressions are used in branching and looping statements.
  • 16. Class string • string class is used to process strings in a manner similar to the other data types. • To use the string class we must first include the string library: • #include <string> • You declare variables of type string just as you declare variables of types int or double. • string day; • day = "Monday"; • You may use cin and cout to read data into strings. • You can use ‘+’ operator between two strings to concatenate them. • When you use cin to read input into a string variable, the computer only reads until it encounters a whitespace character. Whitespace characters are all the characters that are displayed as blank spaces on the screen, including the blank or space character, the tab character, and the new-line character 'n’. This means that you cannot input a string that contains spaces.
  • 17. Arithmetic operators and expressions • In a C++ program, you can combine variables and/or numbers using the arithmetic operators + for addition, – for subtraction, * for multiplication, and / for division. • All of the arithmetic operators can be used with numbers of type int, numbers of type double, and even with one number of each type. • The % operation gives the remainder. • The computer will follow rules called precedence rules that determine the order in which the operators, such as + and *, are performed. These precedence rules are similar to rules used in algebra and other mathematics classes.
  • 18. Comment • In C++ the symbols // are used to indicate the start of a comment. • All of the text between the // and the end of the line is a comment. • The compiler simply ignores anything that follows // on a line. • Anything between the symbol pair /* and the symbol pair */ is considered a comment and is ignored by the compiler. Unlike the // comments, /* to */ comments can span several lines,
  • 19. Naming constants • When you initialize a variable inside a declaration, you can mark the variable so that the program is not allowed to change its value. To do this, place the word const in front of the declaration, as described below: • Syntax • const Type_Name Variable_Name = Constant; • Examples • const int MAX_TRIES = 3; • const double PI = 3.14159;
  • 20. ClassTask 1-Write a program should prompt the user to enter the following strings:  The first or last name of your instructor  Your name  A food  A number between 100 and 120