Summary
Branching causes your program to depart from a top-down statement-by-statement execution.
A method call is the most common form of unconditional branching. When the method completes, execution returns to the point where it left off.
Conditional branching enables your program to branch based on runtime conditions, typically based on the value or relative value of one or more objects or variables.
The
ifconstruct executes a statement if a condition is true and skips it otherwise.When the condition in an
ifstatement is actually two conditions joined by anoroperator, if the first condition evaluates totrue, the second condition will not be evaluated at all. This is called short-circuiting.The
if…elseconstruct lets you take one set of actions if the condition tested evaluates true, and a different set of actions if the condition tested evaluates false.ifstatements can be nested to evaluate more complex conditions.The
switchstatement lets you compare the value of an expression with several constant values (integers, enumerated constants, or strings), and take action depending on which value matches.It is good programming practice for
switchstatements to include adefaultstatement that executes if no other matches are found.Iteration, or looping, allows you to take the same action several times consecutively. Iterations are typically controlled by a conditional expression.
The
gotostatement is used to redirect execution to another point in the program, but its use is typically ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access