Friday, October 15, 2010

Decision Making - Branching PART-I

We have seen that a 'C' program is a set of statements which are normally executed sequentially in order in which they are written or appear. This happens when no options or no repetitions of certain calculations are necessary. However in real practice, we have a number of situations where we may have to change the order of execution of statements based on certain conditions or repeat a group of statements untill certain specified conditions are met. This involves a kind of decision making to see whether a particular condition has occured or not and then direct the computer to execute certain statements accordingly.
'C' language supports the following control or decision making statements:-
  1. if statement
  2. switch statement
  3. conditional operator statement
  4. goto statement
  •  if statement
Simple if statement

The simplest form of the control statement is the If statement. It is very frequently used in decision making and allowing the flow of program execution.

The If structure has the following syntax

if (condition)
statement;

The statement is any valid C’ language statement and the condition is any valid C’ language expression, frequently logical operators are used in the condition statement. The condition part should not end with a semicolon, since the condition and statement should be put together as a single statement. The command says if the condition is true then perform the following statement or If the condition is fake the computer skips the statement and moves on to the next instruction in the program.

Example

#include <stdio.h>

void main()
{
   int number = 0;
   printf("\nEnter an integer between 1 and 10: ");
   scanf("%d",&number);
   if (number > 7)
     printf("You entered %d which is greater than 7\n", number);
   if (number < 3)
     printf("You entered %d which is less than 3\n", number);
}

The if...else statement 

The if...else statement is an extension of simple if statement. The general format is

if (test_expression)
{
   true_block statements(s);
}
else
{
   false_block statements(s);
}
statement x;

If the result of the test_expression is true, then true_block statements(s) is executed, otherwise false_block statements(s) will be executed. If any case either true_block statements(s) is executed or false_block statements(s) is executed but not both, when writing programs this else statement is so frequently required that almost all programming languages provide a special construct to handle this situation.

Example

#include <stdio.h>

void main()
{
   int age = 0;
   printf("\nEnter your age");
   scanf("%d",&number);
   if (number > 18)
     printf("You are eligible for voting");
   else       

    printf("You are not eligible for voting");
}

Nested if...else statements


When a series of decisions are involved, we may have to use more than one if...else statement in nested form as follows:

if(test_condition1)
{
   if(test_condition2)
  {
     statement_1;
  }
  else
  {
      statement_2;
  }
}
else
{
   statement_3;
}
statement_x;

The logic is illustrated as follows:
If the test_condition1 is false, the the statement_3 will be executed, otherwise it continues to perform the second test. If the test_condition2 is true, then statement_1 will be evaluated, otherwise the statement_2 will be evaluated and then the control is transferred to the statement_x;

The else if ladder


There is another way putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else  is and if. It takes the following general form.

if (condition1)
   statement1;
else if (condition2)
   statement2;
else if (condition3)
   statement3;
else if (condition)
   statement n;
else
   default statement;
statement-x; 

This construct is known as if else construct or ladder. The conditions are evaluated from the top of the ladder to downwards. As soon on the true condition is found, the statement associated with it is executed and the control is transferred to the statement – x (skipping the rest of the ladder. When all the condition becomes false, the final else containing the default statement will be executed. 

Example of grading the students in an academic institution.
Marks
Grade
80 to 100
60 to 79
50 to 59
40 to 49
0 to 39

HONOURS
IST DIVISON
2ND DIVISION
3RD DIVISON
FAIL

This grading can be done using else if  ladder as follows:

if(marks>79)
grade="HONOURS";
else if(marks>50)
   grade="1ST DIVISON";
   else if(marks>49)
      grade="2ND DIVISON";
      else if(marks>39)
         grade="3RD DIVISION";
         else
            grade="FAIL";
printf("%s"\n",grade);

0 comments:

Post a Comment