Just like with C, the switch statement in C++ is used to select one of several alternatives based on a value of a single variable or simple expression. The value of this expression may be int or charbut not of type double. Switch statement is particularly used instead of if…elseladder, when there are multiple choices such as menu options. The general form of switch structure is:
switch (variable)
{
case constant1:
statement(s);
break;
case constant2:
statement(s);
break;
case constant1:
statement(s);
break;
default:
statement(s);
}
The switch structure starts with the keyword switch followed by one block which contains the different cases. Each case handles the statements corresponding to an option (a satisfying condition) and ends with a break statement which transfers the control out of the switch structure in the original program.
The variable inside the parentheses following the switch keyword is called the control variable and is used to test the condition. If the value of the variable matches “constant1”, the “case constant1” is executed. If the match is “constant2”, the “case constant2” is executed, and so on. If the value of the variable does not correspond to any case, the default case is executed. Example 7 demonstrates the use of the switch statements.
Example 8.7
// A program to demonstrate switch construct.
#include <iostream.h>
#include <stdlib.h>
main()
{
char choice;
char *a, *b, *c, *d, *e, *f, *g, *h;
a = “ MAIN MENU.”;
b= “_------------------------------------.”;
c = “1 – Mocrosoft Word.”;
d = “2 – Microsoft Excel.”;
e = “3 – Mocrosoft Power Point.”;
f = “4 – AutoCAD.”;
g = “5 - Exit to Desktop.”;
h = “Press the required number:”;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << e << endl;
cout << f << endl;
cout << g << endl;
cout << h << endl;
cint >> choice;
/* Start of the switch block */
switch (choice)
{
case ‘1’:
cout << “Microsoft Word is Chosen.” << endl;
break;
case ‘2’:
cout << “Microsoft Excel is Chosen.” << endl;
break;
case ‘3’:
cout << “Microsoft Power Point is Chosen.” << endl;
break;
case ‘4’:
cout << “AutoCAD is Chosen.” << endl;
break;
case ‘5’:
exit(0)
default;
cout << “Sorry Wrong Key.” << endl;
}
/* End of the switch block */
cout << This is the end of the Switch.” << endl;
cout << Back to the Program.” << endl;
}
8.4 The conditional operator as an alternative of if
The conditional operator “?” ,called ternary operator, operates on three variables (commonly used operators other operators operates on two variables “binary”, and fewer on one variable “unary”). The condition operator has the syntax:
condition ? expression: expression
The condition operator consists of both the question mark and the colon. Condition is the logical expression that evaluates either True or False, while expression 1 and expression 2 are either values or expressions that evaluate to values.
Example: The following conditional statement can be explained as follows:
max = (num1 > num2) ? num1 : num2
The purpose of this statement is to assign to the variable max the value either num1 or num2, whichever is larger. First, the condition (num1 > num2) is evaluated. If it is true, the entire conditional takes on the value of num1; this value is then assigned to max.
If (num1 > num2) is false, the conditional expression takes on the value of num2, and this value is assigned to max.
This expression is equivalent to
if (num1 > num2)
max = num1
else
max = num2
Note that conditional statement is more compact than the than the if-else- since the entire statement takes the takes on a value
8.5 Unconditional control ( goto)
The unconditional goto is used in situations, a programmer want to transfer the flow of control to another part of the program without testing for any condition. The goto should be followed by a label name and the C++ program should have only one statement having that label qualifier in the block, in which goto is used.
The syntax for goto is:
Label: statement;
The label is a valid C++ identifier followed by a colon. You can precede any statement by a label in the form:
Label: statement;
You can, for example, make a loop to repeat the program execution infinitely. This is done by adding a goto statement and a label, as in Example 1 below the label “START” at the vary beginning to repeat executing the program.
Goto START;
The Program in example 1 demonstrating use of unconditional control (goto) and bi-condition if else. Not that the program will not stop unless you enter the character “0” and the function exit() terminates the program.
Example 8.8
/* Program to demonstrate if-else-if ladder */
#include <iostream.h>
#include <conio.h>
main()
{
char a;
START: cout<<”\nPlease enter an alphabetic
character: “;
cin >> a;
if (a == ‘0’)
exit(0);
else if (a > 64 && a < 91)
cout << “\nThe character is an upper-case
letter”;
else if (a > 96 && a < 123)
cout << “\nThe character is a lower-case letter”;
else
cout << “\nThis is not an alphabetic character!”;
goto START;
}
9.0 LOOPS
Many jobs that required to be done with the help of a computer are repetitive in nature. For example, calculation of salary of different workers in a factory is given by the (No. of hours worked) x (wage rate). This calculation will be performed by an accountant for each worker every month. Such types of repetitive calculations can easily be done using a program that has a loop built into the solution of the problem.
9.1 What is a Loop ?
A loop is defined as a block of processing steps repeated a certain number of times. The flow chart in figure 2 illustrates the concept of looping. It is a flowchart for printing values
1, 2, 3, …., 20.
The flowchart uses special geometric figures to represent the actions taken by the computer and the logical flow of the actions that take place. Below are symbol commonly used in flow charts.
In step 5 of the flow chart above, the current value of A is compared with 21. If the current value of A is less than 21, steps 3 and 4 are repeated. As soon as the current value of A is not less than 21, the path corresponding to “NO” is followed and the repetition process stops.
Terms used in Looping
Initialization: It is the preparation required before entering a loop. In the flowchart above, step 2 initializes the value of A as 1.
Increment: It is the numerical value added to the variable each time one goes round the loop. Step 4 of the flowchart shows the increment of A by 1.
The Loop Variable. It is an active variable in a loop. In the flowchart above, A is the active variable.
Example 9.9
Draw a Flowchart for calculating the salary of 100 workers
|
Explanation:
Figure 3 above is the flow chart that calculates a salary of 100 workers. In this figure, step 2 is for initialization of the value of COUNT where COUNT is an active variable. Step 7 is the incrementation. Step 3 is for the EXIT test. Step 4 to six are the repetitive steps in the loop to input the NAME, WAGE, and HOURS, and then calculate the value of PAY in step 5 and print the name and pay in step 6. In step 7, the value of COUNT is increased by 1 and the current value of COUNT is compared with 100 in step 3. If it is more than 100, the process is halted.
Example 9.10
How many times the value of I will be read in the flowchart shown in figure 4 below
No comments:
Post a Comment