Skip to the content of the web site.

Lesson 150: Single-statement conditional and looping statements do not need braces

Previous lesson Next lesson


Up to this point, we have always included a body surrounded by braces for if-statements, for-loops and while-loops; for example,

	if ( some condition ) {
		// Some instructions
	} else {
		// Some instructions
	}

	while ( some condition ) {
		// Some instructions
	}

	for ( initialization; some condition; increment ) {
		// Some instructions
	}

There is, however, one special case where the braces are not needed: if there is only one statement in the body; for example:

	if ( counter < 0 )
		counter = 0;
	else
		++counter;

	while ( abs( x - x0 ) > 1e-10 )
		x = f( x, x0 );

	for ( size_t i{0}; i < N; ++i )
		array[i] = 0;

This was a choice made in 1970 when the C programming language was designed at a time when C was being used exclusively to write the Unix operating system at a time where even hard drive space was at a premium and terminal windows had limited display capabilities. For example, the VT100, shown below, from 1978 used the 8-bit Intel 8008 microprocessor and displayed 24 rows and 80 columns of ASCII text. The 1970 VT05 displayed only 20 rows and 72 columns.

Additionally, the original Unix editor was ed, a line editor, meaning that you could only edit one line at a time, and if you wanted to edit a previous line, you would have issue a command to move to editing the next line. It was not a friendly environment, and vi (now vim) was introduced in 1976 to make use of terminals such as the VT100. Thus, it was not unreasonable at the time to have source code in the form of

	if ( counter < 0 ) counter = 0; else ++counter;

	while ( abs( x - x0 ) > 1e-10 ) x = f( x, x0 );

	for ( size_t i{0}; i < N; ++i ) array[i] = 0;

There is even the possibility of having these structures without any body at all:

	if ( ++counter < 0 ); else ++counter;

	while ( array[++i] != target_value );

	for ( size_t i{0}; i < N; array[++i] = 0 );

All of these rely on side-effects, something that was very useful at the time that the C programming language was written, but not very useful today, as it is more likely to be the source of programming errors rather than improved code. For example, suppose a developer came from using Python, where tabs indicate content of a body, not braces. Suppose that developer saw this code:

	if ( counter < 0 )
		counter = 0;

	array[counter] = 0;

and realized that it required a flag to be set. The correct addition would require the programmer to explicitly insert braces:

	if ( counter < 0 ) {
		counter = 0;
		negative_result = true;
	}

	array[counter] = 0;

Accidentally, the programmer may, however, just append the statement:

	if ( counter < 0 )
		counter = 0;
		negative_result = true;

	array[counter] = 0;

Now, at a cursory glance it looks like the two statements should be included in body of the conditional statement, but in reality, the flag will always be set:

	if ( counter < 0 )
		counter = 0;

	negative_result = true;
	array[counter] = 0;

It is in your best interest to avoid this scenario at all costs. If the single-line conditional statement already had braces, the programmer adding the flag would have naturally added it within the braces:

	if ( counter < 0 ) {
		counter = 0;
		negative_result = true;
	}

	array[counter] = 0;

Important concept:
Never use conditional or looping statements without braces. If the body of a any such statement is empty, explicitly tell the reader:

	while ( array[++i] != target_value ) {
		// empty body
	}

Why should you follow this practice? You as an engineer will not develop code, but you may be supervising the environment in which code is being authored and maintained. If you do not maintain strong coding standards, your developers will end up using poor coding practices and this will lead to additional errors; thus your costs will increase and your profits correspondingly will decrease. Period.

One exception: if—else-if

There is exactly one place where we still use this today: recall that an if—else-if works as follows:

	if ( some condition ) {
		// Some code
	} else if ( another condition ) {
		// Some other code
	} else {
		// Alternative code
	}

What is really happening here is the following:

	if ( some condition ) {
		// Some code
	} else {
		if ( another condition ) {
			// Some other code
		} else {
			// Alternative code
		}
	}

However, as the body of the else statement is a single conditional statement (one including an else) the braces are not required, and it is conventional to simply leave them out, but in this situation and this situation only.


Previous lesson Next lesson