Skip to content

Downloading Tools

Control structures are essential for managing the flow of a program in C++. They determine the order in which statements are executed and allow your code to make decisions, repeat actions, or choose between multiple paths.

In this section, we’ll explore the four main types of control structures: sequential execution, decision-making, case statements, and loops, which form the foundation of logic in programming.

In sequential execution, each command in the program is executed one after the other, in the order they appear. This is the simplest form of control flow and forms the basis for all programs, where instructions are carried out step by step.

The most common decision structure in C++ is the if statement, which directs the flow of execution depending on the truth value of the condition.

A flow chart that shows a sequential flow chart

In decision-making control structures, an expression is evaluated to determine whether it is true or false. Based on the result, one of two possible paths is executed. This allows your program to make choices and respond to different conditions dynamically.

A flow chart that shows a decision based flow chart

The switch statement allows your program to choose from multiple paths based on the value of a variable. Each possible value is matched to a specific case, and the corresponding block of code is executed. This is especially useful when you have several distinct conditions to handle, making your code more organized and readable compared to using multiple if-else statements.

A flow chart that shows a case based flow chart

Repetition control structures, also known as loops, are used when a set of commands needs to be executed multiple times. Loops allow you to repeat actions efficiently, either for a fixed number of iterations or while a specific condition is true. Common loop structures in C++ include for, while, and do-while loops, each suited for different types of repetition scenarios.

A flow chart that shows a repetition based flow chart

All control structures follow similar style requirements:

  • There should be a blank space between the statement type and the parenthesis.

  • There should NOT be a blank space after the opening parenthesis and before the closing parenthesis.

  • There should be a single space between the parenthesis and the opening brace.

  • The opening brace should be on the same line as the statement.

  • There should NOT be any blank lines between the braces and the code block.

  • Braces are REQUIRED for while loops

type (condition) {
// Do something
}

Here are some errors that are thrown for improper styling:

Terminal window
# General Errors
Redundant blank line at the start of a code block should be deleted. [whitespace/blank_line] [2]
Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
{ should almost always be at the end of the previous line [whitespace/braces] [4]
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
Empty loop bodies should use {} or continue [whitespace/empty_loop_body] [5]
# Else Errors
An else should appear on the same line as the preceding } [whitespace/newline] [4]
Else clause should be indented at the same level as if. Ambiguous nested if/else chains require braces. [readability/braces] [4]
If an else has a brace on one side, it should have it on both [readability/braces] [5]