320 likes | 377 Views
Announcements. Please read all of Chapter 1 in the text comments operators variable types libraries if statement and comparisons. Keywords vs. myWords. Keywords are C++ defined: int main char double while cin cout many more. variables, file names are your choice Convention:
E N D
Announcements Please read all of Chapter 1 in the text • comments • operators • variable types • libraries • if statement and comparisons
Keywords vs. myWords • Keywords are C++ defined: • int • main • char • double • while • cin • cout • many more • variables, file names are your choice • Convention: • lowerUpper
Variables Variables are used to set and change values, which represent quantities, text, or states.
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”)
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”
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
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
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
"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
“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
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”).
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
chars and strings #include <iostream> #include <string> 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; }
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?)
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.
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
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;
The "if..else" statement if ( a == b ) { // do this } else if ( a < b ) { // do this } else { // do that } conditionals
"logical" comparisons if ( a == b ) if ( a != b ) if ( a < b ) if ( a > b ) if ( a <= b ) if ( a >= b )
demo #include <iostream> #include <string> using namespace std; int main( ) { int x = 0; string displayText = " "; if ( x == 0 ) // comment - this section runs if x is 0 { cout << "first brackets *****" << endl; displayText = "X is ZERO"; } else // comment - this section runs if x is NOT 0 { cout << "second brackets !!!!!" << endl; displayText = "X is NOT ZERO"; } cout << displayText << endl; cout << x << endl; } // end main
a simple menu #include <iostream>using namespace std;int main( ){int looper = 0;int operatorChoice = 0;while (looper == 0) // loop forever { cout << "*****************" << endl; cout << "* *" << endl; cout << "* 1. process *" << endl; cout << "* 2. quit *" << endl; cout << "* *" << endl; cout << "*****************" << endl; cout << "enter a choice: " ; cin >> operatorChoice; if (operatorChoice == 1) { cout << "Processing here" <<endl; } else if (operatorChoice == 2) { cout << "quitting" << endl; return 0; } else { cout << "nothing to do..." << endl; } } // end while (looper == 0) } // end main
Legality Variable value and variable type must be the same. Deterministic behavior - All computers yield the same result with the same variables
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
Compilation will it work?
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
Illegal #1 – mis-defined variables int y; y = 14.0001; char q; q = "abcdef"; double z; z = "hello";
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.
Illegal #2 – mixing variables in same equation int x; double y; char z; x = 12; y = 16.001; z = x + y;
Illegal #3 - Operation wrong for variable type: char Q, R, S; Q = R / S; // remember "/" is divide