Bill Tucker Austin Community College COSC 1315 Chapter 4 Lecture Notes Bill Tucker Austin Community College COSC 1315 Copyright © 2002 W. A. Tucker Copyright © 2002 W. A. Tucker
Scope of Names All names have a scope that is either Global – the value stored in the memory location referred to by a global name is accessible for ALL of the program Local – the value stored in the memory location referred to by a local name is accessible ONLY for the function where the value is declared Copyright © 2002 W. A. Tucker
Example of Scope #include <iostream> using namespace std; double calcDistance (double); // prototype const double VELOCITY = 55; // global constant double height; // global variable int main() { double distance, time; time = 10; distance = calcDistance (time); cout << distance << endl; return 0; } double calcDistance (double time) double distance = 0; distance = VELOCITY * time; return distance; global constant OK global variable NONO local variables local variable NOTE: time and distance are local Copyright © 2002 W. A. Tucker
Tracing the values Values when the program is loaded GLOBAL main calcDistance VELOCITY 55.0 distance ??? Not loaded yet time ???t Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker
Tracing the values Values after execution of instruction “time = 10;” GLOBAL main calcDistance VELOCITY 55.0 distance ??? Not loaded yet time 10.0 Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker
Tracing the values Values after calling calcDistance (time); GLOBAL main calcDistance VELOCITY 55.0 distance ??? distance 0.0 time 10.0 time 10.0 Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker
Tracing the values Values after execution of instruction in calcDistance “distance = VELOCITY * time;” GLOBAL main calcDistance VELOCITY 55.0 distance ??? distance 550.0 time 10.0 time 10.0 Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker
Tracing the values Values after returning from calcDistance GLOBAL main calcDistance VELOCITY 55.0 distance 550.0 no longer loaded time 10.0 Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker
Style to Document Functions Since communication to and from a function is key, documentation is important Document the “contract” between the function and the calling function // PRE: preconditions – what is true when the function is called // POST: postconditions – what is true when the function finishes Copyright © 2002 W. A. Tucker
Example Documentation double calcDistance (double time) { // PRE: time contains a valid value // POST: returns a double value equal to the product of // time and VELOCITY double distance = 0; distance = VELOCITY * time; return distance; } // end calcDistance NOTE: The pre-conditions are the responsibility of the calling function The post-conditions are the responsibility of the new function Copyright © 2002 W. A. Tucker
Programming Structures Recall that we have only talked about SEQUENTIAL programming structures where each instruction is executed in sequence, each and every time the program is executed But what if we want to execute different instructions based upon the value of some variable that may change during execution? Copyright © 2002 W. A. Tucker
Selection Logic The SELECTION programming structure is used to alter the “flow of control” of instructions on a “real time” basis while the program is executing These decisions will be based upon the evaluation of a logical expression as either TRUE or FALSE Logical expressions can contain RELATIONAL or EQUALITY operators Copyright © 2002 W. A. Tucker
Relational Operators < Less than > Greater than <= Less than or equal to >= Greater than or equal to Which ones are compliments of each other ?? Copyright © 2002 W. A. Tucker
Equality Operators == Equal to != Not equal to Note these are compliments of each other Number one C++ programming error is to confuse equality operator (==) with assignment operator (=) Copyright © 2002 W. A. Tucker
Logical Expressions The data type of values that are compared should be the SAME or the compiler will make assumptions that you may not like! What is the value of the following logical expressions? Given: a = 24, b = 36, a and b are integers Expressions: a > b a == b a < b a != b a > = b a < = b Copyright © 2002 W. A. Tucker
Logical Operators There are three logical operators in C++ that are used to combine or modify logical expressions AND && OR || NOT ! Copyright © 2002 W. A. Tucker
AND Logical Operator expression1 && expression2 both expressions must be true for the combined expression to be true expression1 expression2 result true true true true false false false true false false false false Copyright © 2002 W. A. Tucker
OR Logical Operator expression1 || expression2 either expression must be true for the combined expression to be true expression1 expression2 result true true true true false true false true true false false false Copyright © 2002 W. A. Tucker
! (NOT) Operator ! expression after evaluating the “expression” the value of true or false is FLIPPED ! (a == b) is the same as (a != b) ! (condition1 && condition2) is the same as (condition1 || condition2) Copyright © 2002 W. A. Tucker
Logical Expression Examples What is the value (result) of the following logical expressions? Given: letter1 = ‘A’ letter2 = ‘a’ string1 = “Testing” string2 = “TESTING” value1 = 24.325 value2 = 34.555 bool raining = true letter1 == letter2 letter1 > letter2 string1 == string2 string1 > string2 value1 == value2 value1 > value2 raining ! raining Copyright © 2002 W. A. Tucker
More Examples What is the value (result) of the following logical expressions? Given: letter1 = ‘A’ letter2 = ‘a’ string1 = “Testing” string2 = “TESTING” value1 = 24.325 value2 = 34.555 bool raining = true (letter1 < letter2) && raining (string1 == string2) || ( value1 < value2) ((value1 == value2) || raining) && (value1 > value2) Copyright © 2002 W. A. Tucker
Selection Structure One of two possible alternatives is taken, depending upon a condition Condition? False True Alternative A Alternative B Copyright © 2002 W. A. Tucker
Selection Structure A selection structure may have only a single alternative - the true alternative Condition? True Alternative False Copyright © 2002 W. A. Tucker
if / else Statement The two alternative selection in C++ is coded by using an if / else statement false true if (a > b) largest = a; else largest = b; a > b ? largest = b largest = a NOTE: if and else are reserved words Copyright © 2002 W. A. Tucker
if Statement A single alternative selection is coded in C++ using the if statement largest = b largest = b; if (a > b) largest = a; a > b ? True largest = a False NOTE: if is a reserved word Copyright © 2002 W. A. Tucker
More on the if statement The if statement has two generic forms if (condition) a single statement or block of code; else Copyright © 2002 W. A. Tucker
Block of code A “block of code” is like a paragraph and is written inside a set of curly braces { } if (a > b) { largest = a; smallest = b; } // end if else largest = b; smallest = a; } // end else Copyright © 2002 W. A. Tucker
Nested if statements More than two alternatives may be coded by using “nested if statements” true Display “a is largest” a > b? if (a > b) cout << “a is largest”; else if (a < b) cout << “b is largest”; cout << “a = b”; false true false a < b? Display “b is largest” Display “a = b” Copyright © 2002 W. A. Tucker
Nested if statements More than two alternatives may be coded by using “nested if statements” false true a >= b? if (a >= b) if (a > b) cout << “a is largest”; else cout << “a = b”; cout << “b is largest”; Display “b is largest” false true a > b? Display “a = b” Display “a is largest” Copyright © 2002 W. A. Tucker
Common Errors An else always refers to the last if statement What is the output of the following code? (Try temp = 40, 50 and 60) if (temp < 60) if (temp <= 40) cout << “wear a jacket”; else cout << “no jacket required”; Does this make sense? What is wrong? Copyright © 2002 W. A. Tucker
Common Errors The following program fragment is supposed to print out the values 25, 60 and 8 in that order. Instead, it prints out 50, 60 and 4, why? length = 25; width = 60; if (length = 50) height = 4; else height = 8; cout << length << ‘ ‘ << width << ‘ ‘ << height << endl; Copyright © 2002 W. A. Tucker
In Class Exercise Write a function that will return a letter grade given an integer grade as input char convertGrade (int grade) { //PRE: grade is a positive value <= 100 //POST: returns a letter grade as follows: // 90 – 100 = A; 80 – 89 = B // 70 – 89 = C; 60 – 69 = D: <59 = F char letter; // PUT SELECTION LOGIC HERE return letter; } // convertGrade Copyright © 2002 W. A. Tucker
Common Algorithms Minimum (smallest) Maximum (largest) Swap Odd Even Average (will cover later) Copyright © 2002 W. A. Tucker