Заглавная страница Избранные статьи Случайная статья Познавательные статьи Новые добавления Обратная связь FAQ Написать работу КАТЕГОРИИ: АрхеологияБиология Генетика География Информатика История Логика Маркетинг Математика Менеджмент Механика Педагогика Религия Социология Технологии Физика Философия Финансы Химия Экология ТОП 10 на сайте Приготовление дезинфицирующих растворов различной концентрацииТехника нижней прямой подачи мяча. Франко-прусская война (причины и последствия) Организация работы процедурного кабинета Смысловое и механическое запоминание, их место и роль в усвоении знаний Коммуникативные барьеры и пути их преодоления Обработка изделий медицинского назначения многократного применения Образцы текста публицистического стиля Четыре типа изменения баланса Задачи с ответами для Всероссийской олимпиады по праву Мы поможем в написании ваших работ! ЗНАЕТЕ ЛИ ВЫ?
Влияние общества на человека
Приготовление дезинфицирующих растворов различной концентрации Практические работы по географии для 6 класса Организация работы процедурного кабинета Изменения в неживой природе осенью Уборка процедурного кабинета Сольфеджио. Все правила по сольфеджио Балочные системы. Определение реакций опор и моментов защемления |
Simple and Compound StatementsСодержание книги Поиск на нашем сайте
A simple statement is a computation terminated by a semicolon. Variable definitions and semicolon-terminated expressions are examples:
int i; // declaration statement ++i; // this has a side-effect double d = 10.5; // declaration statement d + 5; // useless statement!
The last example represents a useless statement, because it has no side-effect (d is added to 5 and the result is just discarded). The simplest statement is the null statement which consists of just a semicolon:
; // null statement
Although the null statement has no side-effect, as we will see later in the chapter, it has some genuine uses. Multiple statements can be combined into a compound statement by enclosing them within braces. For example:
{ int min, i = 10, j = 20; min = (i < j? i: j); cout << min << '\n'; }
Compound statements are useful in two ways: (i) they allow us to put multiple statements in places where otherwise only single statements are allowed, and (ii) they allow us to introduce a new scope in the program. A scope is a part of the program text within which a variable remains defined. For example, the scope of min, i, and j in the above example is from where they are defined till the closing brace of the compound statement. Outside the compound statement, these variables are not defined. Because a compound statement may contain variable definitions and defines a scope for them, it is also called a block. The scope of a C++ variable is limited to the block immediately enclosing it.
The if Statement It is sometimes desirable to make the execution of a statement dependent upon a condition being satisfied. The if statement provides a way of expressing this, the general form of which is:
if (expression) statement;
First expression is evaluated. If the outcome is nonzero then statement is executed. Otherwise, nothing happens. For example, when dividing two values, we may want to check that the denominator is nonzero:
if (count!= 0) average = sum / count;
To make multiple statements dependent on the same condition, we can use a compound statement:
if (balance > 0) { interest = balance * creditRate; balance += interest; }
A variant form of the if statement allows us to specify two alternative statements: one which is executed if a condition is satisfied and one which is executed if the condition is not satisfied. This is called the if-else statement and has the general form:
if (expression) statement1; else statement2;
First expression is evaluated. If the outcome is nonzero then statement1 is executed. Otherwise, statement2 is executed. For example:
if (balance > 0) { interest = balance * creditRate; balance += interest; } else { interest = balance * debitRate; balance += interest; }
If statements may be nested by having an if statement appear inside another if statement. For example:
if (callHour > 6) { if (callDuration <= 5) charge = callDuration * tarrif1; else charge = 5 * tarrif1 + (callDuration - 5) * tarrif2; } else charge = flatFee;
The switch Statement The switch statement provides a way of choosing between a set of alternatives, based on the value of an expression. The general form of the switch statement is:
switch (expression) { case constant1: statements; ... case constantn: statements; default: statements; }
First expression (called the switch tag) is evaluated, and the outcome is compared to each of the numeric constants (called case labels), in the order they appear, until a match is found. The statements following the matching case are then executed. Note the plural: each case may be followed by zero or more statements (not just one statement). Execution continues until either a break statement is encountered or all intervening statements until the end of the switch statement are executed. The final default case is optional and is exercised if none of the earlier cases provide a match. For example, suppose we have parsed a binary arithmetic operation into its three components and stored these in variables operator, operand1, and operand2. The following switch statement performs the operation and stored the result in result.
switch (operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: cout << "unknown operator: " << operator << '\n'; } As illustrated by this example, it is usually necessary to include a break statement at the end of each case. The break terminates the switch statement by jumping to the very end of it. There are, however, situations in which it makes sense to have a case without a break.
The while Statement The while statement (also called while loop) provides a way of repeating an statement while a condition holds. It is one of the three flavors of iteration in C++. The general form of the while statement is:
while (expression) statement;
First expression (called the loop condition) is evaluated. If the outcome is nonzero then statement (called the loop body) is executed and the whole process is repeated. Otherwise, the loop is terminated. For example, suppose we wish to calculate the sum of all numbers from 1 to some integer denoted by n. This can be expressed as:
n = 5; i = 1; sum = 0; while (i <= n) { sum += i; i++; }
For n set to 5, Table 1 provides a trace of the loop by listing the values of the variables involved and the loop condition.
The do Statement The do statement (also called do loop) is similar to the while statement, except that its body is executed first and then the loop condition is examined. The general form of the do statement is:
do statement; while (expression);
First statement is executed and then expression is evaluated. If the outcome of the latter is nonzero then the whole process is repeated. Otherwise, the loop is terminated. The do loop is less frequently used than the while loop. It is useful for situations where we need the loop body to be executed at least once, regardless of the loop condition. For example, suppose we wish to repeatedly read a value and print its square, and stop when the value is zero. This can be expressed as the following loop:
do { cin >> n; cout << n * n << '\n'; } while (n!= 0);
Unlike the while loop, the do loop is never used in situations where it would have a null body. Although a do loop with a null body would be equivalent to a similar while loop, the latter is always preferred for its superior readability.
The for Statement The for statement (also called for loop) is similar to the while statement, but has two additional components: an expression which is evaluated only once before everything else, and an expression which is evaluated once at the end of each iteration. The general form of the for statement is:
for (expression1; expression2; expression3) statement;
First expression1 is evaluated. Each time round the loop, expression2 is evaluated. If the outcome is nonzero then statement is executed and expression3 is evaluated. Otherwise, the loop is terminated. The general for loop is equivalent to the following while loop:
expression1; while (expression2) { statement; expression3; }
The most common use of for loops is for situations where a variable is incremented or decremented with every iteration of the loop. The following for loop, for example, calculates the sum of all integers from 1 to n.
sum = 0; for (i = 1; i <= n; ++i) sum += i;
This is preferred to the while-loop version we saw earlier. In this example, i is usually called the loop variable. C++ allows the first expression in a for loop to be a variable definition. In the above loop, for example, i can be defined inside the loop itself:
for (int i = 1; i <= n; ++i) sum += i;
Contrary to what may appear, the scope for i is not the body of the loop, but the loop itself. Any of the three expressions in a for loop may be empty. For example, removing the first and the third expression gives us something identical to a while loop:
for (; i!= 0;) // is equivalent to: while (i!= 0) something; // something;
Removing all the expressions gives us an infinite loop. This loop's condition is assumed to be always true:
for (;;) // infinite loop something;
For loops with multiple loop variables are not unusual. In such cases, the comma operator is used to separate their expressions:
for (i = 0, j = 0; i + j < n; ++i, ++j) something;
Because loops are statements, they can appear inside other loops. In other words, loops can be nested. For example,
for (int i = 1; i <= 3; ++i) for (int j = 1; j <= 3; ++j) cout << '(' << i << ',' << j << ")\t";
produces the product of the set {1,2,3} with itself, giving the output:
(1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3)
The continue Statement The continue statement terminates the current iteration of a loop and instead jumps to the next iteration. It applies to the loop immediately enclosing the continue statement. It is an error to use the continue statement outside a loop. In while and do loops, the next iteration commences from the loop condition. In a for loop, the next iteration commences from the loop’s third expression. For example, a loop which repeatedly reads in a number, processes it but ignores negative numbers, and terminates when the number is zero, may be expressed as:
do { cin >> num; if (num < 0) continue; // process num here... } while (num!= 0);
When the continue statement appears inside nested loops, it applies to the loop immediately enclosing it, and not to the outer loops.
The break Statement A break statement may appear inside a loop (while, do, or for) or a switch statement. It causes a jump out of these constructs, and hence terminates them. Like the continue statement, a break statement only applies to the loop or switch immediately enclosing it. It is an error to use the break statement outside a loop or a switch. For example, suppose we wish to read in a user password, but would like to allow the user a limited number of attempts:
for (i = 0; i < attempts; ++i) { cout << "Please enter your password: "; cin >> password; if (Verify(password)) // check password for correctness break; // drop out of the loop cout << "Incorrect!\n"; }
Here we have assumed that there is a function called Verify which checks a password and returns true if it is correct, and false otherwise.
The goto Statement The goto statement provides the lowest-level of jumping. It has the general form:
goto label;
where label is an identifier which marks the jump destination of goto. The label should be followed by a colon and appear before a statement within the same function as the goto statement itself. For example, the role of the break statement in the for loop in the previous section can be emulated by a goto:
for (i = 0; i < attempts; ++i) { cout << "Please enter your password: "; cin >> password; if (Verify(password)) // check password for correctness goto out; // drop out of the loop cout << "Incorrect!\n"; } out: //etc...
Because goto provides a free and unstructured form of jumping (unlike break and continue), it can be easily misused. Most programmers these days avoid using it altogether in favor of clear programming.
The return Statement The return statement enables a function to return a value to its caller. It has the general form:
return expression;
where expression denotes the value returned by the function. The type of this value should match the return type of the function. For a function whose return type is void, expression should be empty:
return;
The only function we have discussed so far is main, whose return type is always int. The return value of main is what the program returns to the operating system when it completes its execution. Under UNIX, for example, it its conventional to return 0 from main when the program executes without errors. Otherwise, a non-zero error code is returned. For example:
int main (void) { cout << "Hello World\n"; return 0; }
When a function has a non-void return value (as in the above example), failing to return a value will result in a compiler warning.
Lab Overview 2.1. Read the theory and try Control Exercises. 2.2. Develop the algorithm flowchart to solve a problem according to individual case from the Table below. 2.3. Write the program code according to the developed algorithm. 2.4. Debug the program, run it and make screenshots. 2.5. Prepare the Lab Report according to the required structure.
Report Structure - Title page - Task overview - Algorithm’s flowchart - Program code - Program running screenshots - Conclusions
Control Exercises 4.1. Assuming that n is 20, what will the following code fragment output when executed? if (n >= 0) if (n < 10) cout << "n is small\n"; else cout << "n is negative\n";
4.2. Assuming that n is 1, what will the following code fragment output when executed? switch(n) { case 0: cout << “Zero”; case 1: cout << “One”; case 2: cout << “Two”; }
4.3. Assuming that n is 2, what will the following code fragment output when executed? switch(n) { case 0: cout << “Zero”; break; case 1: cout << “One”; break; case 2: cout << “Two”; break; default: cout << “Other”; }
4.4. Assuming that n is 3, what will the following code fragment output when executed? switch(n) { case 0: cout << “Zero”; break; case 1: cout << “One”; break; case 2: cout << “Two”; break; default: cout << “Other”; }
4.5. What will the following code fragment output when executed? for (i=5; i>=-5; i--) { if (i == 0) continue; cout << i; }
4.6. What will the following code fragment output when executed? n = 3; while (n=5) cout << n;
4.7. How many ‘*’ will appear on the screen when executed? i=1; do { cout << “*”; i++; } while (i<5);
References 5.1. Juan Soulié. C++ Language Tutorial. – 2007. – p. 34-40. 5.2. Sharam Hekmat. C++ Essentials. – PragSoft Corporation 2005. – p. 30-44. 5.3. Prata S. C++ Primer Plus (5th Edition). – Sams, 2004. – p. 178-262. Lab #3. C++ USER-DEFINED FUNCTIONS Goal: program numerical integration with different integration methods using C++ user-defined functions Theory A function provides a convenient way of packaging a computational recipe, so that it can be used as often as required. A function definition consists of two parts: interface and body. The interface of a function (also called its prototype) specifies how it may be used. It consists of three entities: - The function name. This is simply a unique identifier. - The function parameters (also called its signature). This is a set of zero or more typed identifiers used for passing values to and from the function. - The function return type. This specifies the type of value the function returns. A function which returns nothing should have the return type void. The body of a function contains the computational steps (statements) that comprise the function. Using a function involves ‘calling’ it. A function call consists of the function name followed by the call operator brackets ‘()’, inside which zero or more comma-separated arguments appear. The number of arguments should match the number of function parameters. Each argument is an expression whose type should match the type of the corresponding parameter in the function interface. When a function call is executed, the arguments are first evaluated and their resulting values are assigned to the corresponding parameters. The function body is then executed. Finally, the function return value (if any) is passed to the caller. Since a call to a function whose return type is non- void yields a return value, the call is an expression and may be used in other expressions. By contrast, a call to a function whose return type is void is a statement.
A Simple Function Listing 1 shows the definition of a simple function which raises an integer to the power of another, positive integer.
Listing 1 1 int Power (int base, unsigned int exponent) 2 { 3 int result = 1; 4 for (int i = 0; i < exponent; ++i) 5 result *= base; 6 return result; 7 }
Annotation 1 This line defines the function interface. It starts with the return type of the function (int in this case). The function name appears next followed by its parameter list. Power has two parameters (base and exponent) which are of types int and unsigned int, respectively Note that the syntax for parameters is similar to the syntax for defining variables: type identifier followed by the parameter name. However, it is not possible to follow a type identifier with multiple comma-separated parameters:
int Power (int base, exponent) // Wrong!
2 This brace marks the beginning of the function body. 3 This line is a local variable definition. 4-5 This for-loop raises base to the power of exponent and stores the outcome in result. 6 This line returns result as the return value of the function. 7 This brace marks the end of the function body. Listing 2 illustrates how this function is called. The effect of this call is that first the argument values 2 and 8 are, respectively, assigned to the parameters base and exponent, and then the function body is evaluated.
Listing 2 1 #include <iostream> 2 void main () 3 { 4 cout << "2 ^ 8 = " << Power(2,8) << '\n'; 5 }
When run, this program will produce the following output: 2 ^ 8 = 256
In general, a function should be declared before its is used. A function declaration simply consists of the function prototype, which specifies the function name, parameter types, and return type. Line 2 in Listing 3 shows how Power may be declared for the above program. A function may be declared without its parameter names. Listing 3 1 #include <iostream>
2 int Power (int base, unsigned int exponent); // function declaration
3 void main (void) 4 { 5 cout << "2 ^ 8 = " << Power(2,8) << '\n'; 6 }
7 int Power (int base, unsigned int exponent) 8 { 9 int result = 1; 10 for (int i = 0; i < exponent; ++i) 11 result *= base; 12 return result; 13 }
Because a function definition contains a prototype, it also serves as a declaration. Therefore if the definition of a function appears before its use, no additional declaration is needed.
Parameters and Arguments C++ supports two styles of parameters: value and reference. A value parameter receives a copy of the value of the argument passed to it. As a result, if the function makes any changes to the parameter, this will not affect the argument. For example, in
#include <iostream>
void Foo (int num) { num = 0; cout << "num = " << num << '\n'; }
void main () { int x = 10; Foo(x); cout << "x = " << x << '\n'; }
the single parameter of Foo is a value parameter. As far as this function is concerned, num behaves just like a local variable inside the function. When the function is called and x passed to it, num receives a copy of the value of x. As a result, although num is set to 0 by the function, this does not affect x. The program produces the following output:
num = 0; x = 10;
A reference parameter, on the other hand, receives the argument passed to it and works on it directly. Any changes made by the function to a reference parameter is in effect directly applied to the argument. Within the context of function calls, the two styles of passing arguments are, respectively, called pass-by-value and pass-by-reference.
Global and Local Scope Everything defined at the program scope level (i.e., outside functions and classes) is said to have a global scope. Thus the sample functions we have seen so far all have a global scope. Variables may also be defined at the global scope:
int year = 1994; // global variable int Max (int, int); // global function
int main (void) // global function { //... }
Uninitialized global variables are automatically initialized to zero. Since global entities are visible at the program level, they must also be unique at the program level. This means that the same global variable or function may not be defined more than once at the global level. (However, as we will see later, a function name may be reused so long as its signature remains unique.) Global entities are generally accessible everywhere in the program. Each block in a program defines a local scope. Thus the body of a function represents a local scope. The parameters of a function have the same scope as the function body. Variables defined within a local scope are visible to that scope only. Hence, a variable need only be unique within its own scope. Local scopes may be nested, in which case the inner scopes override the outer scopes. For example, in
int xyz; // xyz is global
void Foo (int xyz) { // xyz is local to the body of Foo if (xyz > 0) { double xyz; // xyz is local to this block //... } }
there are three distinct scopes, each containing a distinct xyz. Generally, the lifetime of a variable is limited to its scope. So, for example, global variables last for the duration of program execution, while local variables are created when their scope is entered and destroyed when their scope is exited. The memory space for global variables is reserved prior to program execution commencing, whereas the memory space for local variables is allocated on the fly during program execution.
Scope Operator Because a local scope overrides the global scope, having a local variable with the same name as a global variable makes the latter inaccessible to the local scope. For example, in
int error;
void Error (int error) { //... }
the global error is inaccessible inside Error, because it is overridden by the local error parameter. This problem is overcome using the unary scope operator:: which takes a global entity as argument:
int error;
void Error (int error) { //... if (::error!= 0) // refers to global error //... }
Symbolic Constants Preceding a variable definition by the keyword const makes that variable read-only (i.e., a symbolic constant). A constant must be initialized to some value when it is defined. For example:
const int maxSize = 128; const double pi = 3.141592654;
Once defined, the value of a constant cannot be changed:
maxSize = 256; // illegal!
A constant with no type specifier is assumed to be of type int:
const maxSize = 128; // maxSize is of type int
With pointers, two aspects need to be considered: the pointer itself, and the object pointed to, either of which or both can be constant:
const char *str1 = "pointer to constant"; char *const str2 = "constant pointer"; const char *const str3 = "constant pointer to constant"; str1[0] = 'P'; // illegal! str1 = "ptr to const"; // ok str2 = "const ptr"; // illegal! str2[0] = 'P'; // ok str3 = "const to const ptr"; // illegal! str3[0] = 'C'; // illegal!
A function parameter may also be declared to be constant. This may be used to indicate that the function does not change the value of a parameter:
int Power (const int base, const unsigned int exponent) { //... }
A function may also return a constant result:
const char* SystemVersion () { return "5.2.1"; }
Inline Functions Suppose that a program frequently requires to find the absolute value of an integer quantity. For a value denoted by n, this may be expressed as:
(n > 0? n: -n)
However, instead of replicating this expression in many places in the program, it is better to define it as a function:
int Abs (int n) { return n > 0? n: -n; }
The function version has a number of advantages. First, it leads to a more readable program. Second, it is reusable. And third, it avoid undesirable side-effects when the argument is itself an expression with side-effects. The disadvantage of the function version, however, is that its frequent use can lead to a considerable performance penalty due to the overheads associated with calling a function. For example, if Abs is used within a loop which is iterated thousands of times, then it will have an impact on performance. The overhead can be avoided by defining Abs as an inline function:
inline int Abs (int n) { return n > 0? n: -n; }
The effect of this is that when Abs is called, the compiler, instead of generating code to call Abs, expands and substitutes the body of Abs in place of the call. While essentially the same computation is performed, no function call is involved and hence no stack frame is allocated. Because calls to an inline function are expanded, no trace of the function itself will be left in the compiled code. Therefore, if a function is defined inline in one file, it may not be available to other files. Consequently, inline functions are commonly placed in header files so that they can be shared. Generally, the use of inline should be restricted to simple, frequently used functions.
Recursion A function which calls itself is said to be recursive. Recursion is a general programming technique applicable to problems which can be defined in terms of themselves. Take the factorial problem, for instance, which is defined as: - Factorial of 0 is 1. - Factorial of a positive number n is n times the factorial of n-1. The second line clearly indicates that factorial is defined in terms of itself and hence can be expressed as a recursive function:
int Factorial (unsigned int n) { return n == 0? 1: n * Factorial(n-1); }
For n set to 3, the Table below provides a trace of the calls to Factorial. The stack frames for these calls appear sequentially on the runtime stack, one after the other.
A recursive function must have at least one termination condition which can be satisfied. Otherwise, the function will call itself indefinitely until the runtime stack overflows. The Factorial function, for example, has the termination condition n == 0 which, when satisfied, causes the recursive calls to fold back. As a general rule, all recursive functions can be rewritten using iteration. For factorial, for example, an iterative version will be:
int Factorial (unsigned int n) { int result = 1; while (n > 0) result *= n--; return result; }
Default Arguments Default argument is a programming convenience which removes the burden of having to specify argument values for all of a function’s parameters. For example, consider a function for reporting errors:
void Error (char *message, int severity = 0);
Here, severity has a default argument of 0; both the following calls are therefore valid:
Error("Division by zero", 3); // severity set to 3 Error("Round off error"); // severity set to 0
As the first call illustrates, a default argument may be overridden by explicitly specifying an argument.
Lab Overview 2.1. Read the theory and try Control Exercises. 2.2. Develop the algorithm flowchart to solve a numerical integration problem using Rectangle method, Trapezoidal rule and Simpson's rule for a function according to individual case from the Table below. Rectangle method: , where Trapezoidal rule: , where Simpson's rule: , where An interval beginning (a), end (b) and subintervals number (n) should be inputted from a keyboard. User-defined functions should be used to calculate a function value and integral values according to each numerical integration method. 2.3. Write the program code according to the developed algorithm. 2.4. Debug the program, run it and make screenshots. 2.5. Prepare the Lab Report according to the required structure.
Report Structure - Title page - Task overview - Algorithm’s flowchart - Program code - Program running screenshots - Conclusions
Control Exercises 4.1. Given the following definition of a Swap function void Swap (int x, int y) { int temp = x; x = y; y = temp; }
what will be the value of x and y after the following call: x = 10; y = 20; Swap(x, y);
4.2. What will the following program output when executed? char *str = "global"; void Print (char *str) { cout << str << '\n'; { char *str = "local"; cout << str << '\n'; cout <<::str << '\n'; } cout << str << '\n'; }
int main () { Print("Parameter"); return 0; }
References 5.1. Juan Soulié. C++ Language Tutorial. – 2007. – p. 41-53. 5.2. Sharam Hekmat. C++ Essentials. – PragSoft Corporation 2005. – p. 45-64. 5.3. Prata S. C++ Primer Plus (5th Edition). – Sams, 2004. – p. 279-336.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Последнее изменение этой страницы: 2016-04-18; просмотров: 246; Нарушение авторского права страницы; Мы поможем в написании вашей работы! infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 3.142.198.51 (0.011 с.) |