CSC 101 Lecture Notes Week 3
More on Arithmetic Expressions;
Output Formatting;
Introduction to Logical Expressions and Conditionals
| + | Unary plus |
| - | Unary minus |
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Floating point division (floating point result) |
| Integer division (no fractional part) | |
| % | Modulus (remainder from integer division) |
| ++ | increment the value of a variable by 1 |
| -- | decrement the value of a variable by 1 |
x = y++ * 10;
| ( ... ) | highest |
| * / % | next highest |
| + - | lowest |
is 7, since the 2 * 3 subexpression is evaluated first.1 + 2 * 3
which has the value 9, since the parentheses force the evaluation of 1 + 2 to be done first.(1 + 2) * 3
associates as1 - 2 + 3
(1 - 2) + 3
C++ truncates the floating point value 2.5 to the integer value 2.some_int = 2.5
converts the integer 2 into the floating point value 2.0 and stores it in the floating point variable.some_float = 2
|
type of
left operand |
type of
right operand |
type of
result |
| int | int | int |
| int | float | float |
| float | int | float |
| float | float | float |
explicitly converts the result of y + z to an int (i.e., truncates off the fractional part, if any).float x, y, z; x = int(x + z);
some_int = int(some_float + 0.5);
which outputsint i, j, k; i = 123; j = 1234; k = 12; cout << setw(4) << i << setw(5) << j << setw(3) << k;
#include <iostream.h>
#include <iomanip.h>
int main () {
float x, y, z;
cout.setf(ios::fixed, ios::floatfield); // Set up floating point
cout.setf(ios::showpoint); // output format
x = 12.345;
y = 12.3456;
z = 12.3;
cout << setprecision(5) << x << "," << setprecision(8) << y << ","
<< setprecision(4) << z << endl;
}
which outputs
12.34500,12.34560013,12.3000
prints the value as just 10, not 10.0.cout << x
prints the value as 1.234567E+08, i.e., 1.234567 * 108.cout << x
#include <iostream.h> // The C++ input/output library
int main() { // C++ programs always start with this header
int x; // Declare a variable to hold the input number
cin >> x; // Input the number typed on the screen
if (x > 0) // Check if the number is > 0 (i.e., positive)
cout << "yes"; // If so, output "yes" to the screen
else // If not,
cout << "no"; // output "no" to the screen
} // End of main program (matches the "{")
#include <iostream.h>
int main() {
//
// Declare program variables to hold the purchase amount, amount tendered,
// total change amount, and the amounts of each denomination of change,
// from dollars to pennies.
//
int purchase; // Amount of purchase
int tendered; // Amount tendered
int change; // Total change due
//
// Prompt for and input the amount of purchase.
//
cout << "Input the amount of the purchase, in cents: ";
cin >> purchase;
//
// Prompt for and input the amount tendered.
//
cout << "Input the amount tendered, in cents: ";
cin >> tendered;
if (tendered < purchase) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " the amount tendered is less than the amount of the purchase."
<< endl;
}
else {
//
// ... The rest of the change computation goes here ...
//
}
}
#include <iostream.h>
int main() {
//
// Declare program variables to hold the purchase amount, amount tendered,
// total change amount, and the amounts of each denomination of change,
// from dollars to pennies.
//
int purchase; // Amount of purchase
int tendered; // Amount tendered
int change; // Total change due
//
// Prompt for and input the amount of purchase.
//
cout << "Input the amount of the purchase, in cents: ";
cin >> purchase;
//
// Prompt for and input the amount tendered.
//
cout << "Input the amount tendered, in cents: ";
cin >> tendered;
if (tendered <= 0 || purchase <= 0) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " both inputs must be greater than zero."
<< endl;
}
else {
//
// ... The rest of the change computation goes here ...
//
}
}
#include <iostream.h>
int main() {
//
// Declare program variables to hold the purchase amount, amount tendered,
// total change amount, and the amounts of each denomination of change,
// from dollars to pennies.
//
int purchase; // Amount of purchase
int tendered; // Amount tendered
int change; // Total change due
//
// Prompt for and input the amount of purchase.
//
cout << "Input the amount of the purchase, in cents: ";
cin >> purchase;
//
// Prompt for and input the amount tendered.
//
cout << "Input the amount tendered, in cents: ";
cin >> tendered;
if (tendered <= 0 || purchase <= 0) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " both inputs must be greater than zero."
<< endl;
}
else if (tendered < purchase) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " the amount tendered is less than the amount of the purchase."
<< endl;
}
else {
//
// ... The rest of the change computation goes here ...
//
}
}
#include <iostream.h>
int main() {
//
// Declare program variables to hold the purchase amount, amount tendered,
// total change amount, and the amounts of each denomination of change,
// from dollars to pennies.
//
int purchase; // Amount of purchase
int tendered; // Amount tendered
int change; // Total change due
//
// Prompt for and input the amount of purchase.
//
cout << "Input the amount of the purchase, in cents: ";
cin >> purchase;
//
// Prompt for and input the amount tendered.
//
cout << "Input the amount tendered, in cents: ";
cin >> tendered;
if (tendered <= 0 || purchase <= 0 || tendered < purchase) {
cout << "Sorry, your purchase cannot be completed since"
<< endl
<< " both inputs must be greater than zero, and"
<< endl
<< " the amount tendered cannot be less than the amount of purchase."
<< endl;
}
else {
//
// ... The rest of the change computation goes here ...
//
}
}
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| && | Logical and |
| || | Logical or |
| ! | Logical not |
| ! | Highest precedence |
| * / % | | |
| + - | | |
| < <= > >= | | |
| == != | | |
| && | | |
| || | v |
| = | Lowest precedence |
typedef int Boolean; const Boolean TRUE = 1; const Boolean FALSE = 0;
if (result <= 0)
cout << "The result must be positive; program will force it." << endl;
result = 1;
else
cout << "Result is good so far." << endl;
result = result * 10;
if (result = 0) {
cout << "The result must be non-zero; program will force it." << endl;
result = 1;
}
else {
cout << "Result is good so far." << endl;
}