Examples of the 'if' Statement

Here are a set of examples involving the 'if' statement. The first one calculates the difference between two weights measured in pounds and ounces. This requires an 'if' statement to handle the possibility that in subtracting the ounces one may need to borrow.
// A program to find the difference between two weights given in
// pounds and ounces

// file: ch2xmpl6.cpp

#include <iostream.h>

const int OUNCES_IN_POUND = 16;

void main()
{
// Purpose:     to find the difference between two weights given in
//                    pounds and ounces


// Receives:    NONE
// Returns:     NONE

	int numPounds1;
	int numOunces1;

	int numPounds2;
	int numOunces2;

	int diffInPounds;
	int diffInOunces;

	cout << "Please enter the pounds for the first weight"  << endl;
	cin >> numPounds1;

	cout << "Please enter the ounces for the first weight"  << endl;
	cin >> numOunces1;

	cout << "Please enter the pounds for the second weight"  << endl;
	cin >> numPounds2;

	cout << "Please enter the ounces for the second weight"  << endl;
	cin >> numOunces2;

	if (numOunces2 > numOunces1) // need to borrow
	{	numPounds1--;
		numOunces1+=OUNCES_IN_POUND;  // remember there are 16 ounces in a pound
	}
	diffInOunces = numOunces1 - numOunces2;
	diffInPounds = numPounds1 - numPounds2;

	cout << "The difference between weight 1 and weight 2 is: " << endl;
	cout << "         " << diffInPounds<< " Pounds and " << diffInOunces
	      << " Ounces";
}
The next problem is to calculate the number of reams of paper required for some amount of xeroxing. It is a bit more complex in that it uses some tricks of division in C++. First, when two integers are dividied, the result is an integer with any fractional part just dropped. Second, the '%' operator determinies if any fraction was dropped.
// A program to determine the number of reams of paper required for some number
// of copies to be made. This amount is based on the number of page to copy;
// the number of people (plus some extra).
// An example of the 'If' statement
// File: ch2xmpl5.cpp

#include <iostream.h>

const int NUM_EXTRAS = 5; // number of extra copies of each page of a doc
const int PAGES_IN_REAM = 500;

void main()
{
// Purpose:     to determine the number of reams of paper required for some
//                    number of copies to be made.

// Receives:    NONE
// Returns:     NONE

		  int numPeople;
		  int numPages;
		  int copies;
		  int numReams;

		  cout << "Please enter the number of pages in the material to be copied"
				 << endl;
		  cin >> numPages;

		  cout << "Please enter the number of people to receive copies" << endl;
		  cin >> numPeople;

		  copies = (numPeople + NUM_EXTRAS) * numPages;
				// multiply the number of pages times the number of people needing
				// copies - allow for a some extra copies

		  numReams = copies / PAGES_IN_REAM;
		  if (copies % PAGES_IN_REAM != 0) // we have a partial ream
		  {	numReams++;
		  }

		  if (numReams == 1) // produce good English
		  {	cout << "1 ream is required";
		  }
		  else
		  {   cout << numReams << " reams are required";
		  }

}
Discussion of the 'if' statement in "The Story of C++"