Computer
Data type :: Defines what kind of data a variable can store
Why are data types important in C++? :: They specify what kind of data is stored in a variable
Primitive (fundamental) data types :: int, float, double, char, bool
int :: Integer; stores whole numbers without decimals; 2 or 4 bytes
float :: Stores fractional numbers with decimals; 4 bytes; about 7 decimal digits
double :: Stores fractional numbers with decimals; 8 bytes; about 15 decimal digits
char :: Character; stores a single character/letter/number (ASCII); 1 byte
bool :: Boolean; stores true or false; 1 byte
string :: Stores a sequence of characters (text)
What symbols surround a char value? :: Single quotes ' '
What symbols surround a string value? :: Double quotes " "
Example of int declaration :: int age = 35;
Example of float declaration :: float grade = 74.50;
Example of double declaration :: double average = 10.854;
Example of char declaration :: char letter = 'A';
Example of string declaration :: string text = "Hello";
Example of bool declaration :: bool happy = true;
What happens with int myNum = 20.55;? :: It is wrong for an int; ints store whole numbers only (decimal part is dropped)
Which data type for 90? :: int
Which data type for 89.43? :: float or double
Which data type for "Good!"? :: string
Which data type for 'C'? :: char
Which data type for true/false? :: bool
Which data type for age 35? :: int
Which data type for a grade of 74.50? :: float
Which data type for 10.854 (average)? :: double
Which data type for "ABC" or "Hi"? :: string (not char, since char holds only one character)
Which keyword reads user input in C++? :: cin
Which operator is used with cin? :: >> (extraction operator)
Example of user input :: cin >> age;
Which keyword displays output? :: cout (used with <<)
Type modifiers :: Keywords used to change the meaning of fundamental data types
Four type modifiers in C++ :: short, long, signed, unsigned
short :: Used for small integers (range about -32,767 to 32,767); short is equivalent to short int
long :: Used for large integers (range about -2,147,483,647 to 2,147,483,647); long is equivalent to long int
long double :: Used for large floating-point numbers; needs the L suffix, e.g. long double c = 0.3333L;
What if you skip the L suffix on a long double? :: It is a double converted to long double, which may cause data loss
long long :: long repeated twice; for even larger numbers; can only be used with int
unsigned :: Can hold only non-negative integers (0 and up); unsigned int range is 0 to 4,294,967,295
signed :: Can hold both positive and negative values
Range of a regular int :: -2,147,483,648 to 2,147,483,647
How many char types does C++ have? :: 3: char, signed char, unsigned char
signed char guaranteed range :: -127 to 127
unsigned char guaranteed range :: 0 to 256 (per the lesson slide; standard is 0 to 255)
Plain char :: Using char without signed or unsigned
Variable :: A name the programmer uses to refer to a computer storage location
Why use a variable? :: Because the value stored in it can change or vary
Regular variable :: Like an empty container that can be filled with values and changed later
Constant variable :: Stores a value that, once declared, cannot be changed
Examples of regular variables :: Everyday clothes, name of country shown, age, speed of a car
Examples of constants :: PI, birth date, speed of light in a vacuum, leap year
Syntax to declare a variable :: data_type variable_name; (e.g. int grade;)
Syntax to declare and assign :: data_type variable_name = value; (e.g. int grade = 85;)
Assignment operator :: = (assigns a value to a variable)
Rule: variable data type :: You need to know what data type you are going to use
Rule: variable name length :: 1 to 255 characters
Rule: variable name start :: Must begin with a letter or an underscore
Rule: after the first letter :: Names can contain letters and numbers
Rule: case :: Variable names are case sensitive
Rule: spaces and symbols :: No spaces or special characters allowed
Rule: keywords :: You cannot use a C++ keyword as a variable name
Valid variable names :: Username, _temp, aDDress, xyz123, num1, AveGrade, Qty_Price, J_0817
Reserved words :: Words that must be used only for their specified purpose; misusing them causes an error (e.g. int, char, return, while, void, const)
Local variable :: Declared inside a function or block; not known outside its own function
Global variable :: Declared outside all functions; can be accessed by any function in the program
Global vs local: scope :: Global lasts throughout the program; local stays within its function only
Global vs local: lifetime :: Global exists the whole run; local is created when the function runs and destroyed when it ends
Global vs local: default value :: Uninitialized global = 0; uninitialized local = garbage value
Global vs local: memory :: Global is in the data segment; local is in the stack
Global vs local: same name :: Cannot declare many globals with the same name; can reuse a name in different functions for locals
Global vs local: changes :: Changing a global affects the whole program; changing a local does not affect other functions
Operators :: Used to perform operations on variables and values
Four operator categories :: Arithmetic, assignment, comparison, logical
Arithmetic operators :: Perform common mathematical operations
+ :: Addition; adds two values (x + y)
- :: Subtraction; subtracts one value from another (x - y)
* :: Multiplication; multiplies two values (x * y)
/ :: Division; divides one value by another (x / y)
% :: Modulus; returns the division remainder (x % y)
++ :: Increment; increases a variable's value by 1 (++x)
-- :: Decrement; decreases a variable's value by 1 (--x)
Assignment operators :: Used to assign values to variables
= :: x = 5 means x = 5
+= :: x += 3 is the same as x = x + 3
-= :: x -= 3 is the same as x = x - 3
*= :: x *= 3 is the same as x = x * 3
/= :: x /= 3 is the same as x = x / 3
%= :: x %= 3 is the same as x = x % 3
&= :: x &= 3 is the same as x = x & 3
|= :: x |= 3 is the same as x = x | 3
^= :: x ^= 3 is the same as x = x ^ 3
Comparison operators :: Compare two values; return true or false (Boolean values)
== :: Equal to (x == y)
!= :: Not equal (x != y)
> :: Greater than (x > y)
< :: Less than (x < y)
>= :: Greater than or equal to (x >= y)
<= :: Less than or equal to (x <= y)
Logical operators :: Determine the logic between variables or values
&& :: Logical AND; true only if both statements are true
|| :: Logical OR; true if at least one statement is true
! :: Logical NOT; reverses the result (true becomes false)
AND truth table :: T&&T=T, T&&F=F, F&&T=F, F&&F=F
OR truth table :: T||T=T, T||F=T, F||T=T, F||F=F
NOT truth table :: !True = False, !False = True
Evaluate: A=50, B=80; (A>20) && (B<=50) :: False (true && false)
Evaluate: X=35, Y=70; (X>=25) && (Y<=60) :: False (true && false)
Evaluate: X=20, Y=150, Z=50, A=true; !(25>X) && (Z<50) || (Z*3==Y) && !A :: False ((false && false) || (true && false); && is evaluated before ||)
Evaluate: bool x=true, y=false; (x==true) && (y==false) :: True (true && true)
Demo program: what does it display? :: "The product of 5 and 10 is 50" (global a, b, product; main sets a=5, b=10, product=a*b)
Lesson objective: variable program :: Create a simple program that displays the sum, difference, product, and quotient of two numbers using variables