Turbo C: Control Structures


 Definition:

           One of the instructions, statements or groups of statements in a programming language which determines the sequence of execution of other instructions or statements (the control flow).


Types of Control Structures 

1. Conditional structure

   -->if and else
A high-level programming language statement that compares two or more sets of data and tests the results. If the results are true, the THEN instructions are taken; if not, the ELSE instructions are taken.

Format:
if (condition) statement


Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.
 
2. Iteration structures (loops)

-->The while loop
 is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.



Format:

while (expression) statement

and its functionality is simply to repeat statement while the condition set in expression is true.
-->The do-while loop 
A high-level programming language structure that repeats instructions based on the results of a comparison. In a DO WHILE loop, the instructions within the loop are performed if the comparison is true.
Format:

do statement while (condition);

Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled.
 
--> The for loop
A loop construct found in many procedural languages which repeatedly executes some instructions while a condition is true. 
Format:
for (initialization; condition; increase) statement;
and its main function is to repeat statement while condition remains true, like the while loop.
 But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement.
So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration.


3. Jump statements
-->The break statement
 
Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end.
Example:
10  9  8  7  6  5  4  aborted…..

-->The continue statement
The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.


Example:

10  9  8  7  6>  <4  3  2  1  FIRE!
 
--> goto statement

goto allows to make an absolute jump to another point in the program. You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations.

The destination point is identified by a label, which is then used as an argument for the goto statement.

A label is made of a valid identifier followed by a colon (:).


exit function
exit is a function defined in the cstdlib library.
The purpose of exit is to terminate the current program with a specific exit code.

Its prototype is:
void exit (int exitcode);
The exitcode is used by some operating systems and may be used by calling programs. By convention, an exit code of zero means that the program finished normally and any other value means that some error or unexpected results happened.

4. The selective structure
--> Switch Statements
The syntax of the switch statement is a bit peculiar. Its objective is to check several possible constant values for an expression.


Something similar to what we did at the beginning of this section with the concatenation of several if and else if instructions.
  
Format:
switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes group of statements 1 until it finds the break statement.


When it finds this break statement the program jumps to the end of the switch selective structure. If expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure.


 Finally, if the value of expression did not match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default: label, if it exists (since it is optional).
 

1 comment:

  1. Really Good blog post.provided a helpful information.I hope that you will post more updates like this Data Science online Training Hyderabad

    ReplyDelete