1 / 32

Announcements

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:

ulf
Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements Please read all of Chapter 1 in the text • comments • operators • variable types • libraries • if statement and comparisons

  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. Variables Variables are used to set and change values, which represent quantities, text, or states.

  4. 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”)

  5. 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”

  6. 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

  7. 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

  8. 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

  9. "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

  10. “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

  11. 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”).

  12. 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

  13. 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; }

  14. 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?)

  15. 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.

  16. 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

  17. 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;

  18. The "if..else" statement if ( a == b ) { // do this } else if ( a < b ) { // do this } else { // do that } conditionals

  19. "logical" comparisons if ( a == b ) if ( a != b ) if ( a < b ) if ( a > b ) if ( a <= b ) if ( a >= b )

  20. 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

  21. 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

  22. Legality Variable value and variable type must be the same. Deterministic behavior - All computers yield the same result with the same variables

  23. 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

  24. Compilation will it work?

  25. 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

  26. Illegal #1 – mis-defined variables int y; y = 14.0001; char q; q = "abcdef"; double z; z = "hello";

  27. 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.

  28. Illegal #2 – mixing variables in same equation int x; double y; char z; x = 12; y = 16.001; z = x + y;

  29. Illegal #3 - Operation wrong for variable type: char Q, R, S; Q = R / S; // remember "/" is divide

More Related