<< Chapter < Page Chapter >> Page >
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;

}

The output of the program:

Enter the value of monthly sales: 36243.89

The income is $4674.27

The switch statement

The switch statement controls program flow by executing a set of statements depending on the value of an expression.

Note: The value of expression must be an integer data type, which includes the char, int, long int, and short data types.

The syntax for the switch statement:

switch(expression){

case label:

statement(s);

break;

case label;

statement(s);

break;

default:

statement(s);

}

The expression in the switch statement must evaluate to an integer result. The switch expression’s value is compared to each of these case values in the order in which these values are listed until a match is found. When a match occurs, execution begins with the statement following the match.

If the value of the expression does not match any of the case values, no statement is executed unless the keyword default is encountered. If the value of the expression does not match any of the case values, program execution begins with the statement following the word default.

The break statement is used to identify the end of a particular case and causes an immediate exit from the switch statement. If the break statements are omitted, all cases following the matching case value, including the default case, are executed.

Example

#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;

}

The output of the above program:

Enter a number to find the state where a city is located.

1. Boston

2. Chicago

3. Los Angeles

4. Miami

5. Providence

3

Los Angeles is in California

The switch statement is a clean way to implement multi-way selection (i.e., selecting from among a number of different execution paths), but it requires an expression that evaluates to an integral value at compile-time.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask