Decision making in c

Decision making in c is also known as decision control instruction.

Decision control instruction requires the programmers to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

The general form of a decision-making structure found in most of the programming languages −

decision making in c

In C programming language, any non-zero and non-null values are assumed as true. In contrast, zero or null values are assumed as false.

In C programming following are types of decision making in c :

  • if 
  • if-else
  • conditional operator
  • nested if ( other than above 3)

if statement

if a keyword in c programming which let compiler to identify decision control instruction, immediately after if some condition is there. This condition is any valid expression in C. If the result of an expression is non-zero it is considered as true otherwise false.

Syntax

 The syntax of a typical if block will look something like this 

main()
{
 if (some condition)
{
//control enters only when the condition inside if block is true //
statement 1;
statement 2;
}
}

Example:

#include<stdio.h>
int main()
{
int x,y;
printf("Enter two numbers");
scanf("%d %d" , &x , &y);
if (x>y)
     {
         printf("x is greater than y (if block executed successfully)");
      }
}

OUTPUT

  • If you will enter the value of x greater than the value of y then the condition inside if block (x>y) will be true and if block will execute. Hence, it will print: x is greater than y (if block executed successfully)
  • And if you will enter the value of x smaller than the value of y then the condition inside if block (x>y) will be false hence, it will print nothing.

Have You enjoyed the article on decision making in c!

if-else statement

if-else statement is used for decision making in c language. This is similar to if but the else block is a new add-on. When the condition of if statement is true then if block will be executed and when the condition of if block is false then else block will be executed.

It is important to mention that only one from two blocks (if block and else block) can be executed as the condition may have only two results., TRUE or FALSE.

Syntax

The syntax of a typical if-else block will look something like this

main()
{
     if (some condition)
      {
        // control enters when condition inside if block is true //
        statement 1;
        statement 2;
        }
      else
       {
        //control enters only when condition inside if block is false //
        statement 1;
        statement 2;
        }
}

Example

#include<stdio.h>
int main()
{
int x,y;
printf("Enter two numbers");
scanf("%d %d" , &x , &y);
     if (x>y)
        {
           printf("x is greater than y (if block executed successfully)");
        }
     else
        {
            printf("x is less than or equal to y (else block executed)");
         }
}

OUTPUT

  • If you will enter the value of x greater than the value of y then the condition inside if block (x>y) will be true and hence, if block will execute. And, it will print: x is greater than y (if block executed successfully)
  • And if you will enter the value of x smaller than the value of y then the condition inside if block (x>y) will be false hence, else block will execute. And, it will print: x is less than or equal to y (else block executed)

Conditional Operator

The conditional operator is an amazing and important statement used for decision making in c language. It is also known as a ternary operator which means operator needs three operands to perform its operation. In addition, the conditional operator is represented as ( ?  :). 

Expression1 is a condition, which is first evaluated as TRUE or FALSE. If the condition is TRUE then Expression2 will be executed otherwise Expression3.

The conditional operator works similar to if-else, but you don’t have to use the keyword if and else.

Syntax

 Expression1? Expression2: Expression3

Example:

#include<stdio.h>
int main()
{
int x,y;
printf("Enter two numbers");
scanf("%d %d" , &x , &y);
x>y ? printf("True statement is executed") : printf("False is executed");
}

OUTPUT

  • If you will enter the value of x smaller than the value of y then the condition (x>y) will be false hence, it will print: False is executed.
  • And, if you will enter the value of x greater than the value of y then the condition (x>y) will be true hence, it will print: True statement is executed.

nested if or if-else statement

When if or if-else statement is used inside other if or if-else statement then it is called as nested if or if-else statement. Because one statement is nested inside another statement.

Syntax

#include<stdio.h>
int main()
{
if ( some condition)
     {
        if( some condition) 
         {
          statement 1;
          statement 2;
           }
        else 
         {
           statement 1;
            statement 2;
           }
         }
else
     {
       if( some condition) 
       {
          statement 1;
           statement 2;
           }
       else
         {
           statement 1;
           statement 2;
           }
      }
}

Example:

#include<stdio.h>
int main()
{
int x,y;
printf("Enter two numbers");
scanf("%d %d" , &x , &y);
if (x>y)
  {
  if( x==y) // Nested if-if block //
  printf("x is equal to y. Because nested if block executed");
    else  // Nested if- else block as else block is inside if block //
   printf("x is greater than y. Because nested if-else block executed");
 }
else
 {
  if( x==y)  // Nested else-if block as if block is inside else block//
   printf("x is equal to y. Because nested else if block executed");  
else // Nested else - else block as else block is inside another else //
printf("x is less than y. Because nested else-else block executed"); 
  }
}

Have You enjoyed the article on decision making in c!

OUTPUT

  • If you will enter the value of x greater than the value of y then the condition inside if block (x>y) will be true hence, if block will execute.
  • Now, the second condition will be checked by the compiler to validate whether x is equal to y (x == y ).
  • And if x is equal to y then, nested if block will execute and it will print: x is equal to y. Because nested if block executed.
  • Otherwise, nested else block will execute and it will print: x is greater than y. Because nested if-else block executed.
  • If you will enter the value of x less than the value of y then the condition inside if block (x>y) will be false hence, else block will execute.
  • Now, the second condition will be checked by the compiler to validate whether x is equal to y (x == y ).
  • And if x is equal to y then, nested else if block will execute and it will print: x is equal to y. Because nested else if block executed.
  • Otherwise, nested else block will execute and it will print: x is less than y. Because nested else-else block executed.


<< Prev                                                                                                              Next >>