| Name: _____________________ | Class: Comp 217 |
| SSN/ID: _____________________ | Section & Group: ____________ |
In this lab, you will
If a testing expression is not of bool type, it is coerced to bool type automaticaly when it is evaluated. A nonzero value is coerced to true and a zero value is coerced to false.
A Boolean expression can be a simple Boolean variable or constant or it can be a more complex expression involving one or more of the relational operators. Relational operators take two operands and test relationships between them. The following table shows the relational operators and the corresponding C++ symbols.
| Relational Operators | |
|---|---|
| C++ Symbol | Description |
| == | Equal To |
| != | Not Eqaul To |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than Or Equal To |
| <= | Less Than Or Equal To |
For example, The Boolean expression
number1 < number2is evaluated to true if the value stored in number1 is less than the value stored in number2, and evaluated to false otherwise.
When a relational operator is applied between variables of type char, the assertion is in terms of where the two operands fall in the collating sequence of a particular character set.
For example,
character1 < character2is evaluated to true if the value stored in character1 comes before the character stored in character2 in the collating sequence of the machine on which the expression is being evaluated. You can think of collating sequence as being in alphabetic order to help you understand it. ASCII character set is widely used.
A simple Boolean expression is either a Boolean variable or a Boolean constant or an expression involving the relational operators that evaluates to either true or false. These simple Boolean expressions can be combined using logical operations defined on Boolean values. There are three Boolean operators: AND, OR and NOT. Here is a table showing how they are used in C++.
| Logical Operators | |
|---|---|
| C++ Symbol | Meaning |
| && | AND is a binary Boolean operator. If both operands are true, the result is true. Otherwise, the result is false. |
| || | OR is a binary Boolean operator. If at least one of the operands is true, the result is true. Otherwise, if both operands are false, the result is false. |
| ! | NOT is a unary Boolean operator. NOT changes value of its operand. If the operand is true, the result is false. If the operand is false, the result is true. |
The following table summarizes the precedence of all the C++ operators we have seen so far.
| C++ Operator Precedence | |
|---|---|
| ++ -- | Highest Precedence |
| ! Unary + Unary - | ↓ |
| * / % | ↓ |
| + - | ↓ |
| << >> | ↓ |
| < <= > >= | ↓ |
| == != | ↓ |
| && | ↓ |
| || | ↓ |
| = | Lowest Precedence |
Operators in the same line in the table have the same precedence. If an expression contains several operators with the same precedence, most of the operators group from left to right.
if (Expression)The expression in parentheses can be of any simple data type. Almost without exception, this will be a logical (Boolean) expression; if not, its value is implicitly coerced to type bool (nonzero value means true, zero value means false).
Statement
Now, let's look at the following statement:
if (number < 0) number = 0; sum = sum + number;The expression (number < 0) is evaluated. If the result is true, the statement number = 0; is executed. If the result is false, the statement is skipped. In either case, the next statement to be executed is sum = sum + number;.
if (Expression)The expression in parentheses will be evaluated with the result of true or false. Here is an example:
Statement1A else
Statement1B
cout << "You are "; if (age >= 65) cout << "a senior "; else cout << "not a senior "; cout << "citizen." << endl;The characters "You are " are sent to the output stream. The expression age >= 65 is evaluated. If the result is true, the characters "a senior " are sent to the output stream. If the result is false, the characters "not a senior" is sent to the output stream. In either case, the next statement to be executed sends the the characters "citizen." to the output stream.
There is one thing to point out here: any statement in the if statement could be a block or a compound statement; i.e., more than one simple statement are enclosed in a pair of braces.
The following example is a nested If statement.
cout << "You are ";
if (age >= 65)
cout << "a senior." << endl;
else
if (age >= 19)
cout << "an adult." << endl;
else
if (age >= 13)
cout << "a teenager." << endl;
else
cout << "a child." << endl;
cout << "You are a great person." << endl;
The characters "You are " are sent to the output stream.
cout << "You are ";
if (age >= 65) {
cout << "a senior." << endl;
} else {
if (age >= 19) {
cout << "an adult." << endl;
} else {
if (age >= 13) {
cout << "a teenager." << endl;
} else {
cout << "a child." << endl;
}
}
}
cout << "You are a great person." << endl;
Here is an example program that reads four floating point data values from a file and write to another file in the reverse order.
// Program iodemo.cpp demonstrates how to use files
#include <iostream>
#include <fstream>
using namespace std;
int main() {
cout << fixed << showpoint;
float val1, val2, val3, val4; // declares 4 variables
ifstream inData; // declares input stream
ofstream outData; // declares output stream
// binds program variable inData to file "Data.In"
inData.open("Data.In");
//Testing the state of the stream
//true means the last I/O operation on that stream succeeded
//false means the last I/O operation on that stream failed
if (!inData) {
cout << "Can't open the input file successfuly." << endl;
return 1;
}
// binds program variable outData to file "Data.Out"
outData.open("Data.Out");
//Testing the state of the stream
if (!outData) {
cout << "Can't open the output file successfuly." << endl;
return 2;
}
inData >> val1 >> val2 >> val3 >> val4; // inputs 4 values
outData << val4 << endl;
outData << val3 << endl;
outData << val2 << endl;
outData << val1 << endl; // outputs 4 values
return 0;
}
|
Notice that inData and outData are identifiers in the program; "Data.In" and "Data.Out" are character strings. Data.In is the name of the input data file that we have created; Data.Out is the name of the file where the answers are stored.
If the input file Data.In cannot be found, 1 is returned to the operating system. If the output file Data.Out cannot be opened or created, 2 is returned to the operating system. If there is no input and output error, 0 is returned to the operating system. Notice that the main() is exited as soon as a value is returned. Therefore, Returning 0 value means normal completion of a program; returning any other value signals an error. When you write your own program, you may choose the value to return to indicate different error conditions.
You can use the emacs text editor to create the input data file according to the requirement of the data type and format in your program. Input data file must exist and contain correct data. Otherwise, you will get input failure.
For example, in the preceding iodemo.cpp program, the input file should look like this:
5.5 6.6 7.7 8.8You may run the program and and test the state of the I/O stream.
// Program convert.cpp converts a temperature in Fahrenheit to
// Celsius or a temperature in Celsius to Fahrenheit
// depending on whether the user enters an F or a C..
#include <iostream>
using namespace std;
int main() {
char letter; // Place to store input letter
float tempIn; // Temperature to be converted
float tempOut; // Converted temperature
cout << "Input Menu" << endl << endl;
cout << "F: Convert from Fahrenheit to Celsius" << endl;
cout << "C: Convert from Celsius to Fahrenheit" << endl;
cout << "Type a C or an F, then press return." << endl << endl;
cout << "Enter your option: ";
cin >> letter;
cout << "Type a temperature: ";
cin >> tempIn;
if (letter == 'C')
tempOut = (9 * tempIn / 5) + 32;
else
tempOut = 5 * (tempIn - 32) / 9;
cout << endl << endl;
cout << "Temperature to convert: " << tempIn << endl;
cout << "Converted temperature: " << tempOut << endl;
return 0;
}
|
This program demonstrates how to use If-Then-Else statement.
// Program ioexercise.cpp demonstrates how to use files
#include <iostream>
#include <fstream>
using namespace std;
int main() {
cout << fixed << showpoint;
float val1, val2, val3, val4; // declares 4 variables
ifstream inData; // declares input stream
ofstream outData; // declares output stream
// Bind program variable inData to file "Data.In"
inData.open("Data.In");
// Bind program variable outData to file "Data.Out"
outData.open("Data.Out");
inData >> val1 >> val2 >> val3 >> val4; // inputs 4 values
outData << val4 << endl;
outData << val3 << endl;
outData << val2 << endl;
outData << val1 << endl; // outputs 4 values
return 0;
}
|
This program demonstrates how to use File I/O. It reads four floating point values from the input data file and output these numbers to an output file in reverse order.
Hand In: This lab handout with the answers filled in attached
to a listing of your final exercises
(use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename> or a2ps <filename>).