Control Statements

C# offers three types of control statements:

  1. Selection Statements – This consists of if, else, switch, and case branching.
  2. Iteration Statements – This consists of do, for, foreach, and while looping.
  3. Jump Statements – This consists of break, continue, return, and goto statements.

SELECTION

The if statement determines which statement to execute based on a specified condition. Review its syntax below:

if (statement)
{
	statement;
}

Review this example of an if statement:

if (BAC >= 0.08)
{
	Console.WriteLine(“Intoxicated.”);
}

The else statement provides a variation of the if statement which executes a default statement when the condition of the if statement is not true. Review its syntax below:

if (statement)
{
	statement;
}
else
{
	statement;
}

Review this example of an else statement:

if (BAC >= 0.08)
{
	Console.WriteLine(“Intoxicated.”);
}
else
{
	Console.WriteLine(“Review other facts.”);
}

The switch statement selects from a list of possible statements based on a set of results. Review its syntax below:

datatype AVariable;
switch (AVariable)
{
	case s:
		statement;
		break;
	case t:
		statement;
		break;
	default:
		statement;
		break;
}

Review this example of a switch statement:

int EquipmentTest;
switch (EquipmentTest)
{
	case 30:
	Console.WriteLine(“Replace.”);
		break;
	case 60:
	Console.WriteLine(“Perform service.”);
		break;
	case 90:
	Console.WriteLine(“No service required.”);
		break;
	default:
	Console.WriteLine(“Perform another test.”);
		break;
}

ITERATION

The do statement repeats the execution of a statement(s) until a specified condition is false. It always executes once before evaluating the condition. Review its syntax below:

do
{
	statement;
} while (statement);

Review this example of a do statement:

do
{
	Console.WriteLine(“Processing...”);
} while (complete <= 25);

The for statement repeats the execution of a statement until a specified condition is false. It proves most useful when the developer knows the exact number of necessary iterations. Its statements initialize the iteration variable, state the related condition, and state the iteration operation. Note that all statements within a for statement are optional, and omissions can create infinite loops. Review its syntax below:

for (initializer; condition; iterator)
{
	statement;
}

Review this example of a for statement:

for (int products = 1; products <= 126; products++)
{
	Console.WriteLine(“Searching...”);
}

The foreach statement repeats statements for each element of an object collection or array. Review its syntax below:

foreach (int element in NameOfArray)
	{
		statement;
	}

Review this example of a foreach statement:

foreach (string NewMember in department)
	{
		Console.WriteLine(NewMember);
	}

The while statement executes a statement(s) while a condition evaluates to true. Review its syntax below:

while (statement)
	{
		statement;
	}

Review this example of a while statement:

while (level < 200)
	{
		Console.WriteLine(“Continue.”);
	}

JUMP

The following statements are used to pass control:

Statement Description
break; It stops the nearest control statement to its location, and passes control to the next statement, if it exists.
continue; It passes control to the next iteration of the control statement where it is located.
return; It stops execution of the method, and returns a value(optional).
goto It transfers control to a specified statement.