Revised: February 4, 2007
By Richard G. Baldwin
File: Pf 00180.htm
Practice Text
This lesson was written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming. The lesson was written under the assumption that those students have no prior programming knowledge when they enroll in the course.
Another browser window
I recommend that you open another copy of this document in a separate browser window so that you can view the code and the discussion of that code at the same time.
A previous lesson entitled Expressions and Operators explained many of the operators used in C++, Java, and C#. However, that lesson did not explain the use of the relational and logical operators.
This lesson presents two programs that illustrate and explain the use of the following relational and logical operators:
Each of the programs is designed to solve the same problem. However, each of the programs uses a different formulation of the relational and logical operators to solve the problem.
The first program is named Operators01.cpp. A complete listing of this program is shown in Listing 5 near the end of the lesson.
This program illustrates the use of the following relational and logical operators:
Purpose of the program
Basically, the purpose of this program is to use relational and logical operators to determine if a character provided as input by the user falls within the range of characters from A through Z inclusive.
As long as the user enters characters that fall outside that range, the program displays an error message and requests another character.
When the user enters a character that falls within the required range, the program displays a thank-you message and terminates.
Program output
The program displays the following output for several user input values:
Enter an upper-case letter: @ Bad input Enter an upper-case letter: [ Bad input Enter an upper-case letter: A Thanks for the AMuch of the code is familiar
The functions named main and classMain in this program are identical to functions having the same names in several previous lessons. Therefore, I won't explain those functions in this lesson. You can view them in Listing 5 near the end of the lesson.
The doSomething function
All of the interesting code is contained in an instance function of the Operators01 class named doSomething.
The doSomething function begins in Listing 1.
|
The code in Listing 1 does the following in preparation for the later use of a loop structure:
The conditional clause
The really interesting code is contained in the conditional clause of a while loop as shown in Listing 2.
while((temp < 'A') || (temp > 'Z')){ //Outside allowable character range cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop Listing 2 |
The character codes
The character codes for the characters A through Z are the numeric values ranging from 65 through 90 inclusive. (See http://www.lookuptables.com/ for the character codes used by C++.)
The conditional clause
The conditional clause in Listing 2 tests to see if the character entered by the user and stored in the variable temp falls outside the range from 65 through 90 inclusive.
This is accomplished by testing to see if the value of the character is less than 65 (A) OR is greater than 90 (Z).
Execute loop body on true
If the character code is outside the specified range, the conditional clause returns true causing the body of the loop to be executed.
The body of the loop displays an error message and requests another input character from the user.
When the user enters another character, the test is performed again and the process repeats.
I don't see 65 or 90!!
Note that you don't see the numeric values 65 or 90 anywhere in Listing 2. This is because the C++ compiler and/or runtime system automatically translates 'A' into 65 and automatically translates 'Z' into 90.
Bypass loop body on false
If the value of the character stored in temp does not fall outside the range of 65 through 90 inclusive, the conditional clause returns false.
In this case, the while loop terminates, bypassing the body of the loop, and control transfers to the code shown in Listing 3.
//Good input, display msg and terminate. cout << "Thanks for the " << temp << endl; }//end doSomething function Listing 3 |
The code in Listing 3 prints a thank-you message similar to the last line of output shown above.
Then the program terminates.
You can view a complete listing of this program in Listing 6 near the end of the lesson.
This program is designed to solve the same problem as the previous program. However, this version uses a different formulation of the relational and logical operators in the conditional clause of the while loop.
This program illustrates the use of the following relational and logical operators:
Restrict discussion to the while loop
Except for the formulation of the conditional clause in the while loop, this program is identical to the previous program. Therefore, I will restrict my discussion to the while loop as shown in Listing 4.
//Note the negation operator in the following // statement. while(! ((temp >= 'A') && (temp <= 'Z')) ){ //Outside allowable character range cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop Listing 4 |
The nature of the test
Once again, the conditional clause tests to determine if the value of the character entered by the user and stored in the variable named temp falls outside the range from 65 through 90 inclusive.
The logical negation operator
First I want to call your attention to the use of the logical negation operator (!) in the conditional clause. Note its position. Also note that it is applied to the result of evaluating everything enclosed in the matching pair of parentheses immediately to its right.
Is the character within the specified range?
The code inside those parentheses tests to see if the character value stored in temp is greater than or equal to 65 (A) AND is less than or equal to 90 (Z).
In other words, contrary to the test in the previous program, this code tests to see if the character is within the specified range rather than testing to see if the character is outside the specified range.
Character is within the specified range ...
If the character is within the specified range, then the evaluation of the code within the parentheses returns true.
IMPORTANT: When the logical negation operator is applied to this result, the result is changed to false.
Loop body is bypassed
When the conditional clause returns false, the while loop terminates. The body of the loop is bypassed, transferring control to the code that displays a thank-you message and terminates the program.
Character is outside the specified range ...
If the character value is not greater than or equal to 65 (A) AND is not less than or equal to 90 (Z), then the evaluation of the code within the parentheses returns false.
IMPORTANT: When the logical negation operator is applied to this result, it is changed to true.
Loop body is executed
When the conditional clause returns true, the body of the while loop executes.
As in the previous program, the code in the body of the loop displays an error message and then requests that the user enter another character.
When the user enters a new character, the test is performed again, and the process repeats.
Different formulations to solve the same problem
When dealing with combinations of relational and logical operators, it is almost always possible to formulate the solution to the problem in at least two different ways.
I learned in logic-design classes in my electrical engineering curriculum in undergraduate school that there is a formal theorem that describes this process. I still remember how to implement the theorem in a formal way. Unfortunately, I can't remember the name of the theorem. Otherwise, I would direct you to a web site that explains the theorem.
/*File: Operators01.cpp This C++ program illustrates the use of the following relational and logical operators: < || > This program solves the same problem as the program named Operators02 but uses a different formulation of the relational/logical test. The program displays the following output on the screen for several user input values: Enter an upper-case letter: @ Bad input Enter an upper-case letter: [ Bad input Enter an upper-case letter: A Thanks for the A ************************************************/ #include |
/*File: Operators02.cpp This C++ program illustrates the use of the following relational and logical operators plus the negation operator: ! >= && <= This program solves the same problem as the program named Operators01 but uses a different formulation of the relational/logical test. The program displays the following output on the screen for several user input values: Enter an upper-case letter: @ Bad input Enter an upper-case letter: ] Bad input Enter an upper-case letter: Z Thanks for the Z ************************************************/ #include |
Copyright 2005, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.
In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP). His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments. (TI is still a world leader in DSP.) In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-