Any algorithm or program can be more clear and understood if they use self-contained modules called as logic or control structures. It basically analyzes and chooses in which direction a program flows based on certain parameters or conditions. There are three basic types of logic, or flow of control, known as: Sequence logic, or sequential flow Selection logic, or conditional flow Iteration logic, or repetitive flow Let us see them in detail: Sequential Logic Sequential Flow Sequential logic as the name suggests follows a serial or sequential flow in which the flow depends on the series of instructions given to the computer.
Unless new instructions are given, the modules are executed in the obvious sequence. The sequences may be given, by means of numbered steps explicitly.
Also, implicitly follows the order in which modules are written. Most of the processing, even some complex problems, will generally follow this elementary flow pattern.
Sequential Control flow Selection Logic Conditional Flow Selection Logic simply involves a number of conditions or parameters which decides one out of several written modules. The structures which use these type of logic are known as Conditional Structures. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. If the variable is an integer 2, it will be true, because any number that is not zero will be true.
If the condition is true, then an action occurs. If the condition is false, nothing is done. To illustrate:. If the variable indeed holds a value consistent with being true, then the course of action is taken. If the variable is not true, then there is no course of action taken. If the condition is false, take an alternate action. In this case, if the variable is true, it takes a certain course of action and completely skips the ELSE clause.
If the variable is false, the control structure calls a routine and completely skips the THEN clause. This structure is useful when performing iterative instructions to satisfy a certain parameter. This routine will add 1 to X until X is equal to 9, at which point the control structure will quit and move on to the next instruction. Note that when the structure quits, it will not execute the Add function: when X is equal to 9, it will skip over the clause that is attached to the WHILE.
This instruction is useful if a parameter needs to be tested repeatedly before acceptance. A WHILE loop initially checks to see if the parameters have been satisfied before executing an instruction.
A FOR loop is an extension of a while loop. A for loop usually has three commands. The second is the end condition same as in a while loop and is run every round. The third is also run every round and is usually used to modify a value used in the condition block.
This loop would be run 10 times x being 0 to 9 so you won't have to think about the X variable in the loop you can just put code there. Here is a while loop that does the same:. Some programming languages also have a for each loop which will be useful when working with arrays and collections. A for each loop is an even more automated while loop that cycles through array's and such.
Example :. Flow Control Overview [ edit edit source ] A control structure is like a block of programming that analyses variables and chooses a direction in which to go based on given parameters. Basic Terminologies [ edit edit source ] Those initial conditions and parameters are called preconditions. An Example [ edit edit source ] Let us analyse flow control by using traffic flow as a model.
Precondition: The vehicle is in motion.
0コメント