A program using a conditional
// Author: Karl Abrahamson
// Date: 01/12/00
//
// This program reads two integers and prints the larger of
// the two.
#include <iostream>
using namespace std;
///////////////////////////////////////////////////////////
// max
///////////////////////////////////////////////////////////
// max(x,y) returns the larger of x and y.
///////////////////////////////////////////////////////////
int max(int x, int y)
{
if(x > y) {
return x;
}
else {
return y;
}
}
///////////////////////////////////////////////////////////
// main
///////////////////////////////////////////////////////////
int main()
{
int n,m;
cout << "What numbers shall I use? ";
cin >> n >> m;
cout << "The larger of " << n << " and " << m
<< " is " << max(n,m) << endl;
return 0;
}
Notes on the program
// Author: Karl Abrahamson
// Date: 01/12/00
//
// This program reads two integers and prints the larger of
// the two.
#include <iostream>
using namespace std;
///////////////////////////////////////////////////////////
// max
///////////////////////////////////////////////////////////
// max(x,y) returns the larger of x and y.
///////////////////////////////////////////////////////////
The conditional starts with if.
Notice that there are parentheses around the condition. They
are required.
int max(int x, int y)
{
if(x > y) {
return x;
}
else {
return y;
}
}
///////////////////////////////////////////////////////////
// main
///////////////////////////////////////////////////////////
int main()
{
int n,m;
cout << "What numbers shall I use? ";
cin >> n >> m;
cout << "The larger of " << n << " and " << m
<< " is " << max(n,m) << endl;
return 0;
}