Use conditional and iterative statements

Completed

Conditional statements are used to determine if a block of code should be run. Different conditional statements have different advantages and uses depending on the situation. The four conditional statements are: if, if...else, switch, and ternary operators.

If statement

The if statement evaluates whether an expression is true. If the expression is true, it runs the set of statements that are listed in the braces. In the following if statement, the statement checks the expression in parentheses. In this case, the statement checks if the variable x is equal to 5. If the expression is true, then the code in the braces runs. If the expression is false, the code in the braces is skipped. The if statement takes exactly one statement to run. The braces can contain a list of statements that are enclosed into one statement, called a compound statement.

if (x == 5)
{
	info('x equals five');
}

If...else statement

You can set statements to run when the expression is false by using the if...else statement. You can create a normal if statement, but at the end of the if statement, you use the keyword else followed by brackets, as shown in the following example. You can add more code in the brackets that runs when the expression in the if statement is false. In the following example, the statement checks if x is equal to 5. If it is, it runs the info x statement. If it is not equal to 5, it runs the info "X does not equal 5" statement.

if (x == 5)
{
	info('x equals five');
}
else
{
	info('x does not equal five');
}

You can use operators in the condition’s relational operators to determine if the expression is true or false. You can use the operators && and || in your expressions to determine if two or more expressions must be true, or only one expression out of many must be true. Additionally, you can nest if statements to check for multiple expressions, but if you are nesting multiple if statements, you might want to consider a switch statement.

Switch statement

A switch statement is a multibranch conditional statement that is checked against multiple cases. If one of the case values are equal to the switch expression, then the case statement is run. The case statements use a break statement to tell the code to stop running the switch statement, which will prevent the next statement from being run. You can also set a default statement if no case expressions match. The following example shows a switch statement where the value for variable x is checked against different cases. If x is 5, then it runs the code under that case until it comes to a break statement. If x is 10, it runs the code under that case. If x is not 5 or 10, it runs the code under the default statement.

switch (x)
{
	case 5:
		//do something
		break;
	case 10:
		//do something
		break;
	default:
		//do something
		break;
}

Using iterative statements

Iterative statements, or loops, repeat a statement block until a condition has been satisfied. These types of iterative statements are used in X++.

Loops are repetitive constructs. X++ has three kinds of loops:

  • while
  • do...while
  • for

Loops can be combined with the following:

  • break statements
  • continue statements

While loops

A while loop enables you to repeatedly run one statement or a compound statement if a condition is true. The statement is run from zero (not at all) to many times, depending on how many times the condition evaluates to true. This contrasts with a do...while loop, where the statements are always run at least once before the condition is evaluated.

int loopCount = 1;
container cont;

while (loopCount <= conlen(cont))
{
	print conpeek(cont,loopCount);
	loopCount = loopCount + 1;
}

Do...while loops

The do...while loop is like the while loop but differs in that the condition follows the statements. The statements are always performed at least once. The do...while loop is well suited for tasks that always must be done at least once, for example, to get parameters for a report.

int findPower(real _value)
	{
		int ex=-1;
		real curVal;

		do
		{
			ex += 1;
			curVal = power(10, ex);
		}

		while (_value > curVal);

		return ex;
	}

For loops

The for loop is similar to the while loop, but has the following additions:

  • The initial value to a control variable can be assigned.
  • It contains a statement for updating the variable.

These additions make it especially useful for traversing lists, containers, and arrays because they have a fixed number of elements. You can also apply a statement to each element and increment your way through the elements, setting the condition to test for the last element.

for (int i=1; i<=100; i+=1)
{
	print ra[i];
}

Break statements

X++ provides a break statement for the following:

  • Terminating loops
  • Separation of case statements in a switch statement

When used within a whiledo...while, or for loop, the loop is terminated and implementation continues from the statement that follows the loop, as shown in the following example.

mainMenu = SysDictMenu::newMainMenu();
enum = mainMenu.getEnumerator();
found = false;

while (enum.moveNext())
{
	menuItem = enum.current();
	if (menuItem.label() == parentDictsecuritykey.label())
	{
		found = true;
		break;
	}
}
if (found) //Belongs in Navigation Pane.
{
	...
}

When break is used within a Switch Statement statement, the running of the case branch terminates and the statement that follows the switch is run as shown in the following example.

switch (Debtor.AccountNo)
{
	case "1000":  do_something;
			break;
	case "2000": do_something_else;
			break;
	default: default_statement; 
			break;
}

If the Debtor account number is 1000, the program runs do_something and then continues running after the switch statement. If you have a break within a loop, you need to break out of the loop. If you have a continue within a loop, it ends the current iteration and reevaluates the loop condition.

Watch the following video to learn about conditional statements: