Control Statements
In most of the C programs we have encountered so far, the instructions that were executed in the same order in which they appeared within the program (sequentially).
Each instruction was executed one and only once. Program of this type are very simple, since they do not include any logical control structures.
While programming, we often encounter a situation in which decisions have to be made or repetitions are required.
In realistic C programs, the program needs logical condition to determine if certain conditions are true or false, they do require the repeated execution of groups of statements and, they involve the execution of individual groups of statements on a selective basis.
And all these operations can be carried out using the various control statements included in C.
In general, those statements which alters the flow of execution of program are known as control statements. C Language has following decisions making statements:
Decision Making Statements
In realistic C program, it may require a logical test to be carried out at some particular point within the program.
And depending on the outcome of the logical test, several possible actions will be carried out, this is known as branching or conditional branching statements. Different decision making statements are:
- if Statement with Examples
- if-else Statement with Examples
- if-else-if Statement or Ladder with Examples
- Nested if-else Statement with Examples
- switch Statement with Examples
- Conditional Opeartor Statement with Examples
Looping Statements
In C, the program may require that a group of statements be executed repeatedly, until some logical condition has been satisfied this is known as looping or iteration. Sometimes the required number of repetition is known in advance; and sometimes statements are executed until the logical condition becomes true.
A loop generally consists of two section, namely, body of the loop and test condition. Test condition performs a logical test whose result is either TRUE or FALSE. When the result of test condition is TRUE, statements in the body of the loop are executed, otherwise, the loop is terminated.
Depending on the position of the control statement in the loop, the loop statement is classified in two categories, namely, pre-tested loop (also known as entry-controlled loop) and post-tested loop (also known as exit-controlled loop). In pre-tested loop, test condition is evaluated before the execution of loop. If the condition is TRUE then loop is executed, otherwise loop is terminated. In post-tested loop, body of loop is executed unconditionally for the first time because condition is tested after the execution of body of loop. If the condition is TRUE, then loop is executed again otherwise loop is terminated.
Every loop consists three essential elements, they are:
- Initialization: It sets initial value for the loop control variable before the statements within the loop starts to execute.
- Condition: It tests the condition to determine whether to execute the loop or not.
- Update: It increases or decreases the loop control variable such that after some repetitions condition will result FALSE and loop will be terminated.
Different types of looping or iterative statements in C programming language are :