SlideShare a Scribd company logo
1 of 21
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++.pptxMamataAnilgod
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
CSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageCSE 1201: Structured Programming Language
CSE 1201: Structured Programming LanguageZubayer Farazi
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.pptFatimaZafar68
 
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_outputAnil Dutt
 
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 LanguageDr.Florence Dayana
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic conceptsAbhinav Vatsa
 
Basics of C
Basics of CBasics of C
Basics of Cpksahoo9
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 

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

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

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