IC supports most of the standard C control structures. One notable
exception is the switch
statement, which is not supported.
A single C statement is ended by a semicolon. A series of statements may be grouped together into a block using curly braces. Inside a block, local variables may be defined.
The if else
statement is used to make decisions. The syntax is:
if (expression) statement-1 else statement-2
expression is evaluated; if it is not equal to zero (e.g., logic true), then statement-1 is executed.
The else
clause is optional. If the if
part of the
statement did not execute, and the else
is present, then
statement-2 executes.
The syntax of a while
loop is the following:
while (expression) statement
while
begins by evaluating expression. If it is false,
then statement is skipped. If it is true, then statement is
evaluated. Then the expression is evaluated again, and the same check
is performed. The loop exits when expression becomes zero.
One can easily create an infinite loop in C using the while
statement:
while (1) statement
The syntax of a for
loop is the following:
for (expr-1;expr-2;expr-3) statement
This is equivalent to the following construct using while
:
expr-1; while (expr-2) { statement expr-3; }
Typically, expr-1 is an assignment, expr-2 is a relational expression, and expr-3 is an increment or decrement of some manner. For example, the following code counts from 0 to 99, printing each number along the way:
int i; for (i= 0; i < 100; i++) printf("%d\n", i);
Use of the break
provides an early exit from a while
or a
for
loop.