Please send questions to
st10@humboldt.edu .
parameter variables are one kind of variable;
double disk_area(double radius)
another kind of variable declaration: a NAMED CONSTANT
const
= expression ASSIGNMENT to that expression
for a declaration statement that declares a named constant:
const <type> <DESIRED_NAME> = <expression> ;
const double PI = 3.14159;
const int TSPS_PER_TBL = 3;
130 STYLE STANDARD: NO MAGIC NUMBERS (no "bare" literals)
130 STYLE STANDARD: named constants are to be written in all-uppercase
180 + (.04 * attendees(ticket_price))
FIXED_SHOW_COST + (PER_ATTENDEE_COST * attendees(ticket_price))
* named constants CAN make code easier to read
* named constants CAN make code easier to MAINTAIN and CHANGE
Where can these declarations go?
* in the header (.h) file: that's where funct_play2 puts them;
* inside the function body;
SCOPE
IN LECTURE EXERCISE:
1. write a reasonable named constant declaration for the fixed cost of
a performance of $180
2. write a reasonable named constant declaration for the cost per
attendee of 4 cents
3. write a reasonable named constant declaration for the base
attendance when the ticket price is $5 (that is, for 120)
4. write a reasonable named constant declaration for the base
ticket price of $5
ONLY RESPONSIBLE for SECTIONS 1- 3 in HTDP packet for Exam #1
----- this is material now for Exam 2!
there are 4 basic structures in programming! (some will say 3...)
sequence: statements that are one after the other are done one
after the other;
procedure: when you call a function, you know it will come back
Today, we're setting up for the 3rd basic structure:
BRANCHING
(later: after section 5 --- we'll have the 4th basic structure:
REPETITION)
STEP ONE: BOOLEANS and RELATIONS
bool true false
1 is an example of an int literal
2.0 is an example of a double literal
true is an example of a bool literal
logical expressions
boolean expressions
and, relational expressions
Start with relational expressions:
relational operators:
< 3 < 8
>
<= 3 <= 8 is true,
3 <= 2 is false,
3 <= 3 is true
>=
== see if two things are EQUAL!!!!!
3 == 3
!= not equal
4 != 3
(a + b - c) == (d - a)
IN CLASS EXERCISE #5
5. write a relational expression that is true if a parameter variable
age is less than or equal to 12
can write functions that return a boolean!
is_child
// contract: is_child: int -> bool
bool is_child(int age)
// purpose: if a person's <age> is 12 or less, return true, else
// return false.
//
// examples: is_child(11) should return true
// is_child(12) should return true
// is_child(13) should return false
bool is_child(int age)
{
const int CHILD_LIMIT = 12;
return (age <= CHILD_LIMIT);
}
consider: 70 <= grade <= 100
WON'T WORK in C++!
70 <= grade is true or false,
then true or false is compared to <= 100...!
Need: 70 <= grade
and
grade <= 100
I need BOOLEAN operators:
and, or, not
&& || !
(70 <= grade) && (grade <= 100)
(and drew TRUTH TABLES for and, or, not on the chalkboard!)