<< Chapter < Page | Chapter >> Page > |
The control flow of a language specify the order in which operations are performed. Each program includes many statements. Statements are processed one after another in sequence, except where such control statements result in jumps.
An expression such as x = 0 or i++ or printf(. . . .) becomes a statement when it is followed by a semicolon, as in
x=0;
i++;printf(. . . .);
In the C language, the semicolon is a statement terminator.
A block also called a compound 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.
In blocks, declarations and definitions can appear anywhere, mixed in with other code. Note that there is no semicolon after the right brace that ends a block.
{ into I = 0; /* Declarations */
static long a;extern long max;++a; /* Statements */
if( a>= max)
{ . . . } /* A nested block */. . .
}
An expression statement is an expression followed by a semicolon. The syntax is:
[expression] ;
y = x; // Assignment
The expression—an assignment or function call, for example—is evaluated for its side effects. The type and value of the expression are discarded.
A statement consisting only of a semicolon is called an empty statement, and does not perform any operation. For Example
for ( i = 0; str[i] != '\0'; ++i ); // Empty statement
The if statement has two forms:
if(expression) statement
and
if(expression) statement1
else statement2
In the first form, if (and only if) the expression is non-zero, the statement is executed. If the expression is zero, the statement is ignored. Remember that the statement can be compound; that is the way to put several statements under the control of a single if.
The second form is like the first except that if the statement shown as statement1 is selected then statement2 will not be, and vice versa.
Here are the flowcharts of the two forms of if statement
The form involving else works the same way, so we can also write this.
if(expression)
if(expression)statement
elsestatement
this is now ambiguous. It is not clear, except as indicated by the indentation, which of the ifs is responsible for the else. If we follow the rules that the previous example suggests, then the second if is followed by a statement, and is therefore itself a statement, so the else belongs to the first if.
That is not the way that C views it. The rule is that an else belongs to the first if above that hasn't already got an else. In the example we're discussing, the else goes with the second if.
To prevent any unwanted association between an else and an if just above it, the if can be hidden away by using a compound statement, here it is.
if(expression){
if(expression)statement
}elsestatement
if(expression){
Notification Switch
Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?