Site Tools


dpp:docs_flow

Flow Control

Flow control is essential to almost any progam. It is what can make the program behave differently each time it is run, or make a program repeat a few lines of code without having to retype them. Some common flow control mechanisms that are supported in D++ are“if” statements, Do loops and For loops. Other programming languages may support other flow control mechanisms, such as the “goto” statement… BASIC was very well known for this “spaghetti-style” code. D++ used to support “goto” statements, however they were removed due to code complications.

Conditional Statements

Before using any flow control mechanism, it is essential that you understand conditional statements. A conditional statement is an expression that is evaluated to either be true or false. A simple example could be “3 = 3”. Obviously 3 does equal 3, so this statement would evaluate true. An expression such as “1 = 2” would be evaluated as false. However, these expressions mostly involve one or more variable s - this makes it so that the expression is evaluated differently each time the variable(s) change. Conditional statements are evaluated using D++'s RPN engine, so they can be very powerful. Let's look a few examples:

if x = 3 then
if x != y + 2 then
if x > 7 then
if x <= 0 then
if strAzure = "Great!" then
if x = 3 && y = 4 then     '&& is the boolean "AND"
if x = 3 || y = 4 then     '|| is the boolean "OR"
if myVar then
if ! yourVar then

As you can see, there are unlimited possibilities with conditional statements. You can evaluate simple mathmatical expressions or complex string/math expressions. The last 4 expressions are especially important to note: the boolean AND and OR, and expressions without equality operators. In the AND expression, x must equal 3 and y must equal 4 for it to evaluate true. However in the OR expression, either x has to equal 3 or y has to equal 4 - or both x and y could be equal to 3 and 4, respectivly.

In the last 2 expressions, there are no equality operators (such as = or >= or !=). If D++ can't find these operators, it will assume you mean “= true”. Any statement that does not equal 0 or null is true. If you were to write “screenput 7=7;”, it would print a 1 to the screen. You don't always need an equal sign. The ! symbol is the boolean NOT operator. It will invert the equation. So if in the above example, yourVar was equal to 7, the expression would evaluate to false. However if yourVar was equal to 0 or null, it would evaluate to true.

If Statements

if conditional then
  statements
else[if conditional then]
  statements
endif

If statements are probably the most common type of flow control. They evaluate an expression and, if true, executes one peice of code. If the expression is false, it either exits the statement or evaluates a different peice of code (an “elseif” statement). You can have nested if statements (if statements inside other if statements) and as many elseif's as you want. Or, you can just have one if statement with no else's. No semicolon is required after the “endif” or “then”, but you can do this if you prefer the style.

Do Loops

do [until | while] conditional;
   statements
loop

Loops help to repeat code a number of times without haveing to rewrite the code. Do loops are best utilized when you don't know how many times you will be looping, i.e. if you are waiting for the user to type something specific. (You could use a do loop if you do know, but of course this is just my personal method for programming.) do until loops will loop as long as the conditional is false. do while loops will loop as long as the conditional is true. These loops can be nested. Note that you cannot place a semicolon after “loop”.

For Loops

for var = startval to endval [step stepval]
  statements
next var;

For loops, like do loops, help to repeat code. However, For loops are best used for when you already know how many times you will be looping. In a for loop you have a counting variable. You set the value that the variable should be given at the beginning of the loop, and then the value that the loop should end at when the variable is equal to it. The step is a variable that specifies how much to increase the value of the variable each loop iteration. A step of 10 would count by 10's. The step portion of the code is optional, and if not included, the default is 1. For loops can be nested. You must use a semicolon in the “next” statement.

Note: Brackets [] are not to be used in the code. The [] brackets represent optional portions of code - the code within is not required, however it can be used. Do not actually write these brackets.

Back to D++ Documentation Index

dpp/docs_flow.txt · Last modified: 2015-07-30 13:47 by daniel