Flow control

Selection
if else
if (expression1)
instruction1;
else if (expression2)
instruction2;
…
else
instructionN;Ternary operator
expr1 ? inst2 : inst3equal to:
if (exp1)
inst2
else
inst3switch
switch(expression)
{
case constant1:
instruction1
break;
…
default :
istruzione
break;
} expression= integerthe instruction in a case is executed if
expressionhas the valueconstant1the
defaultinstruction 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.
defaultdoes not need a break because it's the last expression and would be executed due to fall-throughbreakis included for code symmetry.
Iteration
while
while(expression)
instructionit repeats the execution of
instructionwhileexpressionistrue
Infinite cycle:
while(1)
instructiondo while
do
instruction
while(expression)it executes
instructionone timein the next iterations, it executes
instructionwhileexpressionistrue
for
for(expr1;expr2;expr3)
instructionexpr1= variable initializationexpr2= condition to be checkedexpr3= variable value increase/decreaseit executes
instructionwhileexpr2istrueexpr1can be removed if the inizialization was done outside the cycle
Infinite cycle:
for( ; ; )
instructionJump
break
breakIt exits from the internal cycle where it is, without verifying the cycle conditions.
continue
continueit 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 LABELIt jumps to the code block labeled as LABEL:
LABEL: codiceComma operator
expr1, expr2expr1is evaluatedexpr2is evaluatedthe value returned by the
,operator isexpr1
Last updated