| Wikipedia has related information atControl flow. |
Conditional clauses are blocks of code that will only execute if a particular expression (the condition) istrue.
Theif-else statement is the simplest of the conditional statements. They are also called branches, as when the program arrives at anif statement during its execution, control will "branch" off into one of two or more "directions". Anif-else statement is generally in the following form:
if (condition): statementelse: other statement
If the original condition is met, then all the code within the first statement is executed. The optional else section specifies an alternative statement that will be executed if the condition is false. Exact syntax will vary between programming languages, but the majority of programming languages (especiallyprocedural andstructured languages) will have some form of if-else conditional statement built-in. The if-else statement can usually be extended to the following form:
if (condition): statementelse if (condition): other statementelse if (condition): other statement...else (condition): another statement
Only one statement in the entire block will be executed. This statement will be the first one with a condition which evaluates to be true. The concept of an if-else-if structure is easier to understand with the aid of an example:
if (temperature is equal to or greater than 40 degrees celsius): print "It's extremely hot"else if (temperature is between 30 and 39 degrees): print "It's hot"else if (temperature is between 20 and 29 degrees): print "It's warm"else if (temperature is between 10 and 19 degrees): print "It's cool"else if (temperature is between 0 and 9 degrees): print "It's cold"else: print "It's freezing"
When this program executes, the computer will check all conditions in order until one of them matches its concept of truth. As soon as this occurs, the program will execute the statement immediately following the condition and continue, without checking any other condition . For this reason, when you are trying tooptimize a program, it is a good idea to sort your if-else conditions in descendingprobability. This will ensure that in the most common scenarios, the computer has to do less work, as it will most likely only have to check one or two "branches" before it finds the statement which it should execute. However, when writing programs for the first time, try not to think about this too much lest you find yourself undertakingpremature optimization.
Having said all that, you should be aware that anoptimizing compiler might rearrange yourif statement at will when the statement in question is free fromside effects. Among other techniques optimizing compilers might even applyjump tables andbinary searches.
Often it is necessary to compare one specific variable against several constant expressions. For this kind of conditional expression thecase statement exists. For example:
case X :when 1 : Walk_The_Dog;when 5 : Launch_Nuke;when 8or 10 : Sell_All_Stock;whenothers : Self_Destruct;esac;
Unconditionals let you change the flow of your program without a condition. You should be careful when using unconditionals. Often they make programs difficult to understand. ReadIsn't goto evil? for more information.
End a function and return to the calling procedure or function.
For procedures:
return
For functions:
return Value
Goto transfers control to the statement after the label.
gotoLabelDont_Do_Something;Label :
Since Professor Dikstra wrote his articleGo To Statement Considered Harmful,goto is considered bad practice in structured programming languages, and, in fact, many programming style guides forbid the use of thegoto statement. But it is often overlooked that any return which is not the last statement inside a procedure or function is also an unconditional statement — agoto in disguise. There is an important difference though: a return is a forward only use ofgoto. Exceptions are also a type ofgoto statement; and note that they need not specify where they are going to.
Therefore, if you have functions and procedures with more than onereturn statement you are breaking the structure of the program similarly to a use ofgoto. In practice, nearly every programmer is familiar with a 'return' statement and its associated behavior; thus, when it comes down to readability the following two samples are almost the same:
procedure Use_Return Do_Somethingif Test :return Do_Something_Elsereturnend
procedure Use_Goto Do_Somethingif Test :gotoExit Do_Something_ElseExit :returnend
Also, note that thegoto statement in Ada is considered safer than in other languages, since it doesn't allow you to transfer control in certain conditions where the control structure is susceptible to be broken.
Because the use of agoto requires the declaration of a label, it can be considered as readable as the use ofreturn. So if readability is a concern instead of a strict programming rule such as "don't usegoto", then it is possible to consider the use ofgoto than multiplereturn statements. Although, it is best to use the structured approach whenever possible, where neithergoto nor multiplereturns are needed, for instance:
procedure Use_If Do_Something;ifnot Test : Do_Something_Else;fi;returnend
Loops allow you to have a set of statements repeated over and over again.
The endless loop is a loop which never ends and the statements inside are repeated forever. The term,endless loop, is a relative term; if the running program is forcibly terminated by some means beyond the control of the program, then an endless loop will indeed end.
loop : Do_Something;repeat
This loop has a condition at the beginning. The statements are repeated as long as the condition is met. If the condition is not met at the very beginning then the statements inside the loop are never executed.
while (X < 5) :let X := Calculate_Somethingrepeat
This loop has a condition at the end and the statements are repeated until the condition is met. Since the check is at the end the statements are at least executed once.
loop X := Calculate_Somethinguntil (X > 5)
Sometimes you need to first make a calculation and exit the loop when a certain criterion is met. However when the criterion is not met there is something else to be done. Hence you need a loop where the exit condition is in the middle.
loop X := Calculate_Somethingif X > 5 :exit Do_Something (X)repeat
Quite often one needs a loop where a specific variable is counted from a given start value up or down to a specific end value. You could use thewhile loop here — but since this is a very common loop there is an easier syntax available.
for I := 1to 10 : Do_Something (I)repeat
Another very common situation is the need for a loop which iterates over every element of an array. The following sample code shows you how to achieve this:
for-each Iin X : X [I] := Get_Next_Element;repeat