looping and counting lecture 3 hartmut kaiser [email protected]

31
Looping and Counting Lecture 3 Hartmut Kaiser [email protected] http://www.cct.lsu.edu/˜hkaiser/spring_2015/csc1254.html

Upload: caitlin-short

Post on 04-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Getting Started

Looping and CountingLecture 3Hartmut [email protected]://www.cct.lsu.edu/hkaiser/spring_2015/csc1254.html

AbstractFirst well discuss types and type safety.Then we will modify the program we developed last time (Framing a Name) to make it more flexible. We will touch on arithmetic expressions, looping constructs and conditions. In addition we will talk about loop invariants and counting.1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting22Types C++ provides a set of typesE.g. bool, char, int, doubleCalled built-in typesC++ programmers can define new typesCalled user-defined typesWe'll get to that eventuallyThe C++ standard library provides a set of typesE.g. string, vector, complexTechnically, these are user-defined types they are built using only facilities available to every user1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting3Types and LiteralsBuilt-in typesBoolean typeboolCharacter typescharInteger typesintand short and longFloating-point typesdoubleand floatStandard-library typesstd::stringstd::complexBoolean literalstrue, falseCharacter literals'a', 'x', '4', '\n', '$'Integer literals0, 1, 123, -6, 0x34, 0xa3Floating point literals1.2, 13.345, .3, -0.54, 1.2e3F, 3F, .3FString literals: "asdf", "Howdy, all y'all!"Complex literalsstd::complex(12.3,99)std:: complex(1.3F)1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting4Types and Value Rangesintshortlongdoublefloatcharstring4 bytes: -231+231-12 bytes: -215+215-18 bytes: -263+263-18 bytes: -1.8e+308 1.8e+308, 15 digits4 bytes: -3.4e+38 3.4e+38, 6 digits1 byte: -27+27-1Arbitrary length character sequence

1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting5Types and ObjectsA typeDefines a set of values and a set of operations (for an object)An objectIs some memory that holds a value of a given typeA valueIs a set of bits in memory interpreted according to a typeA variableIs a named objectA declarationIs a statement giving a name to an objectA definitionIs a declaration that sets aside memory for an object1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting6Definition and Initializationint a = 7;

int b = 9;

char c = 'a';

double x = 1.2;

std::string s1 = "Hello";

std::string s2 = "1.2";

1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting779'a'1.25 | "Hello"3 | "1.2"a:b:c:x:s1:s2:ObjectsAn object is some memory that can hold a value (instance) of a given typeA variable is a named objectA declaration names an object1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting8int a = 7;char c = 'x';complex z(1.0,2.0);string s = "qwerty";7a:"qwerty"6 s:'x'c:1.0 2.0z:Type safetyLanguage rule: type safetyEvery object will be used only according to its typeA variable will be used only after it has been initializedOnly operations defined for the variable's declared type will be appliedEvery operation defined for a variable leaves the variable with a valid valueIdeal: static type safetyA program that violates type safety will not compileThe compiler reports every violation (in an ideal system)Ideal: dynamic type safetyIf you write a program that violates type safety it will be detected at run timeSome code (typically "the run-time system") detects every violation not found by the compiler (in an ideal system)1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting9Type safetyType safety is a very big dealTry very hard not to violate itwhen you program, the compiler is your best friendBut it wont feel like that when it rejects code youre sure is correctC++ is not (completely) statically type safeNo widely-used language is (completely) statically type safeBeing completely statically type safe may interfere with your ability to express ideasC++ is not (completely) dynamically type safeMany languages are dynamically type safeBeing completely dynamically type safe may interfere with the ability to express ideas and often makes generated code bigger and/or slowerMost of what youll be taught here is type safeWell specifically mention anything that is not1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting10The ProblemGiven the following interaction:

Please enter your name: John

We would like to print:

******************** ** Hello John! ** ********************1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting11The ProblemProblems with our SolutionEach printed line has its own variable associatedEven simple change to format requires rewriteSolution to generate each character separatelyNo need to store strings1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting12Overall Structure// Ask a persons name, greet the person#include #include

int main(){ // ask for the persons name std::cout > first_name;

// build the message we intend to write std::string const greeting = "Hello, " + first_name + "!";

// we have to rewrite this part...

return 0;}1/22/2015, Lecture 3CSC 1254, Spring 2015, Looping and Counting13Overall StructureWriting an unknown number of rows

int const line_pad = 1; // number of blank lines surrounding greetingint const column_pad = 2; // number of blank columns surrounding greeting

// total number of rows to writeint const rows = line_pad * 2 + 3;

// write 'rows' rows of outputint r = 0;

// invariant: we have written 'r' rows of outputwhile (r != rows) { // ... write a row of output ...

std::cout