Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Computer Programming

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

C++ has built-in support for 3 major data types:


 Numerical, Alphabetic, and Logical
 Numerical: Integers and floating point
 Alphabetic: character (and string)
 Logical: true or false
 These can be used to save information of the given type by setting aside
a memory location to store the information under a user defined name
Numerical Data

Two broad categories


Integers store “whole” numbers
 No fractions or decimals
 positive and negative “counting” numbers, incl. zero
 Three types: short, int, long

Floating point numbers


 “Fractional” numbers (i.e. those with decimal points)
 Two main types: float and double
 Can be represented in either decimal or scientific notation
 Decimal: 3.14159
 Scientific 2.75e3 (mantissa and exponent) – same as 2.73x10 3
Alphabetic Data

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)

 To use strings, must include the string library: #include <string>


Logical Data

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

To store information of a given type, a variable (identifier) of that type


must be declared
 The type must be specified
 A name for the variable must be declared
 An initial value may, optionally, be assigned

type name[=value], [name2=value2], …;

 Multiple variables can be assigned in one statement


 Separated with commas
Valid variable names

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

Can store positive and negative integers


 No decimals!
 Int (4 byte)
Three types
 short, int, long
 Usually representing different numerical ranges
 We will be using int primarily
 Ranges are compiler dependent
 Generally: short <= int <= long
 On our system, int can store values between -2147483648 and 2147483647
 (approx. +/- 2 billion)
Floating Point Numbers

Stores numerical data with decimals

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

variable can hold a number or a data of other


types, it always holds something. A variable has a
name
1001
the data held in variable is called value 1002
y 12.5 1003
variables are implemented as memory locations 1004
1005
and assigned certain memory address. The exact Temperature 32 1006
address depends on computer and compiler. Letter 'c' 1007
1008
we think as though the memory locations are Number - 1009
actually labeled with variable names
Keywords

keywords are identifiers reserved as part of the language


int, return, float, double
 they cannot be used by the programmer to name things
 they consist of lowercase letters only
 they have special meaning to the compiler
Keywords (cont.)

asm do if return typedef


auto double inline short typeid
bool dynamic_cast int signed typename
break delete long sizeof union
case else mutable static
unsigned
catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned
Variable Declarations

 every variable in C++ program needs to be declared


 declaration tells the compiler (and eventually the computer) what kind of data is going to be stored in
the variable known list of one or
 the kind of data stored in variable is called it’s type

type more identifiers
a variable declaration specifies
 type

 name type id, id, ..., id;


 declaration syntax:
 two commonly used numeric types are:
 int - whole positive or negative numbers:

1,2, -1,0,-288, etc.


 double - positive or negative numbers with fractional part:

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?

cout << ”\” this is a \t very cryptic \” statement \\ \n”;

You might also like