// Lab 1 Lecture // ---------------------------- Data Types ------------------------------------ // We will come across several different types of data while programming. // The computer uses these data types differently, so it is important to know // which is appropriate to use. // There are several basic data types provided by C++ programming language // called primitive data types (also known as fundamental data types). // Group Type Name Examples // _________________________________________________________________________ // Integers - int - (whole numbers) - 1, 57, 5312 // Floating point - float - (numbers with a decimal point) - 4.5, 2.123 // Floating point - double - (Numbers with a decimal point) - 4.5, 3.14159 // Character - char - (letters, symbols, and numbers) - 'x', '5', '$' // Boolean - bool - (true or false) 1, 0, true, false // You may have noticed that there are two floating point data types // 'float' and 'double'. Their only difference is the amount of memory // they use to store the data. Double type data uses twice the memory // as float type and thus is more precise when calculating small fractions. // ---------------------------- Literals ------------------------------------ // A literal is a fixed value that the programmer has written directly // into the program. #include using namespace std; int main(){ // here the number 5.32 is a floating point literal cout << 5.32 << endl; // here the number 5 is an integer literal cout << 5 << endl; // here the character 5 is a character literal. Notice that the single // quotes around it indicate that it is a character and not a number. cout << '5' << endl; // here "hello world" is a string data type. It is not a primitive data // type and so we did not cover it earlier. Notice that a string // is indicated with a pair of double quotes (""). The iostream library // provides the string data type for us. cout << "hello world" << endl; // ---------------------------- Variables ------------------------------------ // Variables are storage locations or containers that are given a name // (identifier) so that they may be easily referenced to in your program. // "Type" is the data type that will be stored in the variable and the "name" // is the identifier given to reference it. A variable name can only include // alphabet letters, numbers and the underscore character (_). Also, // a variable name cannot start with a number. // To declare (create) a variable the following syntax can be used: // Type Name int myint; // This creates a variable called "myint" that will hold an integer. // We can now assign a value to it using the assignment operator (=). myint = 5; // we have now put the value 5 into our variable "myint". // We can also declare and initialize a variable at the same time using the // following syntax : // Type Name A.O. Value (A.O. = Assignment Operator) float myfloat = 5.34; // *Note* the value 5.34 is a literal we are assigning to the variable "myfloat" // ---------------------------- Named Constants ------------------------------- // Sometimes we do not want a variable to change after it has been created. // A named constant is simply a variable that cannot change after being // declared and initialized. To make a variable constant simply put the // identifier 'const' before the variable declaration. // For example the value of pi will never change and so we want to make sure // that we never accidentally modify it. Therefore We should make it constant. const double PI = 3.14159; // *note* it is common style for programmers to name a constant in all capital // letters to remind themselves that the variable is a constant. // ------------------------------ Example ------------------------------------- // Example of a program printing literals and variables to the screen // using cout. // declaring two integer variables. int birthdayDay = 17; int birthdayYear = 1986; // declaring a string variable. string birthdayMonth = "June"; cout << "I was born on " << birthdayMonth << " " << birthdayDay << ", " << birthdayYear << endl; // ---------------------------------------------------------------------------- return 0; }