This documentation is archived and is not being maintained.

Checking Results

You can use assertion statements to check the result of an operation. Assertions are most valuable for testing operations whose results are not obvious from a quick visual inspection.

For example, consider the following code, which updates the variable iMols based on the contents of the linked list pointed to by mols:

while (mols->type != "H2O")
{
 iMols += mols->num;
 mols = mols->next;
}
ASSERT(iMols<=numMols); -- MFC version
_ASSERT(iMols<=numMols); -- CRT version

The number of molecules counted by iMols must always be less than or equal to the total number of molecules, numMols. Visual inspection of the loop does not show that this will necessarily be the case, so an assertion statement is used after the loop to test for that condition.

See Also

Catching Logic Errors | Assertions

Show: