C provides four general categories of control structures: sequential, selection, iteration and encapsulation.
Program/Example
A sequential structure is one in which instructions are executed in sequence. For example,
i = i + 1; j = j + 1;
In the selection structure, the sequence of the instruction is determined by using the result of the condition. The statements that can be used in this category are if and switch. For example:
if (a > b) i = i + 1; else j = j +1;
If the condition is true then the statement i = i +1 is executed; otherwise j = j + 1 is executed.7
The iteration structure is one in which statements are repeatedly executed. The iteration structure forms program loops. The number of iterations generally depends on the values of particular variables.
for (i=0; i<5; i++) { j = j + 1; }
The statement j = j + 1 is executed 5 times and the value of i changes from 0 to 1, 2, 3, and 4.
Encapsulation structure is the structure in which the other component structures are included. For example, you can include an if statement in a for loop or a for loop in an if statement.
Explanation
C provides all the standard control structures that are available in programming languages. These structures are capable of processing any information.
The if statement is the first selection structure. if is used when a question requires a yes or no answer. If you want to choose an answer from several possibilities then use the switch statement.
Program/Example
The general format for an if statement is:
if ( condition ) simple or compound statement.
Following are the properties of an if statement:
Following are the test conditions:
(7)// a non-zero value returns True. (0)// zero value returns False. (i==0) // True if i=0 otherwise False. (i = 0) // False because value of the expression is zero.
The scope of an if clause determines a range over which the result of the condition affects. The scope of an if clause is on the statement which immediately follows the if statement. It can be a simple statement or compound statement.
Case 1:
if (a>b) i = i + 1; // s1 j = j + 1; // s2
Case 2:
if (a>b) { i = i + 1; // s1 j = j + 1; // s2 }
If in Case 1 the if condition is true, then s1 is executed because s1 is a simple statement.
If in Case 2 the if condition is true, then both statements s1 and s2 are executed because s1 and s2 are enclosed in a compound statement.
When you want to take actions based on the outcome of the conditions, (true or false), then you can use the if-else statement.
Program/Example
The general format for an if-else statement is
if (condition) simple or compound statement // s1 else simple or compound statement. // s2
If the condition is true then the s1 part is executed and if the condition is false then the s2 part is executed. For example,
if (a>b) printf (" big number is %d", a); // s1 else printf (" big number is %d", b); // s2
if a is greater than (b) then s1 is executed. Otherwise s2 is executed.
If you want to make many decisions, then you can use the if-else if statement.
Program
The general format for the if-else if statement is:
if (condition 1) simple or compound statement // s1 else if (condition 2) simple or compound statement // s2 else if ( condition 3) simple or compound statement // s3 ..... else if ( conditon n ) simple or compound statement // sn
If condition 1 is true then s1 is executed. If condition 1 is false and condition 2 is true then s2 is executed.
The else clause is always associated with the nearest unresolved if statement.
if (a==5) // A if (a==7) // B i = 10; // C else // D if (a == 7) // E i = 15; // F else // G i = 20; // H
For the else statement at position D, the nearest if statement is specified at B. So, the else statement is associated with if at B and not at A.
For the else statement at G, the nearest if statement is specified at E. So, it is associated with the if statement at E and not at A.
if (a==5) // A if (a==7) // B i = 10; // C else // D if (a == 7) // E i = 15; // F1 j = 20; // F2 else // G i = 20; // H
In this case, the else statement at G cannot be associated with the if statement at E because the if statement at E is already resolved. So, it is associated with the if statement at A.
Points to Remember
You can use a switch statement when you want to check multiple conditions. It can also be done using an if statement but it will be too lengthy and difficult to debug.
Program/Example
The general format for a switch statement is
switch (expressions) { case constant expressions }
Example of a case constant expression and column:
switch (i/10) { case 0: printf ("Number less than 10"); // A break; case 1: printf ("Number less than 20"); // B break; case 2: printf ("Number less than 30"); // C break; default: printf ("Number greater than or equal to 40"); // D break; }
Explanation
Point to Remember
The switch statement is preferable to multiple if statements.
The while loop is used when you want to repeat the execution of a certain statement or a set of statements (compound statement).
Program/Example
The general format for a while loop is
while (condition) simple or compound statement (body of the loop) For example, i = 0; while (i<5) { printf(" the value of i is %d ", i); i = i + 1; }
Explanation
The do-while loop is used when you want to execute the loop body at least once. The do-while loop executes the loop body and then traces the condition.
Program/Example
The general format for a do-while loop is
do simple or compound statement while (condition) For example, i = 0; do { printf(" the value of i is %d ", i); i = i + 1; } while (i<5)
Explanation
The for loop is used only when the number of iterations is predetermined, for example, 10 iterations or 100 iterations.
Program/Example
The general format for the for loop is
for (initializing; continuation condition; update) simple or compound statement For example, for (i = 0; i < 5; i++) { printf("value of i"); }
Explanation
You may want to control the loop variables in the same for loop. You can use one for loop with a comma operator in such situations.
Program/Example
for (i = 0, j = 10; i < 3 && j > 8; i++, j-) printf (" the value of i and j %d %d ",i, j);
Explanation
Just like the switch statement, break is used to break any type of loop. Breaking a loop means terminating it. A break terminates the loop in which the loop body is written.
Program/Example
For example,
i = 0; while (1) { i = i + 1; printf(" the value of i is %d "); if (i>5) break; }
Explanation
The break statement breaks the entire loop, but a continue statement breaks the current iteration. After a continue statement, the control returns to top of the loop, that is, to the test conditions. Switch doesn't have a continue statement.
Program/Example
Suppose you want to print numbers 1 to 10 except 4 and 7. You can write:
for(i = 0, i < 11, i++) { if ((i == 4) || (i == 7)) continue; printf(" the value of i is %d ", i); }
Explanation
Part I - C Language
Part II - Data Structures
Part III - Advanced Problems in Data Structures