Pages

CONTROL STRUCTURES UNIT 4 PART 1


CONTROL STRUCTURES
Introduction
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
Introduction
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:
  1. If the condition is true then the simple or compound statements are executed.
  2. If the condition is false it does not do anything.
  3. The condition is given in parentheses and must be evaluated as true (nonzero value) or false (zero value).
  4. If a compound statement is provided, it must be enclosed in opening and closing braces.
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.


SCOPE OF AN if CLAUSE
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
THE if-else STATEMENT
Introduction
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.
THE if-else if STATEMENT
Introduction
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
  1. You can use if-else if when you want to check several conditions but still execute one statement.
  2. When writing an if-else if statement, be careful to associate your else statement to the appropriate if statement.
  3. You must have parentheses around the condition.
  4. You must have a semicolon or right brace before the else statement.