Download presentation
Presentation is loading. Please wait.
1
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions using namespace std; // enables cout >
2
Keywords vs. myWords Keywords are C++ defined: – int – main – char – double – while – cin – cout – many more variables, file names are your choice Convention: – lowerUpper
3
cout - decimal precision cout only displays 6 decimal characters cout attempts to reduce output to save characters use "cout. precision( n ); " to set displayable values to n decimal characters
4
Scientific notation Try this: double x; x = 35.62 e 6; cout.scientific; cout << x << endl; cout.fixed; //eliminates scientific notation cout.precision(12); cout << x << endl;
5
The Assignment Statement “=” int main ( ) { int A; A = 4; A = A + 3; } NOT “A equals 4” instead: “A is assigned the value 4” semicolon, brackets, main… must be C++
6
Operators The usual: + - / * Precedence: use ( ) e.g. (y + b) * c not the same as y + b * c Negative numbers: - 8
7
Announcements Please read all of Chapter 1 in the text comments operators variable types libraries if statement and comparisons
8
Variables Variables are used to set and change values, which represent quantities, text, or states.
9
How do we use numbers? Counting Measuring Non-numerical designations: Finally, they can designate a “state” unrelated to the number (“1 if by land, 2 if by sea”)
10
So computers Must deal with each type of number in it’s own way Must store those numbers in their own way Must make those numbers available, though, through a common interface That interface, is the “VARIABLE”
11
How computers use info Numbers! Everything is a number! The letter “A” is stored as the number 65 The letter “B” is stored as the number 66 The color RED is stored in three numbers as 255 0 0
15
Variables x, y, z i, j, k totalCountOfNewEmployees any_sensible_name // usually for what // the variable holds They Stand for Something, can be named anything except reserved C++ words
16
Variable Types (primitive, common) Integers – meant to be a whole number (used for counting) C++: int Floating Pt. – meant to be a detailed number (used for measuring) – called a double Character – has no meaning other than to be read by a human – C++: char Strings of Characters – have no meaning other than to be read by a human: string – Not native to C++, but libraries exist to help… Boolean – reflects a state of true or false – C++: bool
17
"x is 14" Could mean 14 Could mean 13.999995 Could mean 14.000001 Could mean 20 (honestly), might not be base- 10, more later Could mean “Fourteen”, which has no meaning other than to be read by humans
18
“Typing” a variable – clarifies what it does and its precision int x; x = 14; Therefore, 14 means 14 double x; x = 14.0000; Therefore, x has meaning, 14 to 4 places
19
note Should be initialized at the declaration int x = 14; // type name = initValue; "typed" = declared Can only be “typed” once Value can be reset (that’s why it’s called a “variable”).
20
floating point (i.e. double)? 14.6567 128.1231 154367.001423 3.14159 987987465986543. “adequate precision” cout rounds numbers for space int x = 45678.34342; cout << x; will DISPLAY as 45678.3, although x will retain its value for calculations
21
once a variable is designed you can get it from the operator (if you want) int x = 1000000; x=0; cout << “enter x: ”; cin >> x;
22
chars and strings #include using namespace std; char Id = 'm'; // chars are single-quoted string name = "mikeb"; // strings are double- quotes int main( ) { cout << Id << endl; cout << name << endl; }
23
New Types Boolean – true or false (really means presence or absence), used to signify a condition, or “state” of being. the “bool” type can have values true or false (note: WITHIN the computer, false is zero, and true is any non-zero value). operations are OR and AND bool X, Y, Z; X = true; (same as = 1, means “X is present”) Y = false; (same as = 0, means “Y is NOT present”) Z = X OR Y; (what is Z?, i.e. does Z signify some presence?) Z = X AND Y; (what is Z?)
24
George Boole said that all life could be explained as a combination of things that were present or absent under certain conditions. Really, just a way of describing things with simple math symbols.
25
A smoke, water, burglar alarm Boolean variables W, B, S, A W is true if water B is true if burglar S is true if smoke A means “sound the alarm” A = W and B and S Does W and B and S make sense? A = W or B or S
26
The "if..else" statement if ( a == b ) { // do this } else if ( a < b ) { // do this } else { // do that } conditionals
27
"logical" comparisons if ( a == b ) if ( a != b ) if ( a < b ) if ( a > b ) if ( a <= b ) if ( a >= b )
28
if demo #include
29
a simple menu #include using namespace std; int main() { bool quit = false; int choice = 0; while ( quit == false ) { cout > choice; if (choice == 0) { quit = true; } else { cout << "loop again..." << endl; } } // end while return 0; } // end main Loop ( iteration ) conditional
30
Legality Variable value and variable type must be the same. Deterministic behavior - All computers yield the same result with the same variables
31
Computers DO NOT completely check our programs Called “Compilation” Must be successful before a program can “run” But not all errors are caught in compilation
32
So, Illegality is... “Compiler” Errors – however, some languages and compilers allow mis-designations of variables Indeterminate results “Run-time” Errors - only show up when you run the program
33
Illegal #1 – mis-defined variables int y; y = 14.0001; char q; q = "abcdef"; double z; z = "hello";
34
Why “Type” Variables Deterministic behavior of variables and the numbers they represent. New definition of “Legality” : We will never perform mathematical or logical operations on variables of different types.
35
Illegal #2 – mixing variables in same equation int x; double y; char z; x = 12; y = 16.001; z = x + y;
36
Illegal #3 - Operation wrong for variable type: char Q, R, S; Q = R / S; // remember "/" is divide
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.