CSC 221: Computer Programming I Fall 2001  conditional execution  if statement  Boolean expressions, comparison operators  if-else variant  cascading.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Computer Science 1620 Loops.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Computer Science 1620 Function Scope & Global Variables.
Objectives You should be able to describe:
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Announcements HW1 grades are announced at SUCourse You may see Belal Amro at his office hour for homework grades at FENS 2014 on Wednesday 10:40-12:30.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
CPS120: Introduction to Computer Science
Flow of Control Part 1: Selection
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
1 CSC 222: Computer Programming II Spring 2004  Review of C++ basics  program structure, input, output  variables, expressions, functions, parameters.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Calvin College Controlling Behavior The if, switch and for Statements.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 Chapter 9 Scope, Lifetime, and More on Functions.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
A First Book of C++ Chapter 4 Selection.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 9 Scope, Lifetime, and More on Functions
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
Objectives You should be able to describe: The while Statement
Fundamental Programming
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Presentation transcript:

CSC 221: Computer Programming I Fall 2001  conditional execution  if statement  Boolean expressions, comparison operators  if-else variant  cascading if-else, nested conditionals  variable scope and lifetime  if statements & blocks  local vs. global variables

change example revisited // change.cpp Dave Reed 9/20/01 ///////////////////////////////////////////////////////// #include using namespace std; int main() { int amount; cout << "Enter an amount (in cents) to make change: "; cin >> amount; int quarters = amount/25; amount = amount%25; int dimes = amount/10; amount = amount%10; int nickels = amount/5; amount = amount%5; int pennies = amount; cout << "Optimal change:" << endl; cout << setw(4) << quarters << " quarters" << endl; cout << setw(4) << dimes << " dimes" << endl; cout << setw(4) << nickels << " nickels" << endl; cout << setw(4) << pennies << " pennies" << endl; return 0; }  undesirable feature Optimal change: 3 quarters 0 dimes 0 nickels 2 pennies  would like to avoid displaying a coin type when number is zero

