switch (expression)
{
case value1 : statement(s);
break;
case value2 : statement(s);
break;
case value3 : statement(s);
break;
default : statement(s);
}
NOTES:
a) single statement, e.g., a=b+c;
b) function call;
c) "null", e.g. case value2 : break;
d) multiple statements.
{ int rank, score;
...
rank = score / 10;
switch (rank)
{
case 10 : cout << "score of 100 gets A+" << endl;
break;
case 9 : cout << "score of " << score << "gets A" << endl;
break;
case 8 : cout << "score of " << score << "gets B" << endl;
break;
case 7 : case 6 : cout << "score of " << score << "gets C" << endl;
break;
case 5 : case 4 : cout << "score of " << score << "gets D" << endl;
break;
default : cout << "Please see the teacher" << endl;
}
There are many predefined functions in C/C++. To use any, one needs to include the appropriate header file.
The most often math functions used (one needs the #include <math.h> statement) are as follows:
sqrt(x) square root sin(x) sine - takes radians as the argument cos(x) cosine - also takes radians floor(x) truncates a real number to convert it to an integer value (drops the fractional part) fabs(x) absolute value pow(x,y) takes x to the power of yWe use these functions similar to the way we use functions in algebra, by enclosing the argument (input values) in parentheses after the function name.
EXAMPLE:
c = sqrt(a*b); a = sin(alpha/2.0);There also exist numerous other functions including many related to the strings of characters. Some of these functions are not that important for our purposes right now.
In C/C++, we can declare certain identifiers as constants. The constant declaration is usually placed along with the variable declarations and is similar to it.
The general form is:
It is permitted to reverse the order of the const and the type at the beginning of the declaration line.
Constants are useful for repeatedly used concepts in the program which may be changed rarely.
EXAMPLE:
const double pi = 3.14159265; const int maxnum = 100000;NOTE: a constant cannot be changed in a program (if this is tried, an error message will be given).
"Elegance" usually refers to proofs which are simple and concise and solve a problem in a clear and complete manner. It may be hard enough to get a program running, and running correctly without having to worry about elegance, much less efficiency. But some small things can be pointed out.
One small thing which can help the speed of a program is to take repeated unnecessary work out of loops.
For example, instead of
for(num=1; num <=12; num++)
{
miles = num * 5.0;
kilometers = miles * 8.0/5.0;
cout << miles << kilometers << endl;
}
it is better to have
eightfifths = 8.0/5.0;
for(num=1; num <= 12; num++)
{
miles = num * 5.0;
kilometers = miles * eightfifths;
cout << miles << kilometers << endl;
}
One important concept in contemporary programming is modularity -- writing programs in small segments. In general, independent subsections are called subprograms. In Pascal, they are called "PROCEDURES" if they do NOT return a single value and "FUNCTIONS" if they do. In FORTRAN, they are called "SUBROUTINES" if they do NOT return a single value and "FUNCTIONS" if they do. In C/C++, they are always called "FUNCTIONS."
Subprograms can be used in many ways, but their main usefulness is two-fold:
Subprograms are written with the main program, and are activated, executed or "called" by another section of the code (the main program or another subprogram).
This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski@math.scu.edu
© Copyright 1997, 1998, Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 5 December 1998.