Decision Making


You must have seen keywords like “if”, “else”, “switch” and “goto” in many programs.
Here we will be discussing their features, capabilities and application in more details and how and where these statements are used in programming.


Why do we use decision making and control statements?

C programming is a set of statements or set of instructions, which are normally executed from the order in which they appear or written. This happens when no certain option or certain condition is used in programming.
There may be situations where we need to change the order of execution of statements and repeat execution of certain statements depending on conditions. Thats why we use decision making and control statements.
C language support the following decision statements:

  • if statement
  • switch statement
  • Conditional Operator statement
  • goto statement

if statement

If statement is a powerful decision-making statement, it controls the flow of execution of statements. Only If condition of statement evaluates to true, statements inside if block is executed.
Examples:
if(it is raining)
   No Players on ground.
if(you have birthday)
  Friends will take party
if(room is dark)
  Switch on the lights

The if statement can be implemented in different forms depending on the condition.

  • Simple if statement
  • if-else statement.
  • Nested if-else statement.
  • Ladder else if statement.

if statement

The syntax of a simple if statement is:if (test expression)
{
statement block;
}
statement -2;

Note:Statement block may be group of statements or single statement.

if statement in C

If the test expression evaluates to is true, the statement-block is executed; otherwise statement-block is just skipped and execution starts from statement-2. When the condition is true both the statement-block and statement- 2 are executed in sequence.


Example Illustrating if statement:

Output:

Enter the four integer values
4
4
2
2
Ratio is 2.000000


if else statement

The if-else statement is an extension of the simple if statement.
if (test expression)
{
True-block statement;
else
{
False-block statement
}
statement-x;

If the test expression is true, than the if statements (True-block statement) will be executed otherwise the else statements (false-block statement) will be executed. Either true-block or false-block will be executed, not both.


if else statement in C

Examples Illustrating if statement:

Output:

Enter 1 for male and 0 for female
0
No. of male: 20
No. of female: 18



Output:

Enter the value of x & y
3
2
The value of x is greater than y


Nested if else statements

When we have to perform a series of decision, than we use more than one if-else statements in nested form. The syntax of nested if else statement is:
if if(test condition-1)
{
  if( test condition-2)
  {
    statement-1;
  }
  else
  {
    statement-2;
  }
}
else
{
  statement-3;
}
statement-x;

Nested if else statement in C

If test condition-1 is false, the statment-3 will execute.
If test condition-1 is True ,then it continues to check test condition -2
If test condition-2 is true the statement -1 will be executed otherwise statement -2 will be executed.
At last the control is transferred to statement-x.


Example: TechEureka has introduced an incentive policy of giving bonus to all employees. the bonus of 5% is given to every employee but if the employee is female and her salary is more than 10,000 than the bonus is 7% is given to her. The logic can be coded as follow.

if(employ is Female)
{
  if(salary>10000)
  {
    bonus=0.07*salary;
  }
  else
  {
    bonus=0.05*salary;
  }
}
else
{
  bonus=0.05*salary;
}
salary=salary+bonus;



Do You Know?