For illustration we will use the syntax for the if then else control structure within the C++ programming language. However this problem generally exists for all control structures within any language that requires the use of compound statements. The syntax is:
if (expression)
statement;
else
statement;Within the C++ programming language there can be only one statement listed as the action part of a control structure. Often, we will want to do more than one statement. This problem is overcome by creating a compound statement. The brace symbols – the opening { and the closing } - are used to create a compound statement. For example:
if(expression)
{
statement;
statement;
}
else
{
statement;
statement;
}Because programmers often forget that they can have only one statement listed as the action part of a control structure; the C++ programming industry encourages the use of indentation (to see the action parts clearly) and the use of compound statements (braces), even if there is only one action. Thus:
if(expression)
{
statement;
}
else
{
statement;
}By writing code in this manner, if the programmer modifies the code by adding more statements to either the action true or the action false; they will not introduce either compiler or logic errors. Using indentation and braces should become standard practice for C++ programmers and programmers in any other language that require the use of compound statements with the control structures.







If Then Else

"Used in the Computer Programming Fundamentals I course."