// A modification of iferror.cpp to incorporate if else statements // and compound statements (those enclosed by braces { }). Also, // an explanation of if and if else is at the bottom of this file. #include #include void main() { int a,b; double x,y,z; cout << "Enter 2 integers: "; cin >> a >> b; if (a==b) cout << "Next time, enter two DISTINCT integers." << endl; else { x = 1./(a-b); cout << "1/(" << a << " - " << b << ") = " << x << endl; } cout << "Enter a real number: "; cin >> y; if (y<0) cout << "Next time, enter a NONNEGATIVE real number." << endl; else { z = sqrt(y); cout << "The square root of " << y << " is " << z << endl; } } // An if statement with syntax // if (expression) statement; // executes the statement if expression is true, and does // nothing if the expression is false. // A modification of this uses if and else: syntax is // if (expression) statement1; // else statement2; // If the expression is true, then statement1 is executed; and if // the expression is false, statement2 is executed.