0% found this document useful (0 votes)
41 views

Programming Fundamentals - Lecture 02

The document outlines topics for a C++ lecture including variables and data types, arithmetic expressions, reading input, and strings. It provides details on defining variables with names, types, sizes and values as well as rules for naming variables. The document also discusses data types in C++, constants, arithmetic operators, and input/output.

Uploaded by

irum jafri
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Programming Fundamentals - Lecture 02

The document outlines topics for a C++ lecture including variables and data types, arithmetic expressions, reading input, and strings. It provides details on defining variables with names, types, sizes and values as well as rules for naming variables. The document also discusses data types in C++, constants, arithmetic operators, and input/output.

Uploaded by

irum jafri
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Lecture Outline

Variables and Constants


Data types
Read input
Arithmetic Expressions
Strings in C++
Variable
A quantity whose value changes during the
execution of a program. It has:

Name
Type
Size
Value
Rules for naming a variable
First character of a variable name must be an alphabet
or underscore
Blank spaces are not allowed
Special characters (+, *) and reserved words cannot be
used as a variable name
A variable name declared for 1 data type cannot be used
for another data type
C++ is case sensitive language so var1 and Var1 are two
separate variables
Variable Initialization
Assignment Operator
The = sign doesnt mean that the left-hand side is
equal to the right-hand side.

The expression on the right is evaluated, and its


value is placed into the variable on the left.

For example, in C++, it is perfectly legal to write


total_volume = total_volume + 2;
Data Types in C++
Using Undefined Variables
You must define a variable before you use it for the first
time. For example, the following sequence of
statements would not be legal:
double can_volume = 12 * liter_per_ounce;
double liter_per_ounce = 0.0296;

In your program, the statements are compiled in order.


When the compiler reaches the first statement, it does
not know that liter_per_ounce will be defined in the
next line, and it reports an error.
Using Uninitialized Variables
If you define a variable but leave it uninitialized,
then your program can act unpredictably.
Constants
When a variable is defined with the reserved word
const, its value can never change.

Constants are commonly written using capital letters


to distinguish them visually from regular variables.

It is good programming style to use named constants


in your program to explain the meanings of numeric
values
Numeric Ranges and Precisions
If a computation yields a value that is outside the int range, the
result overflows. No error is displayed. Instead, the result is
truncated to fit into an int, yielding a useless value. For example,

int one_billion = 1000000000;


cout << 3 * one_billion << endl;
displays 1294967296.
Input/output
place user input into a variable
Arithmetic Operators
To solve most programming problems we have to
manipulate arithmetic expressions. In C++ language,
these are the basic arithmetic operators:

Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Remainder (%)
Precedence
To evaluate the expression, we need to follow the precedence
rule which is as follows:

Expression within parentheses ( )


Multiplication, division and remainder are evaluated from left
to right
Addition and subtraction are evaluated from left to right

You might also like