Flow control

Selection
if else
if (expression1)
instruction1;
else if (expression2)
instruction2;
…
else
instructionN;
Ternary operator
expr1 ? inst2 : inst3
equal to:
if (exp1)
inst2
else
inst3
switch
switch(expression)
{
case constant1:
instruction1
break;
…
default :
istruzione
break;
}
expression
= integerthe instruction in a case is executed if
expression
has the valueconstant1
the
default
instruction is executed if all the other cases were not executed
Falling Through
Normally (if break
is not included), the case
block associated with the value of the expression is executed, and then all subsequent case blocks would also be executed.
default
does not need a break because it's the last expression and would be executed due to fall-throughbreak
is included for code symmetry.
Iteration
while
while(expression)
instruction
it repeats the execution of
instruction
whileexpression
istrue
Infinite cycle:
while(1)
instruction
do while
do
instruction
while(expression)
it executes
instruction
one timein the next iterations, it executes
instruction
whileexpression
istrue
for
for(expr1;expr2;expr3)
instruction
expr1
= variable initializationexpr2
= condition to be checkedexpr3
= variable value increase/decreaseit executes
instruction
whileexpr2
istrue
expr1
can be removed if the inizialization was done outside the cycle
Infinite cycle:
for( ; ; )
instruction
Jump
break
break
It exits from the internal cycle where it is, without verifying the cycle conditions.
continue
continue
it jumps to the end of the block cycle, by terminating the current iteration
then, it verifies the cycle condition before executing the next iteration
goto LABEL
goto LABEL
It jumps to the code block labeled as LABEL
:
LABEL: codice
Comma operator
expr1, expr2
expr1
is evaluatedexpr2
is evaluatedthe value returned by the
,
operator isexpr1
Last updated