Thursday 26 July 2012

Statements

Statements

A program consists of a collection of functions (one of which must be int main() {...}) and type and object declarations. A function may contain declarations and statements. Statements have the following forms, where s is a statement, and t is a true/false expression.
s; // Expression or declaration
; // Empty statement
{s; s;} // A block of 0 or more statements is a statement
if (t) s; // If t is true then s
if (t) s; else s; // else is optional
while (t) s; // Loop 0 or more times
for (s1; t; s2) s; // s1; while (t) {s; s2;}
break; // Jump from while, for, do, switch
return x; // Return x to calling function
try {throw x;} // Throw exception, abort if not caught, x has any type
catch (T y) {s;} // if x has type T then y=x, jump to s
catch (...) {s;} // else jump here (optional)
do s; while (t); // (uncommon) s; while (t) s;
continue; // (uncommon) Start next loop of while, for, do
switch (i) { // (uncommon) Test int expression i to const C
case C: s; break; // if (i==C) go here
default: s; // optional, else go here
}
label: goto label; // (rare) Jump to label within a function

A statement may be a declaration or an expression. Objects and types declared in a block are local to that block. (Functions cannot be defined locally). It is normal (but not required) to show statements on separate lines and to indent statements enclosed in a block. If braces are optional, we indent anyway. For instance,
{ // start of block
int a[10], i=0, j; // declaration
a[i+2]=3; // expression
} // end of block, a, i, and j are destroyed
declares the array of int a with elements a[0] through a[9] (whose values are initially undefined), i with initial value 0, and j with an undefined initial value. These names can only be used in scope, which is from the declaration to the closing brace.

The for loop is normally used for iteration. For instance, the following both exit the loop with i set to the index of the first element of a such that a[i] is 0, or to 10 if not found.
for (i=0; i<10; i=i+1) { i=0;
if (a[i]==0) { while (i<10) {
break; if (a[i]==0)
} break;
} i=i+1;
}
The braces in the for loop are optional because they each enclose a single statement. In the while loop, the outer braces are required because they enclose 2 statements. All statements are optional: for (;;) loops forever. The first statement in a for loop may declare a variable local to the loop. for (int i=0; i<10; i=i+1)

It is only possible to break from the innermost loop of a nested loop. continue in a for loop skips the rest of the block but executes the iteration (s2) and test before starting the next loop.

return x; causes the current function to return to the caller, evaluating to x. It is required except in functions returning void, in which case return; returns without a value. The value returned by main() has no effect on program behavior and is normally discarded. However it is available as the $status in a UNIX csh script or ERRORLEVEL in a Windows .BAT file.
int sum(int x, int y) { // Function definition
return x+y;
}
int main() {
int a=sum(1,2); // a=3;
return 0; // By convention, nonzero indicates an error
}

A test of several alternatives usually has the form if (t) s; else if (t) s; else if (t) s; ... else s;. A switch statement is an optimization for the special case where an int expression is tested against a small range of constant values. The following are equivalent:
switch (i) { if (i==1)
case 1: j=1; break; j=1;
case 2: // fall thru else if (i==2 || i==3) // || means "or else"
case 3: j=23; break; j=23;
default: j=0; else
} j=0;

throw x jumps to the first catch statement of the most recently executed try block where the parameter declaration matches the type of x, or a type that x can be converted to, or is .... At most one catch block is executed. If no matching catch block is found, the program aborts (Unexpected exception). throw; with no expression in a catch block throws the exception just caught. Exceptions are generally used when it is inconvenient to detect and handle an error in the same place.
void f() {
throw 3;
}

int main() {
try {
f();
}
catch(int i) { // Execute this block with i = 3
throw; // throw 3 (not caught, so program aborts)
}
catch(...) { // Catch any other type
}
}

No comments:

Post a Comment