conditional execution up to this point, all code has executed "unconditionally"  main contains a sequence of statements that are executed in order (with possible jumps to functions – but still predictable) examples like the change display require "conditional execution"  execute a statement or statements ONLY IF certain conditions hold conditional execution in C++ is accomplished via an if statement if (BOOLEAN_TEST) { // if TEST evaluates to true STATEMENTS; // then statements inside { } are executed } // otherwise, skip past } and continue if (quarters > 0) { cout << setw(4) << quarters << " quarters" << endl; }

if statements Boolean expressions involve comparisons between values comparison operators: == equal to != not equal to < less than <= less than or equal to > greater than > = greater than or equal to if (firstName == "Dave") { cout << "Hi Dave, how are you?" << endl; } if (firstName != "Dave") { cout << "Imposter! Who are you?" << endl; } if (grade >= 90) { letterGrade = "A"; } NOTE: code inside the "body" of an if statement is executed: once (if test is true) OR never (if test is false)

change example // change.cpp Dave Reed 9/20/01 ///////////////////////////////////////////////////////// #include using namespace std; int main() { // PROMPT USER & ASSIGN VARIABLES AS BEFORE cout << "Optimal change:" << endl; if (quarters > 0) { cout << setw(4) << quarters << " quarters" << endl; } if (dimes > 0) { cout << setw(4) << dimes << " dimes" << endl; } if (nickels > 0) { cout << setw(4) << nickels << " nickels" << endl; } if (pennies > 0) { cout << setw(4) << pennies << " pennies" << endl; } return 0; }  could have an if statement for each coin type for each coin type: if # of coins > 0, then display the message  note similarity of each if statement – can we generalize using a function?

Generalizing the conditional can define a general function for displaying a number of coins parameters: numCoins the number of coins (e.g., 2) coinType the type of coin (e.g., "nickels") void DisplayCoins(int numCoins, string coinType) // Assumes: numCoins >= 0, coinType is a type of coin (e.g., "nickels") // Results: displays a message if numCoins > 0 { if (numCoins > 0) { cout << setw(4) << numCoins << " " << coinType << endl; }. DisplayCoins(2, "dimes");  2 dimes DisplayCoins(4, "pennies");  4 pennies

change example // change.cpp Dave Reed 9/20/01 ///////////////////////////////////////////////////////// #include using namespace std; void DisplayCoins(int numCoins, string coinType); int main() { // PROMPT USER & ASSIGN VARIABLES AS BEFORE cout << "Optimal change:" << endl; DisplayCoins(quarters, "quarters"); DisplayCoins(dimes, "dimes"); DisplayCoins(nickels, "nickels"); DisplayCoins(pennies, "pennies"); return 0; } /////////////////////////////////////////////////////////// void DisplayCoins(int numCoins, string coinType) // Assumes: numCoins >= 0, coinType is a type of coin (e.g., nickel) // Results: displays a message if numCoins > 0 { if (numCoins > 0) { cout << setw(4) << numCoins << " " << coinType << endl; }  function serves to simplify main  makes it easier to change the message (while maintaining consistency) suppose we wanted output lines of the form: quarters: 1 dimes: 2 nickels: 1 pennies: 3

danger! do not confuse = with == = is the assignment operator, used for assigning values to variables == is the equality operator, used for comparing values in Boolean expressions common mistake: if (grade = 100) { cout << "Fantastic job!" << endl; } unfortunately, this will NOT be caught as a syntax error by the compiler obscure fact #1: assignments return a value (the value being assigned) obscure fact #2: in a Boolean expression, any nonzero value is considered true in this case, (grade = 100)  100  true, so message is displayed! to avoid confusion read = as GETS x = 0;  "x gets 0" read == as IS EQUAL TO if (x == 0){…}  "if x is equal to 0 …"

two-way conditionals a single if statement represents a one-way conditional  either executes the statements or don't, based on some test many applications call for two-way conditionals  either do this or do that, based on some test e.g., find the maximum or minimum of two values assign a Pass/Fail grade display a greeting personalized for "Dave" two-way conditionals are defined via the if-else variant if (BOOLEAN_TEST) { // if TEST evaluates to true, STATEMENTS_IF_TRUE; // then execute these statements } // else { // otherwise (if TEST is false), STATEMENTS_IF_FALSE; // execute these statements } //

if-else examples double max(double num1, double num2) // Assumes: nothing // Returns: the larger of num1 and num2 { if (num1 > num2) { return num1; } else { return num2; } double min(double num1, double num2) // Assumes: nothing // Returns: the smaller of num1 and num2 { if (num1 < num2) { return num1; } else { return num2; } note: can have more than one return statement in a function  function ends when first return statement is encountered (& value is returned) do these functions work when num1 == num2 ? double grade; cout << "Enter your grade: "; cin >> grade; if (grade >= 60.0) { cout << "You pass!" << endl; } else { cout << "You fail!" << endl; } string firstName; cout << "Enter your name: "; cin >> firstName; if (firstName == "Dave") { cout << "Hi Dave, what's up?" << endl; } else { cout << "Who are you?" << endl; }

temperature conversion: problem? #include #include "convert.cpp"// contains FahrToCelsius function using namespace std; double max(double num1, double num2); double min(double num1, double num2); int main() { double lowTemp, highTemp; cout << "Enter the forecasted low and high (in Fahrenheit): "; cin >> lowTemp >> highTemp; lowTemp = min(lowTemp, highTemp); highTemp = max(lowTemp, highTemp); cout << endl <<"The forecasted low (in Celsius): " << FahrToCelsius(lowTemp) << endl <<"The forecasted high (in Celsius): " << FahrToCelsius(highTemp) << endl; return 0; } //////////////////////////////////////////////////////////////////////// // max AND min FUNCTION DEFINITIONS AS BEFORE would like to react if user enters temperatures in wrong order will this code work?

temperature conversion: one solution #include #include "convert.cpp"// contains FahrToCelsius function using namespace std; double max(double num1, double num2); double min(double num1, double num2); int main() { double lowTemp, highTemp; cout << "Enter the forecasted low and high (in Fahrenheit): "; cin >> lowTemp >> highTemp; double newLowTemp = min(lowTemp, highTemp); double newHighTemp = max(lowTemp, highTemp); cout << endl <<"The forecasted low (in Celsius): " << FahrToCelsius(newLowTemp) << endl <<"The forecasted high (in Celsius): " << FahrToCelsius(newHighTemp) << endl; return 0; } //////////////////////////////////////////////////////////////////////// // max AND min FUNCTION DEFINITIONS AS BEFORE have to avoid overwriting lowTemp in the first assignment one solution: use new variables

temperature conversion: another solution #include #include "convert.cpp"// contains FahrToCelsius function using namespace std; int main() { double lowTemp, highTemp; cout << "Enter the forecasted low and high (in Fahrenheit): "; cin >> lowTemp >> highTemp; if (lowTemp > highTemp) { double tempValue = lowTemp; lowTemp = highTemp; highTemp = tempValue; } cout << endl <<"The forecasted low (in Celsius): " << FahrToCelsius(lowTemp) << endl <<"The forecasted high (in Celsius): " << FahrToCelsius(highTemp) << endl; return 0; } if backwards, swap the values in the two variables note: need temporary storage

multi-way conditionals many applications call for more than two alternatives e.g., find max or min of three or more values assign a letter grade (A, B+, B, C+, C, D, or F) determine best pizza value (large, medium, or toss-up) can handle multiple alternatives by nesting if-else's if (TEST1) { ALTERNATIVE1; ALTERNATIVE1; } else { else if (TEST2) { if (TEST2) { ALTERNATIVE2; ALTERNATIVE2;} }else { else { ALTERNATIVE3; ALTERNATIVE3;} } or more succinctly:

multi-way conditionals such a multi-way conditional is known as a "cascading if-else"  control cascades down the structure like water down a waterfall if (TEST1) {// if TEST1 is true, ALTERNATIVE1;// execute ALTERNATIVE1 and exit }// else if (TEST2) {// otherwise, if TEST2 is true, ALTERNATIVE2;// execute ALTERNATIVE2 and exit }// else if (TEST3) {// otherwise, if TEST3 is true, ALTERNATIVE3;// execute ALTERNATIVE3 and exit }//.//.// keep working way down until a.// TEST evaluates to true.// else {// if no TEST succeeded, DEFAULT_ALTERNATIVE;// execute DEFAULT_ALTERNATIVE }

comparison shopping #include using namespace std; const double PI = ; double circleArea(double radius); int main() { double largeDiameter, largeCost, mediumDiameter, mediumCost; cout << "Enter the diameter and cost of a large pizza: "; cin >> largeDiameter >> largeCost; cout << "Enter the diameter and cost of a medium pizza: "; cin >> mediumDiameter >> mediumCost; double largeCostPerSqInch = largeCost/circleArea(largeDiameter/2); double mediumCostPerSqInch = mediumCost/circleArea(mediumDiameter/2); if (largeCostPerSqInch < mediumCostPerSqInch) { cout << "The large is the better deal. " << endl; } else if (largeCostPerSqInch > mediumCostPerSqInch) { cout << "The medium is the better deal. " << endl; } else { cout << "They are equivalently priced." << endl; } return 0; } ////////////////////////////////////////////////////////////////// // circleArea FUNCTION DEFINITION AS BEFORE 3 alternatives 1.large cheaper 2.medium cheaper 3.same cost can use cascading if-else to handle each of the alternatives

making the grade // grades.cpp Dave Reed 9/20/01 /////////////////////////////////////// #include using namespace std; const double A_CUTOFF = 90.0; const double B_PLUS_CUTOFF = 87.0; const double B_CUTOFF = 80.0; const double C_PLUS_CUTOFF = 77.0; const double C_CUTOFF = 70.0; const double D_CUTOFF = 60.0; int main() { double grade; cout << "Enter your grade: "; cin >> grade; string letterGrade; if (grade >= A_CUTOFF) { letterGrade = "A"; } else if (grade >= B_PLUS_CUTOFF) { letterGrade = "B+"; } else if (grade >= B_CUTOFF) { letterGrade = "B"; } else if (grade >= C_PLUS_CUTOFF) { letterGrade = "C+"; } else if (grade >= C_CUTOFF) { letterGrade = "C"; } else if (grade >= D_CUTOFF) { letterGrade = "D"; } else { letterGrade = "F"; } cout << "You are guaranteed at least a " << letterGrade << "." << endl; return 0; }

change example re-revisited still one annoying feature of change example: all coin types are plural Enter an amount (in cents) to make change: 85 Optimal change: 3 quarters 1 dimes void DisplayCoins(int numCoins, string coinType) { if (numCoins == 1) { cout << setw(4) << numCoins << " " << coinType << endl; } else if (numCoins > 1) { cout << setw(4) << numCoins << " " << coinType << "s" << endl; }. DisplayCoins(1, "dime");  1 dime DisplayCoins(2, "dime");  2 dimes would like to modify DisplayCoins to display correct plurality  if all coin names were made plural by adding 's', this would be easy note: can have cascading if-else with no else at the end – so it's possible to do nothing

handling plurality unfortunately, "penny" and "pennies" ruin this approach barring a general rule for how to turn singular names into plural, both must be specified when calling DisplayCoins void DisplayCoins(int numCoins, string singleCoin, string pluralCoin) { if (numCoins == 1) { cout << setw(4) << numCoins << " " << singleCoin << endl; } else if (numCoins > 1) { cout << setw(4) << numCoins << " " << pluralCoin << endl; }. DisplayCoins(1, "penny", "pennies");  1 penny DisplayCoins(3, "penny", "pennies");  3 pennies in general: every property that can vary within the function must be specified as a parameter

structuring conditionals there are often numerous ways to structure conditionals  clarity is the first priority  all else being equal, avoid redundancy void DisplayCoins(int numCoins, string singleCoin, string pluralCoin) { if (numCoins > 0) { cout << setw(4) << numCoins << " "; if (numCoins == 1) { cout << singleCoin << endl; } else { cout << pluralCoin << endl; } void DisplayCoins(int numCoins, string singleCoin, string pluralCoin) { if (numCoins > 0) { if (numCoins == 1) { cout << setw(4) << numCoins << " " << singleCoin << endl; } else { cout << setw(4) << numCoins << " " << pluralCoin << endl; } nested if-else inside of if more efficient? nested if-else inside of if factored out common code

final change #include using namespace std; void DisplayCoins(int numCoins, string singleCoin, string pluralCoin); int main() { // PROMPT USER & ASSIGN VARIABLES AS BEFORE cout << "Optimal change:" << endl; DisplayCoins(quarters, "quarter", "quarters"); DisplayCoins(dimes, "dime", "dimes"); DisplayCoins(nickels, "nickel", "nickels"); DisplayCoins(pennies, "penny", "pennies"); return 0; } /////////////////////////////////////////////////////////// void DisplayCoins(int numCoins, string singleCoin, string pluralCoin) // Assumes: numCoins >= 0, singleCoin is the name of a single coin // (e.g., penny), and pluralCoin is the plural (e.g., pennies) // Results: displays a message if numCoins > 0 { if (numCoins == 1) { cout << setw(4) << numCoins << " " << singleCoin << endl; } else if (numCoins > 1) { cout << setw(4) << numCoins << " " << pluralCoin << endl; }  opted for first version of function – seems the clearest

variable scope & lifetime in addition to name and value, variables have additional properties scope: that part of the program in which the variable exists lifetime: the time during execution at which the variable exists for variables declared in a function (also parameters): scope: from the point at which they are declared to the end of the function lifetime: from the point in execution where the declaration is reached, until the point where the function terminates int main() { int x; cin >> x; | scope | of string str = "foo"; ---+ | x | scope |... | of | | str | return 0; | | } scope is a spatial property lifetime is a temporal property

variable scope & lifetime (cont.) constants declared outside of a function are said to be "global" scope: the entire program lifetime: the entire execution of the program #include using namespace std; int HIGH = 100; int Compute(int x); int main() { cout << HIGH << endl; cout << Compute(22) << endl; cout << HIGH << endl; return 0; } //////////////////////////// int Compute(int x) { HIGH = HIGH – 1; return x – HIGH; } you can declare global variables, but don't!  want to be able to call a function and know it doesn't screw anything up  if a function has access to global variables and can change them, who knows? global constants are safe since the compiler ensures that no function can change them declare variables local to the function where they are needed  if another function needs access, then pass the value as a parameter

if statements & blocks the body of an if statement is known as a "block"  a variable declared inside a block has scope & lifetime limited to that block #include #include "convert.cpp"// contains FahrToCelsius function using namespace std; int main() { double lowTemp, highTemp; cout << "Enter... "; cin >> lowTemp >> highTemp; if (lowTemp > highTemp) { double tempValue = lowTemp; lowTemp = highTemp; highTemp = tempValue; } cout << endl <<"The forecasted low (in Celsius): " << FahrToCelsius(lowTemp) << endl <<"The forecasted high (in Celsius): " << FahrToCelsius(highTemp) << endl; return 0; } should make variable declarations as "local" as possible – more efficient & cleaner  memory is only allocated if the declaration is reached (here, if lowTemp > highTemp)  memory is freed up as soon as the end of block is reached (here, at end of if statement)  if desired, can reuse same variable names in different blocks

design example for temperatures in the range –35 o to 45 o Fahrenheit, the wind-chill factor is defined as follows where a = 91.4, b = 10.45, c = 6.69, d = 0.447, and e = want program to:  prompt user for temperature and wind speed  variables, cout for prompts, cin to read values  if temperature is within valid range, compute and display wind-chill  global constants for a, b, c, d, and e  function to compute the wind chill parameters for temperature and wind speed, if-else based on wind speed  if statement to determine if temperature is valid if temp is too low or too high, display error message otherwise, call function to compute wind-chill

main structure // chill.cpp Dave Reed 9/20/01 ///////////////////////////////////////////////////////// #include using namespace std; const double A = 91.4; const double B = 10.45; const double C = 6.69; const double D = 0.447; const double E = 22.0; double WindChill(double temp, double wind); int main() { double temperature; cout << "Enter the temperature (in degrees Fahrenheit): "; cin >> temperature; if (temperature < -35) { cout << "That temperature is too low. " << "Wind-chill is not defined." << endl; } else if (temperature > 45) { cout << "That temperature is too high. " << "Wind-chill is not defined." << endl; } else { int windSpeed; cout << "Enter the wind speed (in miles per hour): "; cin >> windSpeed; cout << endl << "The wind-chill factor is " << WindChill(temperature, windSpeed) << endl; } return 0; } /////////////////////////////////////////////////////////////// constants are obvious: may determine more precision WindChill abstraction is useful: well-defined and easy to separate can define main code before implementing details of WindChill

WindChill function //////////////////////////////////////////////////////////// double WindChill(double temp, double wind) // Assumes: -35 = 0 // Returns: wind-chill factor given temperature and wind speed { if (wind < 4) { return temp; } else { return A - (B + C*sqrt(wind) - D*wind)*(A - temp)/E; } note: WindChill comments specify that temp is assumed to be in range if called with invalid temp, the function makes no guarantees in general, when designing a program: 1.understand the problem and the desired behavior 2.identify inputs, possible constants, useful abstractions (functions), … 3.implement main first, leaving functions blank (or dummy code) 4.implement functions last, testing as you go

conditionals summary if statements are used for conditional execution  one-way conditional: if statement  two-way conditional: if-else statement  multi-way conditional: cascading if-else (alternative notation described in text: switch) if statements are driven by Boolean (true/false) expressions  comparison operators: == != >= the body (inside curly braces) of an if-statement defines a new scope  variables defined inside the body are local to that environment  saves the allocation of unneeded variables  allows for reuse of variable names and space