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.

THE switch STATEMENT

Introduction

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

  1. The switch expression should be an integer expression and, when evaluated, it must have an integer value.
  2. The case constant expression must represent a particular integer value and no two case expressions should have the same value.
  3. The value of the switch expression is compared with the case constant expression in the order specified, that is, from the top down.
  4. The execution begins from the case where the switch expression is matched and it flows downward.
  5. In the absence of a break statement, all statements that are followed by matched cases are executed. So, if you don't include a break statement and the number is 5, then all the statements A, B, C, and D are executed.
  6. If there is no matched case then the default is executed. You can have either zero or one default statement.
  7. In the case of a nested switch statement, the break statements break the inner switch statement.

Point to Remember

The switch statement is preferable to multiple if statements.

THE while LOOP

Introduction

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

  1. Before entering into the loop, the while condition is evaluated. If it is true then only the loop body is executed.
  2. Before making an iteration, the while condition is checked. If it is true then the loop body is executed.
  3. It is the responsibility of the programmer to ensure that the condition is false after certain iterations; otherwise, the loop will make infinite iterations and it will not terminate.
  4. The programmer should be aware of the final value of the looping variable. For example, in this case, the final value of the looping variable is 5.
  5. While writing the loop body, you have to be careful to decide whether the loop variable is updated at the start of the body or at the end of the body.

THE do while LOOP

Introduction

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

  1. The loop body is executed at least once.
  2. The condition is checked after executing the loop body once.
  3. If the condition is false then the loop is terminated.
  4. In this example, the last value of i is printed as 5.

THE for LOOP

Introduction

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

  1. The for loop has four components; three are given in parentheses and one in the loop body.
  2. All three components between the parentheses are optional.
  3. The initialization part is executed first and only once.
  4. The condition is evaluated before the loop body is executed. If the condition is false then the loop body is not executed.
  5. The update part is executed only after the loop body is executed and is generally used for updating the loop variables.
  6. The absence of a condition is taken as true.
  7. It is the responsibility of the programmer to make sure the condition is false after certain iterations.

THE for LOOP WITH A COMMA OPERATOR

Introduction

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

  1. First i is initialized to 0, and j is initialized to 10.
  2. The conditions i<3 and j>8 are evaluated and the result is printed only if both conditions are true.
  3. After executing the loop body, i is incremented by 1 and j is decremented by 1.
  4. The comma operator also returns a value. It returns the value of the rightmost operand. The value of (i = 0, j = 10) is 10.

THE break STATEMENT

Introduction

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

  1. The while (1) here means the while condition is always true.
  2. When i reaches 6, the if condition becomes true and break is executed, which terminates the loop.

THE continue STATEMENT

Introduction

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

  1. If i is 1 then the if condition is not satisfied and continue is not executed. The value of i is printed as 1.
  2. When i is 4 then the if condition is satisfied and continue is executed.
  3. After executing the continue statement, the next statement, (printf), is not executed; instead, the updated part of the for statement (i++) is executed.


C & Data Structures
C & Data Structures (Charles River Media Computer Engineering)
ISBN: 1584503386
EAN: 2147483647
Year: 2006
Pages: 232

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net