240x Filetype PDF File size 0.02 MB Source: www.epaperpress.com
Control Structures
Sequence
Statements execute in sequence, one after the other, such as the following solution for a
quadratic equation:
double desc, x1, x2;
desc = b * b – 4 * a * c;
desc = sqrt(desc);
x1 = (-b - desc) / 2 * a;
x2 = (-b + desc) / 2 * a;
Statements may appear in a block or compound statement:
{
double desc, x1, x2;
desc = b * b – 4 * a * c;
desc = sqrt(desc);
x1 = (-b - desc) / 2 * a;
x2 = (-b + desc) / 2 * a;
}
In C all definitions must be at the beginning of a block. In C++ you may define variables at any
point in the code:
{
double desc = b * b – 4 * a * c;
desc = sqrt(desc);
double x1 = (-b - desc) / 2 * a;
double x2 = (-b + desc) / 2 * a;
}
Variables defined in a block are local to that block and override global variables and variables in
outer blocks with the same name.
{
int a = 1, b = 2;
cout << a << b << endl;
{
int a = 3;
cout << a << b << endl;
}
cout << a << b << endl;
}
The above code outputs the following:
12
32
12
Selection
If Statement
The general form of the if and if-else statements follow:
if (condition)
statement
if (condition)
statement
else
statement
A statement may be a single statement terminated by a semicolon, or a compound statement. For
example:
int g; // grade, 0..100
if (g > 50)
cout << "passed" << endl;
if (g > 50)
cout << "passed" << endl;
else
cout << "failed" << endl;
if (g > 50) {
cout << "passed" << endl;
cout << "good job" << endl;
} else {
cout << "failed" << endl;
cout << "bad job" << endl;
}
What does the following code output if the grade is 10?
if (g > 50)
cout << "passed" << endl;
cout << "good job" << endl;
What does the following code output if grade is zero?
if (grade = 0)
cout << "failed" << endl;
Nested If
Recall that control structures may be nested. Lets nest an If statement within another If
statement:
if (g > 50)
if (g > 90)
cout << "super" << endl;
else
cout << "good" << endl;
else
if (g > 25)
cout << "fair" << endl;
else
cout << "poor" << endl;
Well, that's a bit hard to read. Let's rewrite it with proper indenting:
if (g > 50)
if (g > 90)
cout << "super" << endl; // g = 91..100
else
cout << "good" << endl; // g = 51..90
else
if (g > 25)
cout << "fair" << endl; // g = 26..50
else
cout << "poor" << endl; // g = 0..25
What is the output for the following code if the grade is 20?
if (g > 50)
if (g > 90)
cout << "pt1";
else
cout << "pt2";
Ternary Conditional Operator
The following code evaluates i and j, placing the smallest number in min:
if (i < j)
min = i;
else
min = j;
Let's use the ternary operator to accomplish the same logic:
min = i < j ? i : j;
The general form is as follows:
condition ? trueExpression : falseExpression
If the condition evaluates to true (non-zero) then trueExpression is returned. Otherwise,
falseExpression is returned. Not meant as a substitute for If-Else, ternary operators sometimes
simplify logic. This is illustrated below, in a min function:
int min(int x, int y) { int min(int x, int y) {
int t; return (x < y) ? x : y;
if (x < y) }
t = x;
else
t = y;
return t;
}
Switch Statement
If you are repeatedly testing a variable for constant values, consider using the switch statement
instead of an if-else statement:
if (a == 1) switch(a) {
x = 5; case 1:
else if (a == 3) x = 5;
x = 10; break;
else if (a == 5) case 3:
x = 15; x = 10;
else if (a == 6) break;
x = 15; case 5:
else case 6:
x = 100; x = 15;
break;
default:
x = 100;
}
Control transfers directly to the appropriate case, then falls through until there is a break
statement. On a break, control transfers to the next sequential statement after the switch
statement.
no reviews yet
Please Login to review.