Flow control

Selection

if else

Ternary operator

equal to:

switch

  • expression = integer

  • the instruction in a case is executed if expression has the value constant1

  • 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-through

  • break is included for code symmetry.

Iteration

while

  • it repeats the execution of instruction while expression is true

Infinite cycle:

do while

  • it executes instruction one time

  • in the next iterations, it executes instruction while expression is true

for

  • expr1 = variable initialization

  • expr2 = condition to be checked

  • expr3 = variable value increase/decrease

  • it executes instruction while expr2 is true

  • expr1 can be removed if the inizialization was done outside the cycle

Infinite cycle:

Jump

break

It exits from the internal cycle where it is, without verifying the cycle conditions.

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

It jumps to the code block labeled as LABEL:

Comma operator

  • expr1 is evaluated

  • expr2 is evaluated

  • the value returned by the , operator is expr1

Last updated