1.2.9 Control statements

This section describes the supported control statements.

if statement

The if statement is used to execute an instruction or a block of instructions depending on a condition.
The condition must be of bool type. If it evaluates to false, the code is not executed. If it evaluates to true, the code is executed.
if (condition)
    statement; 
or
if (condition) 
    { 
        statement 1;
        … 
        statement n;
    } 
if statements can be nested, for example:
    if (condition1) 
    { 
        statement1; 
        if (condition2)
        { 
            statement2;
        }
    }

else statement

The else statement is used to append an alternative code block to an if statement. It is executed if the condition of the if statement is false.
if (condition)
    statement; 
else 
    alternative statement; 
if and else statements can be nested. If the relationship is ambiguous, an else always belongs to the last if statement:
if (condition) /* 1 */ 
    if (condition) /* 2 */ 
        statement1; 
    else /* belongs to if 2 */ 
        statement2; 
It is good style, however, to remove ambiguity by using additional blocking:
if (condition) /* 1 */ 
    {
        if (condition) /* 2 */ 
            statement1; 
        else /* belongs to if 2 */ 
            statement2; 
    }
To check for multiple conditions of which only one is true, the following construct can be used (no special elseif instruction exists):
if (condition)
{
} 
else if (condition2) 
{
} 
else if (condition3) 
{
} 
else 
{
}

for statement

The for keyword is followed by an initial value for an integer variable, an exit condition, a modifier function, and a statement or a block containing statements.
The statements in the for loop are executed until the condition is false.
for (loop_var; condition; modifier) 
    statement; 
or
for (loop_var; condition; modifier) 
{ 
    statement1; 
    statement2; 
}
for statements can be nested.
If the loop variable is declared in the for statement, its use is local to the for block:
for (int i; i<3; ++i) 
{ 
    statement1; 
    statement2; 
} 

while statement

The while keyword is followed by a condition (which must evaluate to a bool) and a statement or a block containing statements. The statements in the while loop are executed until the condition is false. If the condition is false when entering the while loop the statements are not executed.
while(condition) 
    statement; 
or
while(condition) 
{ 
    statement1; 
    statement2; 
}
Loop statements can be nested:
while (condition) 
{
     … 
    while (condition) 
    {
        … 
    }
    …
}
The do while form is similar to the while form except that the statements are evaluated before the test. If the condition is false when entering the while loop the statements are executed once.
do 
statement 
while(condition); 
or
do 
{ 
    statement1; 
    statement2; 
}
while(condition);

break statement

The break statement can be used to prematurely leave while, do while, or for loops. If used in nested loops the innermost loop is exited.
while (condition) 
{
    if (condition2)
        break;
    …
}

continue statement

The keyword continue can be used to jump over the remainder of a while, do while, or for loop body and to continue with the evaluation of the condition.
while (condition) 
{
    if (condition2) 
        continue; 
    …
} 
If used in nested loops, the innermost loop is continued.
Non-ConfidentialPDF file icon PDF versionARM DUI0371M
Copyright © 2007-2014 ARM. All rights reserved.