Control Statements Introduction

Normal program execution proceeds sequentially from one statement to the next after it in the source file, unless a control flow statement is encountered.

When a function is used in an expression in any statement, that causes the control flow to be redirected to the instructions that carry out that function. This is described in some depth in the tutorial section from the main menu entitled: Program. When the function has completed its work, it uses the return statement:

	return;

to come back to where it left off in the calling function. The return statement can be, as shown, without a return value when the called function is of type "void". If it is of any other type, then the called function must use a statement of the form:

	return expression;
or
	return(expression);

where 'expression' must be of the same as the return type of the function which is returning a value.

A function may be called from more than one place in a program. The called function will always know where to continue execution of the statements so that it executes the next instruction after the one that called it. How the function knows where to return, varies from computer to computer, andcompiler to compiler - it is an implementation detail.

All of the other control flow statements stay within the function that executes them. These other control flow statements start with one of the following keywords:

	goto        while
	if          do
	switch      for
	break       continue

Each of these 8 types of control flow will be illustrated by one or more short program fragments and by a graphical representation called a 'flow chart' to show how they behave.


© 1991-2008 Prem Sobel. All Rights Reserved.