Summary: The flow of control refers to the order in which a program’s statements are executed. Unless directed otherwise, the normal flow of control for all programs is sequential. This means that statements are executed in sequence, one after another, in the order in which they are placed within the program. Selection, repetition and function invocation structures permit the flow of control to be altered in defined ways. This chapter introduces to you C++’s selection statements. Repetition and invocation techniques are presented in the next two chapters.
![]() Figure 1: Relational operators |
a = = b
(a*b) != c
s == ‘y’
x<= 4
(age > 40) && (term < 10)
(age > 40) || (term < 10)
!(age > 40)
( i==j) || (a < b) || complete
![]() Figure 2: Associativity of operators |
char key = ‘m’;
int i = 5, j = 7, k = 12;
double x = 22.5;
![]() Figure 3: Results of expressions |
![]() Figure 4: Evaluation process |
#include<iostream.h>
int main()
{
bool t1, t2;
t1 = true;
t2 = false;
cout << “The value of t1 is “<< t1
<< “\n and the value of t2 is “<< t2 << endl;
return 0;
}
if (conditional expression) {
statements;
}
else {
statements;
}
![]() Figure 5: Flowchart of statement |
#include <iostream.h>
#include <iomanip.h>
const float LOWRATE = 0.02; // lower tax rate
const float HIGHRATE = 0.025; // higher tax rate
const float CUTOFF = 20000.0; // cut off for low rate
const float FIXEDAMT = 400; // fixed dollar amount for higher rate amounts
int main()
{
float taxable, taxes;
cout << "Please type in the taxable income: ";
cin >> taxable;
if (taxable <= CUTOFF)
taxes = LOWRATE * taxable;
else
taxes = HIGHRATE * (taxable - CUTOFF) + FIXEDAMT;
// set output format
cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
cout << "Taxes are $ " << taxes << endl;
return 0;
}
![]() Figure 6: Flowchart of example |
{ // start of outer block
int a = 25;
int b = 17;
cout << “The value of a is “ << a
<< “ and b is “ << b << endl;
{ // start of inner block
float a = 46.25;
int c = 10;
cout << “ a is now “ << a
<< “b is now “ << b
<< “ and c is “ << c << endl;
}
cout << “ a is now “ << a
<< “b is now “ << b << endl;
} // end of outer block
if (conditional expression) {
statements;
}
![]() Figure 7: Flowchart of statement |
#include <iostream.h>
int main()
{
int grade;
cout << "\nPlease enter a grade: ";
cin >> grade;
if(grade < 0 || grade > 100)
cout << " The grade is not valid\n";
return 0;
}
if (expression-1)
statement-1
else if (expression-2)
statement-2
else
statement-3
![]() Figure 8: Layout of result |
#include <iostream.h>
#include <iomanip.h>
int main()
{
float monthlySales, income;
cout << "\nEnter the value of monthly sales: ";
cin >> monthlySales;
if (monthlySales >= 50000.00)
income = 375.00 + .16 * monthlySales;
else if (monthlySales >= 40000.00)
income = 350.00 + .14 * monthlySales;
else if (monthlySales >= 30000.00)
income = 325.00 + .12 * monthlySales;
else if (monthlySales >= 20000.00)
income = 300.00 + .09 * monthlySales;
else if (monthlySales >= 10000.00)
income = 250.00 + .05 * monthlySales;
else
income = 200.00 + .03 * monthlySales;
// set output format
cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
cout << "The income is $" << income << endl;
return 0;
}
switch(expression){
case label:
statement(s);
break;
case label;
statement(s);
break;
default:
statement(s);
}
#include <iostream.h>
int main()
{
int iCity;
cout << "Enter a number to find the state where a city is located. "<< endl;
cout << “1. Boston” << endl;
cout << "2. Chicago" << endl;
cout << "3. Los Angeles” << endl;
cout << "4. Miami” << endl;
cout << "5. Providence” << endl;
cin >> iCity;
switch (iCity)
{
case 1:
cout << "Boston is in Massachusetts " << endl;
break;
case 2:
cout << "Chicago is in Illinois " << endl;
break;
case 3:
cout << "Los Angeles is in California " << endl;
break;
case 4:
cout << "Miami is in Florida " << endl;
break;
case 5:
cout << "Providence is in Rhode Island " << endl;
break;
default:
cout << “You didn’t select one of the five cities” << endl;
} // end of switch
return 0;
}
switch(number)
{
case 1:
cout << “Have a Good Morning\n”;
break;
case 2:
cout << “Have a Happy Day\n”;
break;
case 3:
case 4:
case 5:
cout << “Have a Nice Evening\n”;
}
enum day { mon, tue, wed, thu, fri, sat, sun};
enum color {red, green, yellow};
enum day a, b,c;
#include <iostream.h>
int main()
{
enum color{red, green, yellow};
enum color crayon = red;
cout << “\nThe color is “ << crayon << endl;
cout << “Enter a value: “;
cin >> crayon;
if (crayon == red)
cout << “The crayon is red.” << endl;
else if (crayon == green)
cout << “The crayon is green.” << endl;
else if (crayon== yellow)
cout << “The crayon is yellow.” << endl;
else
cout << “The color is not defined. \n” << endl;
return 0;
}
Display a program purpose message.
Accept user-input values for a, b, and c.
If a = 0 and b = 0 then
Display a message saying that the equation has no solution.
Else if a = 0 then
calculate the single root equal to –c/b.
display the single root.
Else
Calculate the discriminant.
If the discriminant > 0 then
Solve for both roots using the given formulas.
Display the two roots.
Else if the discriminant < 0 then
Display a message that there are no real roots.
Else
Calculate the repeated root equal to –b/(2a).
Display the repeated root.
Endif.
Endif.
// This program can solve quadratic equation
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
int main()
{
double a, b, c, del, x1, x2;
cout << “This program calculates the roots of a\n”;
cout << “ quadratic equation of the form\n”;
cout << “ 2\n”;
cout << “ ax + bx + c = 0\n\n”;
cout << “Enter values for a, b, and c: “;
cin >> a >> b >> c;
if ( a == 0.0 && b == 0.0)
cout << “The equation is degenerate and has no roots.\n”;
else if (a == 0.0)
cout << “The equation has the single root x = “
<< -c/b << endl;
else
{
del = b*b – 4.0*a*c;
if (del > 0.0)
{
x1 = (-b + sqrt(del))/(2*a);
x2 = (-b – sqrt(del))/(2*a);
cout << "The two roots are “
<< x1 << “ and “ << x2 << endl;
}
else if (del <0)
cout << "Both roots are imaginary.\n";
else
cout << “Both roots are equal to “ << -b/(2*a) << endl;
}
return 0;
}
Comments, questions, feedback, criticisms?