[ Pobierz całość w formacie PDF ]

declaration statement. All of the following are declaration statements:
type_spec(a); // declaration
type_spec(*b)(); // declaration
type_spec(c)=23; // declaration
type_spec(d),e,f,g=0; // declaration
type_spec(h)(e,3); // declaration
v  Chapter 3. Declarations on page 23
v  Chapter 5. Expressions and Operators on page 71
v  Function Calls ( ) on page 78
Block Statement
A block statement, or compound statement, lets you group any number of data
definitions, declarations, and statements into one statement. All definitions,
declarations, and statements enclosed within a single set of braces are treated as a
single statement. You can use a block wherever a single statement is allowed.
A block statement has the form:
{ }
type_definition statement
file_scope_data_declaration
block_scope_data_declaration
In C, Any definitions and declarations must come before the statements.
A block defines a local scope. If a data object is usable within a block and its
identifier is not redefined, all nested blocks can use that data object.
Example of Blocks
The following program shows how the values of data objects change in nested
blocks:
/**
** This example shows how data objects change in nested blocks.
**/
#include
int main(void)
{
int x = 1; /* Initialize x to 1 */
int y = 3;
if (y > 0)
{
int x = 2; /* Initialize x to 2 */
printf("second x = %4d\n", x);
}
Chapter 8. Statements 145
Block Statement
printf("first x = %4d\n", x);
return(0);
}
The program produces the following output:
second x = 2
first x = 1
Two variables namedxare defined inmain. The first definition ofxretains storage
whilemainis running. However, because the second definition ofxoccurs within a
nested block,printf("secondx=%4d\n",x);recognizesxas the variable defined
on the previous line. Becauseprintf("firstx=%4d\n",x);is not part of the
nested block,xis recognized as the first definition ofx.
v  Storage Class Specifiers on page 24
v  Type Specifiers on page 30
if Statement
An if statement lets you conditionally process a statement when the
specified test expression, implicitly converted to bool, evaluates to true. If the
implicit conversion to bool fails the program is ill-formed.
In C, an if statement lets you conditionally process a statement when the
specified test expression evaluates to a nonzero value. The test expression must be
of arithmetic or pointer type.
You can optionally specify an else clause on the if statement. If the test expression
evaluates to false (or in C, a zero value) and an else clause exists, the statement
associated with the else clause runs. If the test expression evaluates to true, the
statement following the expression runs and the else clause is ignored.
An if statement has the form:
if ( expression ) statement
else statement
When if statements are nested and else clauses are present, a given else is
associated with the closest preceding if statement within the same block.
A single statement following any selection statements (if, switch) is treated as a
compound statement containing the original statement. As a result any variables
declared on that statement will be out of scope after the if statement. For example:
if (x)
int i;
is equivalent to:
if (x)
{ int i; }
146 C/C++ Language Reference
if Statement
Variableiis visible only within the if statement. The same rule applies to the else
part of the if statement.
Examples of if Statements
The following example causesgradeto receive the valueAif the value ofscoreis
greater than or equal to90.
if (score >= 90)
grade = 'A';
The following example displaysNumberispositiveif the value ofnumberis
greater than or equal to0. If the value ofnumberis less than0, it displaysNumber
is negative.
if (number >= 0)
printf("Number is positive\n");
else
printf("Number is negative\n");
The following example shows a nested if statement:
if (paygrade == 7)
if (level >= 0 && level
salary *= 1.05;
else
salary *= 1.04;
else
salary *= 1.06;
cout
The following example shows a nested if statement that does not have an else
clause. Because an else clause always associates with the closest if statement,
braces might be needed to force a particular else clause to associate with the
correct if statement. In this example, omitting the braces would cause the else
clause to associate with the nested if statement.
if (kegs > 0) {
if (furlongs > kegs)
fpk = furlongs/kegs;
}
else
fpk = 0;
The following example shows an if statement nested within an else clause. This
example tests multiple conditions. The tests are made in order of their appearance.
If one test evaluates to a nonzero value, a statement runs and the entire if
statement ends.
if (value > 0)
++increase;
else if (value == 0)
++break_even;
else
++decrease;
v  Boolean Variables on page 33
Chapter 8. Statements 147
switch Statement
switch Statement
A switch statement lets you transfer control to different statements within the switch
body depending on the value of the switch expression. The switch expression must
evaluate to an integral or enumeration value. The body of the switch statement
contains case clauses that consist of
v A case label
v An optional default label
v A case expression
v A list of statements.
If the value of the switch expression equals the value of one of the case
expressions, the statements following that case expression are processed. If not, the
default label statements, if any, are processed.
A switch statement has the form:
switch ( expression ) switch_body
The switch body is enclosed in braces and can contain definitions, declarations, case
clauses, and a default clause. Each case clause and default clause can contain
statements.
{
type_definition case_clause
file_scope_data_declaration
block_scope_data_declaration
}
default_clause case_clause
Note: An initializer within a type_definition, file_scope_data_declaration or
block_scope_data_declaration is ignored.
A case clause contains a case label followed by any number of statements. A case
clause has the form:
case_label statement
A case label contains the word case followed by an integral constant expression and
a colon. The value of each integral constant expression must represent a different
value; you cannot have duplicate case labels. Anywhere you can put one case
label, you can put multiple case labels. A case label has the form:
148 C/C++ Language Reference
switch Statement
case integral_constant_expression :
A default clause contains a default label followed by one or more statements. You
can put a case label on either side of the default label. A switch statement can
have only one default label. A default_clause has the form:
default : statement
case_label case_label
The switch statement passes control to the statement following one of the labels or
to the statement following the switch body. The value of the expression that
precedes the switch body determines which statement receives control. This
expression is called the switch expression.
The value of the switch expression is compared with the value of the expression in
each case label. If a matching value is found, control is passed to the statement
following the case label that contains the matching value. If there is no matching
value but there is a default label in the switch body, control passes to the default
labelled statement. If no matching value is found, and there is no default label
anywhere in the switch body, no part of the switch body is processed.
When control passes to a statement in the switch body, control only leaves the [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • helpmilo.pev.pl
  •