Skip to main content

C-Decision Making


What is decision?
Decision is certain actions which we take based on certain conditions. If it is raining outside we would not go to play. Similarly in C there are certain conditions known as decision making statements based on which it will perform some actions.

Why do we need decision making statement?
Many a time we want  a set of instructions to be executed under one condition and an entirely different set of conditions under another conditions and this is why we need decision making statements in C.

In C decision control statements can be done using:
  1. If statement
  2. If-else statement
  3. By using Logical operators
If Statement:
Syntax: if(this condition is true)
              execute this lines;
The if keyword tells the compiler that if the conditions inside the brackets turns out to be true then the code under, will be executed. One may ask what are the conditions that has to be written inside the brackets and how those conditions has to be written.
Let us consider two variables x and y. Below we will write possible instructions and explain those syntax.

  • x==y
    This means x is equal to y
  • x!=y
    This means x is not equal to y
  • x<y
    x less than y
  • x>y
    x greater than y
  • x<=y
    x is less than or equal to y
  • x>=y
    x is greater than or equal to y
Example 1:

Output:


Explanation:The program prints a number if the number provided by user is less than 50. We have used a if condition inside the if keyword parenthesis. The condition is given inside the brackets, after which the code is written  is to be executed if the condition is satisfied. If there is one line after if statement then we do not need to put braces after if statement.The braces are for multiple statements after if.

Multiple statements within if:

Example 2:

Output:


If-else:
Suppose we want to perform an action if a condition is correct and also want to execute a group of code if the condition does not satisfies. This is why we need if else.

Example 3:


Output:


Explanation:In this code,user is asked to provide a number and simultaneously we will get to know whether the number is less than or greater than 50. The code under if statement is executed if the given number is less than 50 and if the number is greater than 50 the else part will execute.

Various forms of if-else:
  1. if(condition)
    execute code;
  2. if(condition)
    {
    execute code;
    execute code;
    }
  3. if(condition)
    execute code;
    else
    execute this code;
  4. if(condition)
    {
    execute code;
    execute code;
    }
    else
    {
    execute code;
    execute code;
    }
  5. if(condition)
    execute code;
    else
    {
    if(condition)
    execute code;
    else
    {
    execute code;
    execute code;
    }
    }
Logical Operators:
Basically C uses three logical operators and they are:
  1. &&(And)
  2. ||(Or)
The uses of these operators is shown below:

Example 4:
Output:

Explanation:The statement if((percentage>=50)&&(percentage<60)) depicts that the expression in this condition will only execute if both the conditions is satisfied i.e the percentage has to be greater than 50 and less than 60.If the percentage is greater than 50 but not less than 60(suppose percentage is 70) then this condition will not work.Again if the percentage is less than 60 but less than 50(suppose 40) then also this condition will not work.


**Another disadvantage of using multiple 'if 'statement as above is that if the condition is satisfied in the first 'if 'statement then also the other 'if' conditions will be checked which will increase the execution time and here comes else-if clause.**

Else-if:
It is nothing but with different convention to write if  and else statements.Previously we found that using multiple statements increases the execution time.So that is why we will use else-if so that all the if statement is not executed.



The left hand side and right hand side is similar .In left we are writing if and else separately and in right hand we are writing it together.A program below will simplify it.

Example 5:
Output:


Explanation:The only difference between this code and the last one is that in the last one all the if conditions are checked and in this one if the correct condition is found,other conditions are not checked,thus saving execution time.

!(not) operator:
This operator reverses the result of the expression it operates on.Consider the following expression:

!(x<80).
This means not 'x less than 80'.If value of x is less than 80 the expression will be false.

Ternary Operators:
This operator is a combination of ? and : . Its general form is:
expression1?expression2 : expression3

This expression depicts that if expression1 is true then expression2 will be evaluated else expression3 will be evaluated.Examples are given below:

1)x = y > 5? 2 : 6;
This explains that if y is greater than 5 then value of x will be 2 else value of x will be 6;
It is similar to:
if(y>5)
x=2;
else 
x=6;

2)d=(a > b ? (a > c ? 3 : 4) : (b > c ? 6 :9 ));
This explains that if a>b then  (a > c ? 3 : 4) will be evaluated, else  (b > c ? 6 :9 )) will be evaluated.Suppose we consider that the condition a>b is true then (a > c ? 3 : 4) is evaluated and if a>c is true then 3 will be the value of d else 4 is assigned to d.

3)a >= 5 ? b = 100 : b = 200;
This expression will give an error as Lvalue required.This is solved with the help of an extra braces like below:
a >= 5 ? b = 100 :(b = 200);
Error is given due to less precedence of = operator and that is why we need to put extra bracket else compiler reads it as  (a >= 5 ? b = 100 :b) = 200;


**One disadvantage of ternary operator is that after '?'or after ':' only one C statement can occur.**

*End of chapter 2*

Comments