// A simple C++ program # include using namespace std; int main () { cout << “Welcome to CS1005! \n”; return 0; }
General comments Use whitespace to make it more readable Common programming errors Don’t reinvent the wheel Maximum warnings on compiler
What not to do with Whitespace #include using namespace std;int main(){cout<<"Welcome to CS1005!\n";return 0;}
// This program adds two numbers and displays the result // on the screen # include using namespace std; int main () { int result; result = 2 + 3; cout << “The sum of is “ << result << endl; return 0; }
Data Type Determines how much memory is needed to store this information Determines what sort of information the data type represents Determines which operations are legal on this information
Rules for Identifiers Must start with a letter May only contain letters, digits and underscore character Must not be a keyword C++ is case sensitive. C++ allows any length Our standard - start with small letter, capitalize first letter of each word (avgTemp) Should be meaningful. Reflect it’s purpose (avgGrade, worstGrade, bestGrade)
Arithmetic Operators C++ OperationArithmetic Operator Algebraic Expression C++ Expression Addition+a + b Subtraction-a - b Multiplication*ab or a x ba * b Division/a/b or a or a ba/b Modulus%a mod ba % b
Rules of precedence for Arithmetic Operations OperatorOperationOrder of evaluation ( )ParenthesesEvaluated first. If the parentheses are nested the innermost pair is evaluated first. If there are several pairs on the same level they are evaluated left to right. * / %Division, mulitiplication, modulus Evaluated second. If there are several they are evaluated left to right. + -Addtition, subtractionEvaluated last. If there are several, they are evaluated left to right
Exercise 2 int a, x, b, c, y; a = 2; x = 5; b = 3; c = 7; y = a * x * x + b * x + c; What is y after this program executes? Exercise 1 int num1, num2, num3; num1 = 3; num2 = num1; num1 = num2 + 1; num3 = num2 * num1 - 6; What are the values for num1, num2 and num3 after this program executes?
Relational Operators Standard algebraic expression C++ operatorExample of C++ expression Meaning >>a > ba is greater than b <<a < ba is less than b >=a >=ba is greater than or equal to b <=a <= ba is less than or equal to b ===a == ba is equal to b !=a != ba is not equal to b
Logical Operators OperatorNameExampleTrue if !Not!xx is false &&Andx && yx and y are both true ||Orx || yx or y is true
OperatorType ()parentheses !not * / %multiplication, division, modulus + -addition, subtraction =relationals == !=equality &&and ||or Precedence chart
Data Type Determines how much memory is needed to store this information Determines what sort of information the data type represents Determines which operations are legal on this information
Determining Size and Range // Determining the size and range of the the signed long integer type. #include using namespace std; int main() { cout << "signed long type is " << sizeof (signed long) << "bytes\n"; cout << "largest value of signed long type is " << LONG_MAX << endl; cout << "smallest value of signed long type is " << LONG_MIN << endl; return 0; }
Floating Point Data Types TypeTypical rangeTypical precision float10 e -38 to 10 e 38 6 digits double10 e -308 to 10 e digits long double10 e to 10 e digits
Things to remember about floating point data types Mantissa is limited in size so value may not be exact Arithmetic is faster using integer types than floating point types Don’t use equality operators Modulus operator not valid for floating point Don’t use floating point #’s for loop control
Mixing float and integer types #include using namespace std; int main() { float average; int numberOfGrades = 2, totalOfGrades = 9; average = totalOfGrades / numberOfGrades; cout << "The average is " << average << endl; return 0; }
Data Types Summary Given the following declarations, determine the value of the variable on the left-hand- side of each assignment statement. int int1, int2, int3; float f1=1.0, f2=2.5, f3=5.0; double d1, d2; char ch1, ch2; int1 = 5; d2 = 5.0; ch1 = '5'; int3 = f2; int2 = int1 / int3; d1 = f3 - -f1 * 6 / int * (int1 - d2); ch2 = ch1 - 2; int3 = 'a' - 'A'; ch1 = 'W' + int3;