DECISION MAKING IN
C++
C++ switch statement
• A switch statement allows a variable to be tested for equality against a
list of values.
• Each value is called a case, and the variable being switched on is
checked for each case.
C++ switch statement
◦ Syntax
◦ switch(expression)
◦ { case constant-expression : statement(s); break; //optional case
case constant-expression : statement(s); break; //optional
◦ // you can have any number of case statements.
◦ default : //Optional statement(s);
◦ }
C++ switch statement
◦ The following rules apply to a switch statement:
1. The expression used in a switch statement must have an integral or
character.
2. You can have any number of case statements within a switch. Each
case is followed by the value to be compared to and a colon.
3. The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
4. When the variable being switched on is equal to a case, the
statements following that case will execute until a break statement is
reached.
C++ switch statement
5. When a break statement is reached, the switch terminates, and the
flow of control jumps to the next line following the switch statement.
6. Not every case needs to contain a break. If no break appears, the flow
of control will fall through to subsequent cases until a break is
reached.
7. A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is needed
in the default case.
Flow Diagram
Example
◦ #include <iostream> using
namespace std; int main ()
◦ { // local variable declaration:
◦ char grade = 'D';
switch(grade)
◦ { case 'A' : cout <<
"Excellent!" << endl; break;
◦ case 'B' : cout << “Very
Good!" << endl; break;
◦ case 'C' : cout << "Well
done" << endl; break;
◦ case 'D' : cout << "You
passed" << endl; break;
◦ case 'F' : cout << "Better try
again" << endl; break;
◦ default : cout << "Invalid
grade" << endl; }
◦ cout << "Your grade is " <<
grade << endl;
◦ return 0; }
Nested if Statements
◦ Syntax
◦ The syntax for a nested if statement is as follows:
◦ if( boolean_expression 1)
◦ { // Executes when the boolean expression 1 is true
◦ if(boolean_expression 2)
◦ { // Executes when the boolean expression 2 is true }
◦ }
Example
◦ #include <iostream>
◦ using namespace std;
◦ int main ()
◦ { // local variable declaration:
◦ int a = 100;
◦ int b = 200;
◦ // check the boolean
condition
◦ if( a == 100 )
◦ { // if condition is true then
check the following
◦ if( b == 200 ) {
◦ // if condition is true then print
the following
◦ cout << "Value of a is 100 and b
is 200" << endl;
◦ } }
◦ cout << "Exact value of a is : "
<< a << endl;
◦ cout << "Exact value of b is : "
<< b << endl;
◦ return 0; }
exercise
1. Write a menu driven C++ Program to Make a Simple Calculator to
Add, Subtract, Multiply or Divide Using switch...case.
2. Write a program that will ask number of the month as input and print
the number of the days in that month.
3. Write a program that presents the user with a choice of your 5
favorite beverages (Coke, Water, Sprite, ... , Whatever).
Then allow the user to choose a beverage by entering a number 1-5.
Output which beverage they chose with price.
4. Modify the program so that if the user enters a choice other than 1-5
then it will output "Error. choice was not valid, here is your money
back."
exercise
5. Write a C++ program to find whether a given year is a leap year or
not.
6. Write a C++ program to accept a coordinate point in a XY coordinate
system and determine in which quadrant the coordinate point lies.
7. Write a C++ program to find the eligibility of admission for a
professional course based on the following criteria:
Marks in Maths >=65
Marks in Phy >=55
Marks in Chem>=50
Total in all three subject >=180
or
Total in Math and Subjects >=140
exercise
8. Write a program in C++ which is a Menu-Driven Program to compute
the area of the various geometrical shape. (e.g. circle, triangle,
rectangle)
9. Write a C++ program to check whether a character is an alphabet,
digit or special character.
10. Write a program in C++ to calculate and print the Electricity bill of a
given customer. The customer id., name and unit consumed by the
user should be taken from the keyboard and display the total amount
to pay to the customer. The charge are as follow :
exercise
◦ If bill exceeds Rs. 400 then a additional charge of 15% will be charged
and the minimum bill should be of Rs. 100/-
Unit Charge/unit
upto 199 @1.20
200 and above but
less than 400
@1.50
400 and above but
less than 600
@1.80
600 and above @2.00

Switch.pptx

  • 1.
  • 3.
    C++ switch statement •A switch statement allows a variable to be tested for equality against a list of values. • Each value is called a case, and the variable being switched on is checked for each case.
  • 4.
    C++ switch statement ◦Syntax ◦ switch(expression) ◦ { case constant-expression : statement(s); break; //optional case case constant-expression : statement(s); break; //optional ◦ // you can have any number of case statements. ◦ default : //Optional statement(s); ◦ }
  • 5.
    C++ switch statement ◦The following rules apply to a switch statement: 1. The expression used in a switch statement must have an integral or character. 2. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. 3. The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal. 4. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • 6.
    C++ switch statement 5.When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 6. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. 7. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
  • 7.
  • 8.
    Example ◦ #include <iostream>using namespace std; int main () ◦ { // local variable declaration: ◦ char grade = 'D'; switch(grade) ◦ { case 'A' : cout << "Excellent!" << endl; break; ◦ case 'B' : cout << “Very Good!" << endl; break; ◦ case 'C' : cout << "Well done" << endl; break; ◦ case 'D' : cout << "You passed" << endl; break; ◦ case 'F' : cout << "Better try again" << endl; break; ◦ default : cout << "Invalid grade" << endl; } ◦ cout << "Your grade is " << grade << endl; ◦ return 0; }
  • 9.
    Nested if Statements ◦Syntax ◦ The syntax for a nested if statement is as follows: ◦ if( boolean_expression 1) ◦ { // Executes when the boolean expression 1 is true ◦ if(boolean_expression 2) ◦ { // Executes when the boolean expression 2 is true } ◦ }
  • 10.
    Example ◦ #include <iostream> ◦using namespace std; ◦ int main () ◦ { // local variable declaration: ◦ int a = 100; ◦ int b = 200; ◦ // check the boolean condition ◦ if( a == 100 ) ◦ { // if condition is true then check the following ◦ if( b == 200 ) { ◦ // if condition is true then print the following ◦ cout << "Value of a is 100 and b is 200" << endl; ◦ } } ◦ cout << "Exact value of a is : " << a << endl; ◦ cout << "Exact value of b is : " << b << endl; ◦ return 0; }
  • 11.
    exercise 1. Write amenu driven C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case. 2. Write a program that will ask number of the month as input and print the number of the days in that month. 3. Write a program that presents the user with a choice of your 5 favorite beverages (Coke, Water, Sprite, ... , Whatever). Then allow the user to choose a beverage by entering a number 1-5. Output which beverage they chose with price. 4. Modify the program so that if the user enters a choice other than 1-5 then it will output "Error. choice was not valid, here is your money back."
  • 12.
    exercise 5. Write aC++ program to find whether a given year is a leap year or not. 6. Write a C++ program to accept a coordinate point in a XY coordinate system and determine in which quadrant the coordinate point lies. 7. Write a C++ program to find the eligibility of admission for a professional course based on the following criteria: Marks in Maths >=65 Marks in Phy >=55 Marks in Chem>=50 Total in all three subject >=180 or Total in Math and Subjects >=140
  • 13.
    exercise 8. Write aprogram in C++ which is a Menu-Driven Program to compute the area of the various geometrical shape. (e.g. circle, triangle, rectangle) 9. Write a C++ program to check whether a character is an alphabet, digit or special character. 10. Write a program in C++ to calculate and print the Electricity bill of a given customer. The customer id., name and unit consumed by the user should be taken from the keyboard and display the total amount to pay to the customer. The charge are as follow :
  • 14.
    exercise ◦ If billexceeds Rs. 400 then a additional charge of 15% will be charged and the minimum bill should be of Rs. 100/- Unit Charge/unit upto 199 @1.20 200 and above but less than 400 @1.50 400 and above but less than 600 @1.80 600 and above @2.00

Editor's Notes

  • #14 Area of circle: 3.14*radius*radius Triangle= .5*base*height Rectangle= length * width