We all need to change or alter our actions in the face of changing circumstances. Suppose, if weather is fine, then I will go to canteen. If the road is busy, I would prefer a diversion and many other examples are there.

You can notice here that all decisions depend on some favorable conditions.

C language also able to perform such operations such as decision making that is different set of actions.

Basically, C has three major decision making instructions:

  1. If-else statement
  2. If statement
  3. Switch statement

In the previous tutorials we have gone through from various statements which are executed sequentially, in which order they were written. Now, we are going to deal with some cases like we want to execute one statement in one situation or under one condition and other statements under other conditions.

For achieving this goal we have three instructions available in C language as mentioned above.

Let us learn about these instructions turn by turn.

If statement:

In C, if keyword is used to implement the decision control instructions.

Example:

if(Condition is true)

Statement execution starts;

else

Other statements execution start;

The keyword if tells the compiler that what needs to follow is a decision control instruction. The condition following the keyword if always enclosed within a pair of parentheses ( () ).

The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater one is greater than the other.

decision making in c programming language for beginners

The relational operators should be familiar to you expect for the equality operator == and the inequality operator !=. As we have already studied in the previous tutorials. Note: Equal to (=) is used for assignment, means the value is assigned to a variable, whereas, == is mainly used for the comparison of two variables. This could be understood by a simple program that demonstrates the use of if and the relational operators.

/* Demonstrate of if statement*/

# include<stdio.h>

int main()

{

int num;

printf(“Enter a number less than 10”);

scanf(“%d”,&num);

if(num<10)

printf(“Good”);

return 0;

}

If-else Statement:

The if statement by itself will only execute a single statement, or a group of statements, when the expression execute when if evaluates the condition to true. It does nothing when the expression evaluates false.

Syntax of if-else decision making statement:

if(condition)        //the statement1 will only execute when the condition //is true

{

//statement1 or code

}

else                      //when condition is false then this statement2 will be //executed

{

//statement2 or code

}

Example:

#include<stdio.h>

int main()

{

int n;

if(n>10)

{

printf(“Number is greater than 10”);

}

else

{

printf(“Number is less than 10”);

}

return 0;

}

Here, User wants to check the number whether it is greater than 10 or less than 10. It can be checked by the if-else statement. Two conditions can be checked by using this decision making statement.

decision making in c programming language for beginnersNested if-else statement:

The if-else statement is used when we have only two statements to be executed but what if we have more than two conditions to be checked, in that case nested if-else statement comes under the light.

Nested if-else allows the programmer to check multiple condition at the same time.

 

Syntax of nested if-else:

if(expression1)

{

//statement will execute when only expression1 is true.

}

else

if(expression2)

{

//Statement will only execute when expression2 is true.

}

else

{

//statement will be executed when both the expressions are false.

}

decision making in c programming language for beginnersExample:

Write a program to find the greater of three numbers.

#include<stdio.h>

int main()

{

int a,b,c;

printf(“Enter all three numbers”);

scanf(“%d %d %d”,&a,&b,&c);

if(a>b && a>c)

printf(“%d is greater”,a);

else

if(b>a && b>c)

printf(“%d is greater”,b);

else

printf(“%d is greater”,c);

}

Switch statement:

Switch statement in C allows the variable to be tested for equality against a list of values given by the programmer.

Each value is called on the call of the given case value, the variable being switched on its check.

Syntax of Switch statement:

decision making in c programming language for beginnersExample:

#include<stdio.h>

int main()

{

int choice;

printf(“Enter your choice\n”);

scanf(“%d”,&choice);

switch(choice)

{

case 1: printf(“%d is pressed”,a);

break;

case 2: printf(“%d is pressed”,b);

break;

default: printf(“bye”);

break;

}

return 0;

}

Output:

Enter your choice  1

1 is pressed

Rules:

  • The expression used in switch statement should be an integral value or an enumerated type.
  • We can have number of cases initialized with a colon (:).
  • The constant expression is needed to be followed in the case and the switch as well.
  • The case statement needs to execute at once if a single case contain a break, otherwise it will execute in the general flow of the execution until the break statement is reached.

Break statement terminates the switch statement and stops the flow of execution.