Home > SparkCharts > Cs > C++ > Basic Control Structures

C++


 
 

Basic Control Structures

The basic logic of a program is captured within its control structures. Control structures provide the sequential framework of a program. All conditional tests are Boolean tests for true or false. It is possible to cast non-Boolean values, either implicitly or explicitly, to fulfill the requirements of a conditional test.

 

if/else Statement

The most basic conditional test is an if conditional. If the conditional test of the if statement returns true, then code is executed. Add additional conditional statements either with else (a default case executed if no other if or else if tests are met) and with else if statements. During run-time, only the first true conditional in any if/else statement will have the associated code executed. Any number (including none) of if/else statements are allowed. If the optional code is a single statement, then opening/closing code braces are also optional.

if (conditional test) {
code }
else if (conditional test)
{ code }
....
else { code }

 
 

switch Statement

Each set of cases is broken by a break; statement. Multiple cases can lead into the same set of code. Until a break is met the code continues to run, so a break statement should separate each set of distinct code segments. A break statement is optional after the default case of the switch statement. The switch variable is tested for equality with the test value.

switch (switch variable) {
case test value:
additional cases
code
break;
...
default:
default code
}

 
 

while and do/while Statements

while statements either test then start execution or execute a single iteration of the code block before checking the loop test. If the code is run before testing, then it is a do/while statement. If the while loop is entirely contained in the loop test, opening/closing code braces are optional.

while (conditional test);

or

while (conditional test) {
      code
}

or

do {
      code
} while (conditional test);

 
 

for Loop

for loops function like while and do/while loops, except that the initialization and increment statements are part of the for structure. Braces for self-contained loops or single-line loops are optional.

for (initialization
statement; conditional test;
increment statements
);

or

for (initialization
statement; conditional test;
increment statement
) {
code
}

 
 

break and continue Commands

The commands break; and continue; are useful for altering the flow of control. If a break command is executed in a while, do/while, for, or switch statement, then the execution immediately leaves that control structure. If a continue statement is executed in a while, do/while, or for structure, then the flow of control immediately skips the rest of the code in the loop. In the case of a while or do/while statement, the continue statement means that the program will execute the conditional test. In the case of a for loop, the increment statement is executed, after which the conditional test is checked.

while (conditional test)
{
   code
   continue; /* Will skip
   to conditional test
*/
   code
   break; /* Will
   immediately end loop
   execution completely
*/
   code
}