If a loop or conditional has a short enough statement (i.e., loop body, or what to do if the condition is true) that it can all fit on one line without squishing out whitespace to the point of being difficult to read, you may keep it on one line. Otherwise, it must go on a separate line. For example:
/*
* acceptable this way, or without block delimiters,
* but NOT all on one line
*/
if (very_long_logical_expression)
{
very_long_variable = other_long_variable + yet_another;
}
/* acceptable any of the three ways */
if (short_expr) short_var = other_short_var + yet_another;
If either the condition/loop predicate or the statement, or both, must go on multiple lines itself, the statement should be surrounded by block delimiters. For example, the following two are acceptable as shown, but NOT without block delimiters:
if (logical_expression)
{
printf ("The %s %s %s %ss %s the %s %s.\n",
description1a,
description1b,
animal1,
action,
adverb,
description2,
animal2);
}
if (widgetPtr->height > globalParms.maxWidgetHeight ||
widgetPtr->width > globalParms.maxWidgetWidth)
{
printf ("Widget %d is too big\n", widgetNum);
}
In C, if a "for" loop's initializer, condition, and reinitializer cannot all fit on one line, they should be stacked so that the starts of the condition and reinitializer are directly below the start of the initializer. For example:
for (longVariableName = 0;
longVariableName < upperLimit;
longVariableName++)
Likewise, any function call, while-loop condition, etc. that cannot fit on one line, should be further indented. There are three alternative methods:
veryLongVariableName = VeryLongFunctionName (variable1,
variable2, variable3, variable4, variable5, variable6);
variableName = FunctionName (variable1,
variable2,
variable3,
variable4,
variable5,
variable6);
variableName = FunctionName (variable1, variable2, variable3,
variable4, variable5, variable6);