Professional Documents
Culture Documents
Chapter 2 (Part 2) - Week 2
Chapter 2 (Part 2) - Week 2
Contents
Keywords, Variables and Identifiers
Variable Declaration & Definition
Basic C++ Variable Types
Assignment Statement
Variable Initialization and Memory Concepts
Input with cin
Cascading of Input/Output Operator
setw Manipulator
Objectives
To understand how to declare the variables and assign values to
them
To know different data types of the variables and their size
To understand how to accept the user’s input using cin object
To know use of setw manipulator
Learning Outcomes
After completing this lecture, students will be able to:
Understand the concept of variables and constants.
Use the concept of data types in C++.
Know the syntax and semantics of data declaration statements and
assignment statements.
Use cin to get information from user.
Use of setw manipulator to set field width for text as well as numbers.
Keywords
Keywords are words reserved as part of the language and special meaning to the
compiler.
Example,
are containers that locate on computer’ memory to store data then use and change its
value in a program.
E.g.; int firstnumber=1;
Variable value 1
Identifiers
The name given to variables are called identifiers.
The rules for writing identifiers:
use upper or lower case letter
use digit letters 0..9
use the underscore(_)
the first character must be a letter or underscore
The compiler distinguishes the upper and lower letters means
case sensitive.(eg: num and NUM are two different variables.)
As Naming convention, all lowercase letters is used for
variable names.
Identifiers with Examples
Grade My Name
myName 3rd_Number
movie_name first-name
a_123 Grade(Test)
abc int
_temp 123
Variable Declaration and Definition
Variable declaration
declares firstly before it is used.
tells the compiler to allocate enough memory to hold a value of this
data type and to associate the identifier with this location.
Memory
number
1
Syntax: <data type> <name>; 0
Variables of type bool can have only two possible values: true and false.
Compilers often store them as a byte (for true/false).
Float
stores decimal numbers (15 decimal digits) and requires 8 bytes of storage.
Long double
int integer1;
integer1= 45; integer1 4
5
int integer2; integer1 4
integer2 = 72; 5
integer2 7
2
int sum = integer1 + integer2;
integer1 4
5
integer2 7
2
su 1
m 1
7
Input with cin
Input operator
//display the multiplication of the two //display the multiplication of the two numbers using cin
numbers without using cin #include <iostream>
#include <iostream> using namespace std;
using namespace std; int main()
int main() {
{ int x, y;
int x = 2, y = 3; int result;
int result; cin >> x;
result = x * y; cin >> y;
cout << “Result = " << result; result = x * y;
return 0; cout << “Result = " << result;
} Outp
Outp return 0; ut
ut 12
Result = 6 }
3
Result = 36
Cascading of Input/Output Operator
Cascading of the input operator
refers to the consecutive occurrence of input operators in a single
statement.
Example: cin >> a >> b >> c >> d ;
Cascading of the output operator
refers to the consecutive occurrence of output operators in a single
statement.
Example: cout << a << b << c << d ;
The setw Manipulator
The manipulators are operators used with the insertion operator (<<) to modify
—or manipulate—the way data is displayed.
The setw manipulator changes the field width of output.
The field width of integer 567 will occupy a field three numers wide, and the
string “pajamas” will occupy a field seven characters wide.
The setw manipulator causes the number (or string) that follows it in the stream
to be printed within a field n characters wide, where n is the argument to
setw(n).
The value is right justified within the field.
Example setw(10)
cout << setw(10) << “number” << endl;
The setw Manipulator Example 1
#include <iostream.h>
The output should be
#include <iomanip.h> // for setw
int main()
{
long pop1=2425785, pop2=47, pop3=9761;
cout << setw(8) << “LOCATION” << setw(12) << “POPULATION” <<
endl
<< setw(8) << “Portcity” << setw(12) << pop1 << endl
<< setw(8) << “Hightown” << setw(12) << pop2 << endl
<< setw(8) << “Lowville” << setw(12) << pop3 << endl;
return 0;
}
The setw Manipulator Example 2
#include <iostream.h>
#include <iomanip.h> // for setw What is the output of the program? Do you
guess the function of setfill()?
int main()
{
long pop1=2425785, pop2=47, pop3=9761;
cout << setw(8) << “LOCATION” << setw(12) << “POPULATION” <<
endl
<< setw(‘*’)<<setw(8) << “Portcity” << setw(12) << pop1 << endl
<< setw(‘*’)<< setw(8) << “Hightown” << setw(12) << pop2 << endl
<< setw(‘*’)<< setw(8) << “Lowville” << setw(12) << pop3 << endl;
return 0;
}
Summary
Keywords, Variables and Identifiers
Variable Declaration & Definition
Basic C++ Variable Types
Assignment Statement
Variable Initialization and Memory Concepts
Input with cin
Cascading of Input/Output Operator
setw Manipulator
Reading Assignments