Decision making using if statement

The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It's a basically two way decision statement and is used in conjuction with an expression.

It's syntax is:- "if(test expression)"

It allows the computer to evaluate the expression first and then, depending on condition, it transfers the control to a particular statement. This point of program has two paths to follow,one for the true and other for false condition.As shown in figure:- 
 
 Practical examples of if statement:-

1. if(bank balance is < 500)
    deposit money

2. if(room is dark)
    put on lights

The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms of if statements are:-

1.Simple if statement
2.if....else statement
3.Nested if....else statement
4. else if ladder

1.Simple if statement.

 The general form of simple if statement is
   
 if (test expression)
{
   statement-block to execute;
 }
statement-x;

Here, statement-block may be a single statement or a group of statements. if the test expression is true, the statement-block will be executed. Otherwise, statement-block will be skipped and execution will jump to statement-x. If condition is true, then also statement-x will be execute after execution of statement-block.


Flowchart of simple if control:-

flowchart of if statement


















Example:-
            ------------------
            ------------------
            if (category == SPORTS)
            {
               marks = marks+bonus_marks;
            }
            printf("%f", marks);
            -------------------
            -------------------
            -------------------
Note:-  if(age>18 && nationality ==INDIAN)
        {
         count = count+1;
        }
it can also be written as:-
        if(age>18)
         {
           if (nationality ==INDIAN)
            {
              count = count+1;
            }
         }
Here the age & nationality of people is checking, if age is greater than 18 yrs. and nationality is Indian than we increase the value of count, it will count the total voter of India.

 2.The if....else statement:- 


This statement is an extension of the simple if statement. The general form of this statement is :- 
 
 if(test expression)
   {
     True-block statement
   }
 else
   {
     False-block statement
   }
 statement-x


If the test expression is true, then if part (the True-block statement) will be executed.Otherwise if the test expression is false, then else part (the False-block statement) will be executed,nor both.In both the cases,the control is transferred to the statement-x
flowchart of if...else statement
Example:-
------------------
------------------
if(code == 1)
 boy = boy +1;
else
 girl = girl+1;
------------------
------------------
Here code 1 is used for boy and 2 is used for girls and this program is used to count the number of boys & girls in a class.






 3.Nested if....else statement:-

 
 If a series of decisions are involved,we may have to use more than one if....else statement in nested form.
 Syntax of nested if....else statement:-
syntax of nested if...else
 Here the logic of exectution is that, if the condition-1 is false, statement -3 will be executed. Otheriseit continues to perform the 2nd test. If the condition-2 is true, the statement-1 will be evaluated, otherwise statement -2 will be evaluated and then control is transferred to the statement -x.

Flowchart of nested if....else statement


Example:-
...............
...............
if(sex == "f")
{
 if(balance > 5000)
    bonus = 0.05 * balance;
 else
    bonus = 0.02 * balance;
}
else
{
  bonus = 0.03 * balance;
}
balance = balance + bonus;
................
................ 
                                
Here if sex is female & bonus is more than 5000, than 5% of extra bonus will be added. If sex is female and balance is less than 5000 than 2% bonus will be added to bonus.And if sex is not f(female) than 3% of bonus will be added to its bonus. And finally bonus is added to balance.

4.The else....if ladder:-


There is another way of putting ifs together when multipath decision is involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if. Its general form is :-

  " if(condition 1)
     statement 1;
  
     else if (condition 2)
       statement 2;
     
       else if(condition 3)
         statement 3;
       
         else if(condition n)
           statement n;
    else
      default statement;

  statement -x; "


This construct is known as the else if ladder. The conditions are evaluated from the top, downwards. As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the statement -x. When all the n conditions becomes false, then the final else containing the default statement will be executed.
flowchart of if....else ladder

    
Example:-

------------------
------------------
if (marks > 79)
  grade = "Honours";
 else if (marks >59)
    grade = "First division";
   else if (marks >49)
      grade = "Second division";
     else if (marks > 39)
        grade = "Third division";
 else
  grade = "Fail";
printf("%s\n", grade);
-------------------
-------------------    

Here the value of grade is deciding on the basis of marks. if marks is greater than 79, it's grade will be honours, if marks is greater than 59, it's grade will be First division, if marks is greater than 49, it's grade will be second division,if marks is greater than 39, it's grade will be Third division, else it's grade will be of Fail.

Comments

  1. Decision making statements lets you evaluate one or more conditions and then take decision whether to execute a set of statements or not. Types of Decision Making statements in C:
    If Statement
    If Else Statement
    Switch Case statement
    If Else Ladder Statement

    ReplyDelete

Post a Comment

Popular posts from this blog

Components of C language

How to make payment for final certificate, migration, provisional for the students of last semester of ptu?