C is a free-form language.
Bracing style varies from programmer to programmer and can be the subject of debate.
Compound statements
In the items in this section, any <statement> can be replaced with a compound statement. Compound statements have the form:
{
<optional-declaration-list>
<optional-statement-list>
}
and are used as the body of a function or anywhere that a single statement is expected. The declaration-list declares variables to be used in that scope, and the statement-list are the actions to be performed. Brackets define their own scope, and variables defined inside those brackets will be automatically deallocated at the closing bracket. Declarations and statements can be freely intermixed within a compound statement..
Selection statements
C has two types of selection statements: the if statement and the switch statement.
The if statement is in the form:
if (<expression>)
{
<statement1>
}
else
{
<statement2>
}
In the if statement, if the <expression> in parentheses is nonzero (true), control passes to <statement1>. If the else clause is present and the <expression> is zero (false), control will pass to <statement2>. The else <statement2> part is optional and, if absent, a false <expression> will simply result in skipping over the <statement1>. An else always matches the nearest previous unmatched if; braces may be used to override this when necessary, or for clarity.
The switch statement causes control to be transferred to one of several statements depending on the value of an expression, which must have integral type. The substatement controlled by a switch is typically compound. Any statement within the substatement may be labeled with one or more case labels, which consist of the keyword case followed by a constant expression and then a colon (:). The syntax is as follows:
switch (<expression>)
{
case <label1> :
<statements 1>
case <label2> :
<statements 2>
break;
default :
<statements 3>
}
No two of the case constants associated with the same switch may have the same value. There may be at most one default label associated with a switch. If none of the case labels are equal to the expression in the parentheses following switch, control passes to the default label or, if there is no default label, execution resumes just beyond the entire construct.
Iteration statements
C has three forms of iteration statement:
do
{
<statement>
}
while ( <expression> ) ;
while ( <expression> )
{
<statement>
}
for ( <expression> ; <expression> ; <expression> )
{
<statement>
}
In the while and do statements, the sub-statement is executed repeatedly so long as the value of the expression remains non-zero (equivalent to true). With while, the test, including all side effects from <expression>, occurs before each iteration (execution of <statement>); with do, the test occurs after each iteration. Thus, a do statement always executes its sub-statement at least once, whereas while may not execute the sub-statement at all.
The statement:
for (e1; e2; e3)
s;
is equivalent to:
e1;
while (e2)
{
s;
cont:
e3;
}
except for the behaviour of a continue; statement (which in the for loop jumps to e3 instead of e2). If e2 is blank, it would have to be replaced with a 1.
Any of the three expressions in the for loop may be omitted. A missing second expression makes the while test always non-zero, creating a potentially infinite loop.
Jump statements
Jump statements transfer control unconditionally. There are four types of jump statements in C: goto, continue, break, and return.
A continue statement may appear only within an iteration statement and causes control to pass to the loop-continuation portion of the innermost enclosing iteration statement. That is, within each of the statements
while (expression)
{
/* ... */
cont: ;
}
do
{
/* ... */
cont: ;
} while (expression);
for (expr1; expr2; expr3) {
/* ... */
cont: ;
}
a continue not contained within a nested iteration statement is the same as goto cont.
The break statement is used to end a for loop, while loop, do loop, or switch statement. Control passes to the statement following the terminated statement.
A function returns to its caller by the return statement. When return is followed by an expression, the value is returned to the caller as the value of the function. Encountering the end of the function is equivalent to a return with no expression.