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