if (condition) {
statements
}
branch to next if condition is false
code for statements
next:
if (condition) {
then_statements
} else {
else_statements
}
branch to else if condition is false
code for then_statements
unconditional branch to next
else:
code for else_statements
next:
while (condition) {
statements
}
loop_top:
branch to next if condition is false
code for statements
unconditional branch to loop_top
next:
unconditional branch to condition_test
loop_top:
code for statements
condition_test:
branch to loop_top if condition is true
next:
do {
statements
} while (condition);
loop_top:
code for statements
condition_test:
branch to loop_top if condition is true
next:
The label condition_test is only needed if one of the
statements in statements is a continue statement.
for (initialization; condition; increment) {
statements
}
code for initialization
unconditional branch to condition_test
loop_top:
code for statements
increment:
code for increment
condition_test:
branch to loop_top if condition is true
next:
The label increment is only needed if one of the statements in
statements is a continue statement.
Break statements
A break statement can occur inside a C loop or switch statement.
It is implemented as a unconditional branch to a label immediately
following the loop or switch statement.
This label is named next in the loop and switch assembler
code.
In assembly language, a conditional branch can be used to implement a conditional break statement. This has no direct equivalent in C.
Continue statements
A continue statement can occur inside a C loop.
For while and do-while loops, it is implemented as a unconditional branch
to a label for the condition test of the while or do-while statement.
This label is named loop_top in the while loop assembler code,
and condition_test in the and do-while assembler code and the
while loop alternate assembler code.
For a for loop, a continue statement is implemented as a unconditional
branch to a
label immediately preceding the code to implement the increment
statements.
This label is named increment in the for loop assembler code.
In assembly language, a conditional branch can be used to implement a conditional continue statement. This has no direct equivalent in C.
switch (expression) {
case val_1:
case_1_statements
case val_2:
case_2_statements
.
.
.
case val_n:
case_n_statements
default:
default_statements
}
code to evaluate expression and save its value in exp
branch to case_1 if exp equals val_1
branch to case_2 if exp equals val_2
.
.
.
branch to case_n if exp equals val_n
unconditional branch to default (or next if default clause is omitted)
case_1:
code for case_1_statements
case_2:
code for case_2_statements
.
.
.
case_n:
code for case_n_statements
default:
code for default_statements
next:
Normally, C switch statements have a break statement at the end of each
case.
This is implemented with an unconditional branch to the next
label.