Lec01BasicsFlow_6per.pdf - EECS 402 Identifiers The...

Info icon This preview shows pages 1–3. Sign up to view the full content.

1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 01 Savitch Ch. 2 C++ Basics Flow Of Control EECS 402 EECS 402 Andrew M Morgan 2 Identifiers Names of variables, constants, user-defined functions, etc Valid identifiers Must start with letter or underscore Contains only letters, digits, or underscore Can not be C/C++ reserved word Note: C/C++ identifiers are case sensitive Valid identifier examples i, val, Val, VAL, _internal, my_var, myVar, twoNums, x54 Invalid identifier examples 2nums, my-var, class, file.name EECS 402 EECS 402 Andrew M Morgan 3 Variables All variables must have a name and a type C++ is a strongly-typed language Variable names are any valid C++ identifier The type of a variable describes what kind of data it holds Values of variables can change throughout a program Following are some of the C++ data types int: Integer data (-6, 0, 741, -1024) float/double: Floating point data (6.5, 8.0, -97.21204, 0.0081) char: Character data ('a', 'q', '5', '\n') bool: Boolean values (true, false) EECS 402 EECS 402 Andrew M Morgan 4 Using Variables Before any variable can be used, it must be declared Gives the variable a type and sets aside memory int counter; //Declares an integer variable called counter float average; //Declares a float called average char grade; //Declares a character to represent a grade Assignment setting a variables value counter = 10; average = 88.25; grade = 'B'; Initialization can be done during declaration char modif = '+'; //Modifer to be appended to grade int sumOfValues = 0; //Some of input values float initialBudget = 135.50; //Initial budget for week If not initialized, the value of the variable is undefined Note: It will most likely NOT be 0 Style: Variable names in lower case, except first letters of non-first words EECS 402 EECS 402 Andrew M Morgan 5 Declaring Constants Constants must have a name and a type The value of a constant must be initialized at declaration time The value is not allowed to change during program execution Used to avoid "magic numbers" literal values in a program Seeing the value 12 in a program is not very meaningful it could represent the number of quiz scores, the number of hours in a half- day… Begin declaration with C++ keyword "const" const float PI = 3.1415; const int NUM_SCORES = 12; const char BEST_GRADE = 'A'; Style: Constant names in ALL CAPS to differentiate from variables EECS 402 EECS 402 Andrew M Morgan 6 Some C++ Operators Operators are used on variables and/or literals to compute a new value. = Assignment operator (not equality) + , - , * , / Add, subtract, multiply, divide % Modulus (remainder) == , != Equality, inequality ++ , -- Increment, decrement += , -= , *= , /= Add/assign, etc i -= 4 is equivalent to i = i 4 > , < , >= , <= Greater than, less than, greater or equal, less or equal && Logical AND, returns true when both operands are true || Logical OR, returns true when >= 1 operand is true ! Local NOT, returns true when operand is false
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.