This preview shows pages 1–3. Sign up to view the full content.
1The UniversityOf MichiganAndrew M. MorganEECS402 Lecture 01Savitch Ch. 2C++ BasicsFlow Of ControlEECS402EECS402Andrew M Morgan2Identifiers•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.nameEECS402EECS402Andrew M Morgan3Variables•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)EECS402EECS402Andrew M Morgan4Using 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 wordsEECS402EECS402Andrew M Morgan5Declaring Constants•Constants must have a name and a type•The value of a constant mustbe 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 variablesEECS402EECS402Andrew M Morgan6Some 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
has intentionally blurred sections.
Sign up to view the full version.