| C Programming Statements | Preprocessor reference |
Astatement is a command given to the computer that instructs the computer to take a specific action, such as display to the screen, or collect input. A computer program is made up of a series of statements.
In C, a statement can be any of the following:
A statement can be preceded by a label. Three types of labels exist in C.
A simple identifier followed by a colon (:) is a label. Usually, this label is the target of agoto statement.
Withinswitch statements,case anddefault labeled statements exist.
A statement of the form
caseconstant-expression:statement
indicates that control will pass to this statement if the value of the control expression of theswitch statement matches the value of theconstant-expression. (In this case, the type of theconstant-expression must be an integer or character.)
A statement of the form
default:statement
indicates that control will pass to this statement if the control expression of theswitch statement does not match any of theconstant-expressions within theswitch statement. If thedefault statement is omitted, the control will pass to the statement following theswitch statement. Within aswitch statement, there can be only onedefault statement, unless theswitch statement is within anotherswitch statement.
Acompound statement is the way C groups multiple statements into a single statement. It consists of multiple statements and declarations within braces (i.e.{ and}). In the ANSI C Standard of 1989-1990, a compound statement contained an optional list of declarations followed by an optional list of statements; in more recent revisions of the Standard, declarations and statements can be freely interwoven through the code. The body of a function is also a compound statement by rule.
Anexpression statement consists of an optional expression followed by a semicolon (;). If the expression is present, the statement may have a value. If no expression is present, the statement is often called thenull statement.
Theprintf function calls are expressions, so statements such asprintf ("Hello World!\n"); are expression statements.
Three types of selection statements exist in C:
if(expression)statement
In this type of if-statement, the sub-statement will only be executed iff the expression is non-zero.
if(expression)statementelsestatement
In this type of if-statement, the first sub-statement will only be executed iff the expression is non-zero; otherwise, the second sub-statement will be executed. Eachelse matches up with the closest unmatchedif, so that the following two snippets of code are not equal:
if(expression)if(secondexpression)statement1;elsestatement2;if(expression){if(secondexpression)statement1;}elsestatement2;
because in the first, theelse statement matches up with the if statement that hassecondexpression for a control, but in the second, the braces force theelse to match up with the if that hasexpression for a control.
Switch statements are also a type of selection statement. They have the format
switch(expression)statement
The expression here is an integer or a character. The statement here is usually compound and it contains case-labeled statements and optionally a default-labeled statement. The compound statement should not have local variables as the jump to an internal label may skip over the initialization of such variables.
C has three kinds of iteration statements. The first is a while-statement with the form
while(expression)statement
The substatement of a while runs repeatedly as long as the control expression evaluates to non-zero at the beginning of each iteration. If the control expression evaluates to zero the first time through, the substatement may not run at all.
The second is a do-while statement of the form
dostatementwhile(expression);
This is similar to a while loop, except that the controlling expression is evaluated at the end of the loop instead of the beginning and consequently the sub-statement must execute at least once.
The third type of iteration statement is the for-statement. In ANSI C 1989, it has the form
for(expressionopt;expressionopt;expressionopt)statement
In more recent versions of the C standard, a declaration can substitute for the first expression. Theopt subscript indicates that the expression is optional.
The statement
for(e1;e2;e3)s;
is the rough equivalent of
{e1;while(e2){s;e3;}}
except for the behavior ofcontinue statements withins.
Thee1 expression represents an initial condition;e2 a control expression; ande3 what to happen on each iteration of the loop. Ife2 is missing, the expression is considered to be non-zero on every iteration, and only abreak statement withins (or a call to a non-returning function such asexit orabort) will end the loop.
C has four types of jump statements. The first, thegoto statement, is used sparingly and has the form
gotoidentifier;
This statement transfers control flow to the statement labeled with the given identifier. The statement must be within the same function as thegoto.
The second, the break statement, with the form
break;
is used within iteration statements andswitch statements to pass control flow to the statement following the while, do-while, for, or switch.
The third, the continue statement, with the form
continue;
is used within the substatement of iteration statements to transfer control flow to the place just before the end of the substatement. Infor statements the iteration expression (e3 above) will then be executed before the controlling expression (e2 above) is evaluated.
The fourth type of jump statement is thereturn statement with the form
returnexpressionopt;
This statement returns from the function. If the function return type isvoid, the function may not return a value; otherwise, the expression represents the value to be returned.
| C Programming Statements | Preprocessor reference |