Skip to content

1. Ternary Operator in C

Definition: The ternary operator is a conditional operator in C. It can be used as an alternative to a simple if...else statement, especially when a value has to be assigned to the same variable based on a condition.

The ternary operator is called a ternary operator because it works with three operands.

condition ? expression1 : expression2;

Important: C has only one ternary operator: ?:

Example: Ternary Operator

Consider the following if...else statement:

if(a > 10)
{
    b = 2;
}
else
{
    b = 4;
}

The same logic can be written using the ternary operator:

b = a > 10 ? 2 : 4;

Complete Program

#include <stdio.h>

void main()
{
    int a, b;

    a = 12;

    b = a > 10 ? 2 : 4;

    printf("a=%d", a);
    printf("\nb=%d", b);
}
Output:
a=12
b=2
Remember: The ternary operator is most suitable for simple if...else situations, especially when assigning a value based on a condition.

2. Nested if Statement

Definition: A nested if means placing one if statement inside another if statement.

We use nested if when one condition has to be checked only after another condition becomes True.

The inner if appears inside the True part of the outer if.

Example of Nested if

#include <stdio.h>

void main()
{
    int num = 12;

    if(num > 0)
    {
        if(num % 2 == 0)
        {
            printf("%d is even natural number", num);
        }
    }

    printf("\nRest of the program");
}

How does it work?

  1. First, the condition num > 0 is checked.
  2. If it is True, the inner if is executed.
  3. The inner condition num % 2 == 0 is checked.
  4. If both conditions are True, the message is displayed.

3. Practice Question

Write a C program to determine whether the given number is a multiple of both 5 and 10 using nested if.

Hint:

  1. First check whether the number is divisible by 5.
  2. If it is True, check whether it is divisible by 10.

4. Nested if using Logical AND

Sometimes a nested if can be replaced by a single if using the logical AND operator.

#include <stdio.h>

void main()
{
    int num = 12;

    if((num > 0) && (num % 2 == 0))
    {
        printf("%d is even natural number", num);
    }

    printf("\nRest of the program");
}
Key Point: The && operator requires all specified conditions to be True.

5. Other Statements Inside Outer if

There can be other statements inside the outer if along with the inner if.

#include <stdio.h>

void main()
{
    int num = 12;

    if(num > 0)
    {
        printf("%d is natural number", num);

        if(num % 2 == 0)
        {
            printf("\n%d is even number too", num);
        }

        printf("\nRest of the code in outer if");
    }

    printf("\nRest of the program");
}

6. Nested if with else

The else part can be used for both the outer if and the inner if.

#include <stdio.h>

void main()
{
    int num = 12;

    if(num > 0)
    {
        printf("%d is natural number", num);

        if(num % 2 == 0)
        {
            printf("\n%d is even number too", num);
        }
        else
        {
            printf("\n%d is odd number too", num);
        }

        printf("\nRest of the code in outer if");
    }
    else
    {
        printf("%d is not natural number", num);
    }

    printf("\nRest of the program");
}

Understanding the Program

  1. The outer if checks whether the number is greater than zero.
  2. If True, the number is considered a natural number.
  3. The inner if checks whether the number is even.
  4. If True, the even-number message is displayed.
  5. Otherwise, the inner else displays the odd-number message.
  6. If the outer condition is False, the outer else executes.

7. Practice Questions

A. Ternary Operator

  1. Write a C program to check whether a given number is positive or negative using the ternary operator.
  2. Write a C program to determine whether a given number is even or odd using the ternary operator.
  3. Write a C program to find the greater of two numbers using the ternary operator.
  4. Write a C program to find the smaller of two numbers using the ternary operator.
  5. Write a C program to check whether a student is Pass or Fail using the ternary operator. Consider 35 as the pass mark.
  6. Write a C program to assign 10 to b if a is greater than 50; otherwise assign 20 to b.

B. Nested if

  1. Write a C program to determine whether a given number is a multiple of both 5 and 10 using nested if.
  2. Write a C program to check whether a given number is positive and even using nested if.
  3. Write a C program to check whether a given number is positive and divisible by 5.
  4. Write a C program to check whether a given number is a natural number. If it is a natural number, determine whether it is even or odd.
  5. Write a C program to check whether a given number is divisible by both 3 and 7 using nested if.
  6. Write a C program to find the greatest among three numbers using nested if statements.

C. Think and Compare

  1. Rewrite a simple if...else assignment using the ternary operator.
  2. Rewrite a nested if program using the logical AND operator wherever possible.
  3. For a given problem, decide whether nested if, logical operators or the ternary operator is easier to understand.

8. Quick Revision

  • Ternary operator: ?:
  • Number of operands: 3
  • General form: condition ? expression1 : expression2;
  • Nested if: An if statement inside another if.
  • Outer if: The controlling if statement.
  • Inner if: The if statement placed inside the outer if.
  • && operator: Used when all specified conditions must be True.
  • Nested if with else: Both inner and outer if statements can have their own else.