lecture2 - Last Time: statements and execution variables:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 Last Time: statements and execution variables: the substitution model blocks and environments Today: * types * selection statements * Nature of a procedural abstraction * Declaration and Definition * Specification: Requires-Modifies-Effects * Recursion +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ When a variable is declared, we not only know it exists, we also know its *type*. A variable's type tells us two things: * the set of possible values that object may assume * the set of operations possible on/with that object C++ is a typed language: every variable has an associated type. The language comes with some types/operations already defined for you: The "integral" types (int), and obvious computations (+, -, *, /, . ..) and comparisons (<, >, ==) The "floating point" types (double) and even more math ops (standards plus arc, cos, tan, . ...) A boolean type (bool) plus logical operators (==, A string type plus operations. Unfortunately, C++ will let you mix types in various ways, and often won't warn you about dangerous combinations. The compiler sometimes complains about these combinations, but sometimes doesn't. The lesson? Don't mix types. For the most part, use "int" for integral types, and "double" for floating point types. Any competent C++ text/reference will tell you all the details about native types and operations---I won't bore you (or me) with the litany here. +++++++++++++++++++++++++++++++++++++++==
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sheet1 Page 2 So far, we've seen no way to *conditionally* compute something. This makes for pretty boring computations that do the same thing no matter what their input. A *selection* statement allows us to conditionally execute code. The most common selection statement is the "if" statement. Its general form is: if (<boolean-expression>) <consequence-statement> else <alternative-statement> The <boolean-expression> can be any expression that evaluates to a logical value. The consequence is executed only if the boolean expression evaluates to true. Otherwise, the alternative is executed. Boolean expressions: any comparison (<, ==, . ..) will result in either true or "false" and is a boolean expression. The language also happily converts most other types to boolean expressions for us: if the value is "0", the resulting boolean value is "false", otherwise it is true. It is possible to leave the "else clause" out entirely. So, the following is a valid if statement: if (x) x++ You might want to do more than one thing in either the consequence or alternative. You can use a statement block to do this For example (from Project 1): if (years < 1) { PrintErrorMessage() exit(1) } *********************************************************** ***********************************************************
Background image of page 2
Sheet1 Page 3 The mechanism we use to deal with complexity is *abstraction*. There are two principal means of abstraction: *procedural* abstraction and *data* abstraction. For the next three weeks, we'll be concerned
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

lecture2 - Last Time: statements and execution variables:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online