<< Chapter < Page | Chapter >> Page > |
When writing a switch statement, you can use multiple case values to refer to the same set of statements; the default label is optional. For example, consider the following example:
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”;
}
An enumerated data type is a way of attaching names to numbers, thereby giving
more meaning to anyone reading the code. The enum specifier creates an enumerated data type, which is simply a user-defined list of values that is given its own data type name. Such data types are identified by the reserved word enum followed by an optional user-selected name for the data type and a listing of acceptable values for the data type.
Example:
enum day { mon, tue, wed, thu, fri, sat, sun};
enum color {red, green, yellow};
Any variable declared to be of type color can take only a value of red or green or yellow. Any variable declared to be of type day can take only a value among seven given values.
The statement
enum day a, b,c;
declares the variables a, b, and c to be of type day.
Internally, the acceptable values of each enumerated data type are ordered and assigned sequential integer values beginning with 0. For example, for the values of the user-defined type color, the correspondences created by the C++ compiler are that red is equivalent to 0, green is equivalent to 1, and yellow is equivalent to 2. The equivalent numbers are required when inputting values using cin or displaying values using cout.
#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;
}
The output of the above program:
The color is 0
Enter a value: 2
The crayon is yellow.
Two major uses of C++’s if statements are to select appropriate processing paths and to prevent undesirable data from being processed at all. In this section, an example of both uses is provided.
Problem: Solving Quadratic Equations
A quadratic equation is an equation that has the form ax2 + bx + c = 0 or that can be algebraically manipulated into this form. In this equation, x is the unknown variable, and a, b and c are known constants. Although the constants b and c can be any numbers, including 0, the value of the constant a cannot be 0 (if a is 0, the equation becomes a linear equation in x). Examples of quadratic equations are:
5x^2 + 6x + 2 = 0
x^2 - 7x + 20 = 0
34x^2 + 16 = 0
In the first equation, a = 5, b = 6, and c = 2; in the second equation, a = 1, b = -7, and c = 20; and in the third equation, a = 34, b = 0 and c = 16.
The real roots of a quadratic equation can be calculated using the quadratic formula as:
delta = b2 – 4ac
root1 = (-b + squared-root(delta))/(2a)
Notification Switch
Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?