Information Technology Reference
In-Depth Information
Flow-of-Control
Methods contain most of the code for the actions that make up a program. The remainder is in
other function members, such as properties and operators—but the bulk is in methods.
The term flow-of-control refers to the flow of execution through your program. By default,
program execution moves sequentially from one statement to the next. The control statements
allow you to modify the order of execution.
In this section, I will just mention some of the available control statements you can use in
your code. Chapter 9 covers them in detail.
￿
Selection statements: These statements allow you to select which statement, or block of
statements, to execute.
- if : Conditional execution of a statement.
- if ... else : Conditional execution of one statement or another.
- switch : Conditional execution of one statement from a set.
￿
Iteration statements: These statements allow you to loop, or iterate, on a block of
statements.
- for : Loop—testing at the top.
- while : Loop—testing at the top.
- do : Loop—testing at the bottom.
- foreach : Execute once for each member of a set.
￿
Jump statements: These statements allow you to jump from one place in the block or
method to another.
- break : Exit the current loop.
- continue : Go to the bottom of the current loop.
- goto : Go to a named statement.
- return : Return execution to the calling method.
For example, the following method shows several of the flow-of-control statements. Don't
worry about the details.
void SomeMethod()
{
int IntVal = 3;
Equality comparison operator
if( IntVal == 3 ) // if statement
Console.WriteLine("Value is 3.");
for( int i=0; i<5; i++ ) // for statement
Console.WriteLine("Value of i: {0}", i);
}
Search WWH ::




Custom Search