Download presentation
Presentation is loading. Please wait.
1
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm
2
2 Write programs in source code; preprocess, compile, and link them to get executable file we can run. Syntactic errors appear at compile-time; semantic errors (bugs) appear at run- time. Use test plan to to find bugs. Review of Last Week
3
3 Identifiers are names used in programs E.g., main, number, length Keywords can't be used as identifiers E.g., int, float, double A variable is an identifier that holds a value that can change. We initialize a variable when creating it; we can use an assignment to change its values. Review of Last Week
4
4 Can use letters: Remember that C++ is case sensitive (e.g., NumWidgets is not the same as numwidgets) Can use digits 0-9, and underscore Cannot start with a digit Cannot contain spaces or other characters Typical maximum of 32 characters Cannot use C++ keywords Names for Variables
5
5 Should use a meaningful, descriptive name so that variable’s use is easily understood: Examples: counter, second, minute, length, width Be consistent with case; usually lower case with upper case for second part of variable name. Examples: averageRainfall, totalStudentGrades, maxBuildingHeight, minPackageWeight; Naming Variables (Cont)
6
6 Scope: Area of a program within which a variable can be referenced Variable definitions are recognized in the curly braces in which they were defined "Global" Variables declared outside of functions are recognized from the point declaration through the rest of the program Variable Scope
7
7 Write a program that declares 2 integer variables, 1 double, 1 character, and 3 floats. 5 minutes: Go! Try This!
8
8 int main() { int buildingHeight, buildingLength; double averageHeight; char firstLetter; float avgLength, avgSalary,avgHours; return(0); }
9
9 Named Constant: An identifier that is initialized to a value that cannot change Usually declared at top of program using keyword const Standard naming of constants is to use all upper case letter with or without underscore between words All constants must be initialized Syntax: const int MAXHT = 100; Named Constants
10
10 Easier to understand Easier to modify Example: Compare using 5000 in program versus constant MAXHT Advantages of Constants
11
11 const int MAXHT = 100; main() {... currentHeight > MAXHT...... bridgeHeight == MAXHT...... bridgeHeight + newAddition >= MAXHT... // used MAXHT 223 times in this program } Named Constants
12
12 Constants whose values are already known: Characters (specified Inside Single Quotes): 'A', 'a', '5', ' ', '\n' (newline), '\0' (NULL Character) Integers: 10, 1345, -34 Float or double: 2.3, -45.18, 10.6e6 String (Specified Inside Double Quotes): "HELLO", "What a great deal.", "5" Literal Constants (Values)
13
13 Also known as I/O Output stream: cout << variable; Input stream: cin >> variable; New line: endl All C++ statements end in semicolon (;) Input and Output
14
14 #include using namespace std; int main() { int numEntered; cout << "Please enter an integer:"; cin >> numEntered; cout << "Thanks, you entered: " << numEntered << "." << endl; return 0; } Input and Output Example
15
15 Standard Template Library (STL): Library of C++ functions used worldwide string type is NOT a primitive type in C++ string type is defined in STL Characters and Strings
16
16 To Use STL Strings: #include using namespace std; string lastName; Characters and Strings
17
17 Declaration: string yourName; Assigning a value to a string: yourName = "A. Goose"; String constants (string values) must be enclosed in double quotes. STL Strings
18
18 #include using namespace std; int main() { string lastName; cout << "Enter your last name: "; cin >> lastName; cout << "Your last name is: " << lastName << endl; return(0); } Input String Example
19
19 Expressions Expression: A sequence of one or more operands and operators that evaluates to a value Operator: A symbol expressing a way to modify a value or values (e.g., + for addition) Operand: A value being used by an operator
20
20 Expressions Example Expressions: 5 currentHeight currentHeight + 10 sqrt(3.14159)
21
21 Arithmetic Expressions Standard arithmetic operations can be performed: addition, subtraction, multiplication, division Standard arithmetic operators can be used for these operations: +, -, *, /
22
22 Arithmetic Expressions Others operators include % - "Modulo (Mod)" – Remainder after Integer Division -- (two hyphens) Decrement (subtract 1) ++ Increment (Add 1)
23
23 Order of Operations Precedence: Level of importance of operations Multiplicative operators have higher precedence than additive operators: Higher: *, /, % Lower: +, -
24
24 Order of Operations Associativity: Order of operation for equal level precedence. Most operators have left-to-right associativity Use parentheses to force different precedence for operations
25
25 Know for the Quiz All terms (underlined items) Also add: Variables, Identifiers, Keywords Variable declaration, initialization, and assignment, constant declaration Expressions, operators Input (cin) and Output (cout)
26
26 Be Ready! Quiz 1 Next Week
27
27 Today’s Challenge Imagine you are in a room with 3 switches. In an adjacent room there are 3 bulbs (all are off at the moment), each switch belongs to one bulb. It is impossible to see from one room to another. How can you find out which switch belongs to which bulb, if you may enter the room with the bulbs only once?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.