Friday, July 11, 2008

5. Branch and Loop Statements

5.1 Boolean Values, Expressions and Functions
In this lecture we will look more closely at branch and loop statements such as "for" and "while" loops and "if ... else" statements. All these constructs involve the evaluation of one or more logical (or "Boolean") expressions, and so we begin by looking at different ways to write such expressions.

As we have seen, in reality C++ represents "True" as the integer 1, and "False" as 0. However, expressions such as

condition1 == 1
or

condition2 == 0
aren't particularly clear - it would be better to be able to follow our intuition and write

condition1 == True
and

condition2 == False
Furthermore, it is desirable to have a separate type for variables such as "condition1", rather than having to declare them as of type "int". We can achieve all of this with a named enumeration:


enum Logical {False, True}

which is equivalent to


enum Logical {False = 0, True = 1}

This line acts a kind of type definition for a new data type "Logical", so that lower down the program we can add variable declarations such as:


Logical condition1, condition2;

Indeed, we can now use the identifier "Logical" in exactly the same way as we use the identifiers "int", "char", etc. In particular, we can write functions which return a value of type "Logical". The following example program takes a candidate's age and test score, and reports whether the candidate has passed the test. It uses the following criteria: candidates between 0 and 14 years old have a pass mark of 50%, 15 and 16 year olds have a pass mark of 55%, over 16's have a pass mark of 60%:


#include
using namespace std;

enum Logical {False, True};

Logical acceptable(int age, int score);

/* START OF MAIN PROGRAM */
int main()
{
int candidate_age, candidate_score;

cout << "Enter the candidate's age: "; cin >> candidate_age;
cout << "Enter the candidate's score: "; cin >> candidate_score;

if (acceptable(candidate_age, candidate_score))
cout << "This candidate passed the test.\n"; else cout << "This candidate failed the test.\n"; return 0; } /* END OF MAIN PROGRAM */ /* FUNCTION TO EVALUATE IF TEST SCORE IS ACCEPTABLE */ Logical acceptable(int age, int score) { if (age <= 14 && score >= 50)
return True;
else if (age <= 16 && score >= 55)
return True;
else if (score >= 60)
return True;
else
return False;
}
/*END OF FUNCTION */

Program 5.1.1
Note that since "True" and "False" are constants, it makes sense to declare them outside the scope of the main program, so that the type "Logical" can be used by every function in the file. An alternative way to write the above function "acceptable(...)" would be:


/* FUNCTION TO EVALUATE IF TEST SCORE IS ACCEPTABLE */
Logical acceptable(int age, int score)
{
Logical passed_test = False;

if (age <= 14 && score >= 50)
passed_test = True;
else if (age <= 16 && score >= 55)
passed_test = True;
else if (score >= 60)
passed_test = True;

return passed_test;
}
/*END OF FUNCTION */

Defining our own data types (even if for the moment they're just sub-types of "int") brings us another step closer to object-oriented programming, in which complex types of data structure (or classes of objects) can be defined, each with their associated libraries of operations.

Note: The Identifiers "true" and "false" in C++
Note that C++ implicitly includes the named enumeration

enum bool {false, true};

So you can't (re)define the all-lower-case constant identifiers "true" and "false" for yourself. In addition, you can use the type bool in the same way as we used Logical in our example.

0 comments: