#include <iostream>
using namespace std;
// This "IntegerDivision.cpp" program is designed to demonstrate both
// integer division and the modulus (%) operation.
int main() {
int denom, numer, answer, remain;
cout << "Enter an integer for the numerator: ";
cin >> numer;
cout << "Enter a number for the denominator: ";
cin >> denom;
answer = numer / denom; // Performs integer division
remain = numer % denom; // Gives remainder from integer division
cout << endl;
cout << numer << " divided by " << denom << " is " << answer;
cout << " leaving a remainder of " << remain << "." << endl;
return 0;
}
Download, compile, and run this code,
IntegerDivision.cpp,
trying a number of examples so you see what is happening.
This type of division of two integers that discards the decimal or fractional
part of the number is called integer division.
Integer division can be a useful tool because it give you the most number
of times the denominator can be multipled without exceeding the numerator.
| WARNING: | Most of us are so used to working with decimal numbers that it can be easy to make a logic error and expect a decimal number answer instead! |
|---|
| WARNING: | Not following these rules will likely result in syntax errors! |
|---|
if( num < 0 ) {
cout << "The number is negative.\n";
}
Note that because the example above is a portion of a complete proram, it is
called a code fragment.
You can compile and run the complete program, called
IfConditional.cpp
The expression in parentheses is called the condition.
If the condition is true, then the statement(s) between the curly brackets
get executed.
If the condition is not true, these statement(s) are skipped.
In C++, the condition can contain any of the following comparison operators:
|
if( num % 2 == 0 ) { // What to do when num is exactly divisible by 2.
cout << "The number is even." << endl;
}
else { // What to do when num is NOT divisible by 2.
cout << "The number is odd." << endl;
}
Try this example,
ConditionalIfElse.cpp,
noting that here we used only the modulus operator, %, without the division
operator, /, because we only needed the remainder.
Note that the above code is a much more efficient way of coding the algorithm than the following code:
if( num % 2 == 0 ) { // What to do when num is exactly divisible by 2.
cout << "The number is even." << endl;
}
if( num % 2 != 0 ) { // What to do when num is NOT divisible by 2.
cout << "The number is odd." << endl;
}
Though this code with two if statements will accomplish exactly the
same task and have the same output as the code with the if-else, it
is less efficient because num is tested twice instead of just once.
if( num > 0) {
cout << "The number is positive." << endl;
}
else
if( num < 0) {
cout << "The number is negative." << endl;
}
else {
cout << "The number is 0." << endl;
}
Note that the second if-else is chained in the first else.
Try this example,
ConditionalChaining.cpp.
These if-else chains can become as long as you need, although they
can be difficult to read if they get too long.
One way to make them easier to read is to use standard indentation, as
demonstrated in these examples.
If you keep all the statements and braces lined up, you are less likely to
make syntax errors, and you can find errors more quickly if you do make them.
In addition to chaining, you can also nest one conditional within another. The previous example done this way is:
if( num > 0) {
cout << "num is positive" << endl;
}
else {
if( num < 0) {
cout << "num is negative" << endl;
}
else {
cout << "num is 0" << endl;
}
}
This example will have exactly the same output as the previous example,
however, in this nested example, there is now an "outer conditional" that
contains two branches.
The first branch contains a simple output statement, but the second branch
contains another if statement, which has two branches of its own.
Fortunately, those two branches are both output statements, although they
could have been conditional statements as well.
Notice again that standard indentation helps make the structure apparent,
but nevertheless, nested conditionals get difficult to read very quickly.
This kind of nested structure is common and enables conditional execution
behavior that is not possible otherwise.
int) type.
Therefore, names such as "a", "x", or "q" are not recommended.
// Course: CSC 306 Introduction to Programming with C++
// Name: Your Name
// Assignment #3: The C++ Conditional Statement
// Purpose: This program reads in a year as input and outputs the
// the number of days in that year in an easily readable format.
You will include a similar header section in every program for this
class.
This assignment must be completed individually. When you are finished with your assignment, drop your source code into the CSC306_A03 dropbox on the Academic server.