Professional Documents
Culture Documents
Programming Fundamentals 02
Programming Fundamentals 02
LECTURE #2
C++ format
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
C++ Data Types
Data Type
A key capability of a programming language
Establishes both the type of information that can be stored as well as
operations that can be done on that information
Character data
Store a single alphanumeric character
Type is char
characters are represented in single quotes: ‘x’, ‘A’, or ‘=‘
Strings data
Store a “string” of characters, like a name
Type is string – specified in double quotes: “John” or “apple”
Not built-in, comes from a pre-defined library (distributed with C++)
It is actually a class (which did not exist in the original C language)
Boolean
Named for George Boole
English mathematician (mid 1800’s) – developed Boolean Algebra
Type is bool – used to represent conditional values
Supports only two values: true and false
Note: true and false are not strings – no quotes!
Variables/Identifiers
Rules
Can only contain letters, numbers, and underscore (_)
Good: name, x, abc123, last_name
Bad: last-name
Cannot begin with a number
Bad: 1letter
Cannot be a C++ reserved word
Bad: float, int, main
Is case-sensitive
Name is not the same as name
Variable Declaration Examples
int i; // variable (no initialization)
int j, k, l; // multiple variables
int n=10; // variable with initialization
float x, pi= 3.14159; // init and no init
char a= ’A’;
string name= ”John”;
bool maybe= true;
Integers
Two types
float and double
We will be using float primarily
Ranges are compiler and system dependent - on ours:
float can store pos and neg values between 1e-38 and 1e38 (approx.)(4 byte)
float has 6 decimal digits of precision
double has much greater range and precision(8 byte)
long double (16 byte)
Data/Variable Storage
Counting in Binary
Data stored in memory 0 0
1 1
Each memory location has a numerical address
2 10
Each location can store one byte of information
3 11
One byte is 8 bits
One bit can store one binary digit (0 or 1) 4 100
So, a byte can store 28 unique values (256 values) 5 101
6 110
Number of bytes required to store information 7 111
8 1000
will depend on the type of data being stored
… -
255 11111111
Variables
1.75, -0.55
example declarations:
int numberOfBars;
double weight, totalWeight;
Where to Declare
the variables should be declared as close to the place where they are used as possible.
if the variable will be used in several unrelated locations, declare it at the beginning of the program:
int main() {
right here
note that variable contains a value after it is declared. The value is usually arbitrary
Assignment
var = value;
assignment statement is an order to the computer to set the value of the variable on the left hand side of the
equation to what is written on the right hand side
it looks like a math equation, but it is not
Example:
numberOfBars = 37;
totalWeight = oneWeight;
totalWeight = oneWeight * numberOfBars;
numberOfBars = numberOfBars + 3;
Escape Sequences
certain sequences of symbols make special meaning to the computer.
They are called escape sequences
escape sequence starts with a backslash (\). It is actually just one special
character.
Useful escape sequences:
new-line \n
horizontal tab \t
alert \a
backslash \\
double quote \”
What does this statement print?