C++ Basics: Variables, I/O, Data Types, Control Flow & Style (Chapter 2 Notes)
C++ Basics: Variables, I/O, Data Types, Control Flow & Style
2.1 Variables and Assignments
Variables
Variables in C++ are like names for memory locations.
We can write a value into them.
We can change the value stored there.
Memory locations are never truly "erased"; some value is always present.
Analogy: Small blackboards where one can write, change, or erase a number (though erasing isn't quite accurate for computer memory).
Example (Display 2.1): A basic C++ program often uses variables like
int number_of_bars;anddouble one_weight, total_weight;to store and manipulate numerical data.
Identifiers
Variable names are referred to as identifiers.
Rules for Choosing Variable Names (Identifiers):
Should be meaningful and represent the data they store (e.g.,
number_of_bars).The first character must be:
A letter (a-z, A-Z).
The underscore character (
_).
Remaining characters (after the first) must be:
Letters.
Numbers (0-9).
The underscore character (
_).
Keywords (Reserved Words)
These are words that are used by the C++ language itself.
They have predefined meanings and must be used as defined by the programming language.
Cannot be used as identifiers (variable names) because that would cause ambiguity for the compiler.
Declaring Variables
Variables must be declared before they can be used in a program.
Declaration tells the C++ compiler the type of data the variable will store.
Examples of Data Types:
int: An abbreviation for integer. Used for whole numbers without fractional components.Can store values like 3, 102, 3211, -456. (e.g.,
int number_of_bars;)
double: Represents floating-point numbers with a fractional component.Can store values like 1.34, 4.0, -345.6. (e.g.,
double one_weight, total_weight;)
Placement of Declaration: Variables can be declared immediately prior to their first use.
Declaration Syntax:
Type_Name Variable_Name;Type_Name Variable_1, Variable_2, ...;(for declaring multiple variables of the same type in one statement).
Declaration Examples:
int number_of_widgets, count;double size;
Assignment Statements
An assignment statement is used to change or set the value of a variable.
Syntax:
variable = expression;Assignment statements always end with a semicolon (
;).The single variable whose value is being changed is always on the left of the assignment operator (
=).The right side of the assignment operator can be:
A constant:
age = 21;Another variable:
my_cost = your_cost;An expression:
circumference = diameter * 3.14159;
Example:
total_weight = one_weight + number_of_bars;This statement calculates the sum of
one_weightandnumber_of_bars, then stores that result into thetotal_weightvariable.
Assignment Statements and Algebra
The
='operator in C++ is not equivalent to the equal sign in algebra.In C++,
variable = expression;means to evaluate theexpressionon the right and then store the resulting value into thevariableon the left.Example:
number_of_bars = number_of_bars + 3;This statement is mathematically false in algebra.
In C++, it means: take the current value of
number_of_bars, add 3 to it, and then store this new result back intonumber_of_bars.
Initializing Variables
Declaring a variable does not automatically give it a value; its memory location might contain arbitrary "garbage" data.
Initialization: The process of giving a variable its first value.
Variables are commonly initialized through assignment statements.
Combining Declaration and Initialization:
Method 1 (Assignment-like syntax):
int number_of_bars = 32;Method 2 (Parentheses/Constructor-like syntax):
double mpg(26.3), area(0.0), volume;
Note: In this method,volumeis declared but not initialized. Onlympgandareareceive initial values.
2.2 Input and Output
Data Streams
A data stream is a sequence of data, typically characters or numbers.
Input Stream: Data that a program uses, typically originating from the keyboard or a file.
Output Stream: Data that a program produces as its output, typically destined for the monitor or a file.
Output using cout
coutis the standard C++ output stream, by default sending data to the monitor.The insertion operator
<<is used to insert (send) data intocout.Example: `cout << numberofbars <<