✏️ Explanatory Question

What is a NULL statement?

👁 1,303 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

What is a NULL Statement?

A NULL statement is a statement that does not perform any action. It consists of only a semicolon (;) and is used when the syntax of the programming language requires a statement, but no operation needs to be executed.

Syntax

;

Why is a NULL Statement Used?

  • To represent an empty body in loops.
  • To satisfy the requirement of a statement in control structures.
  • To improve code readability in specific situations.

Example 1: NULL Statement in a Loop

#include 

int main() {
    int i = 1;

    while (i

Sometimes, the work of the loop is performed within the condition or update expression, making an empty loop body necessary.

Example 2: Empty Loop Using a NULL Statement

#include 

int main() {
    int i;

    for (i = 0; i < 5; i++)
        ;   // NULL statement

    printf("Value of i = %d", i);

    return 0;
}

In this example, the semicolon (;) acts as the loop body. The loop executes until the condition becomes false, but no statements are executed inside the loop.

Example 3: NULL Statement in an if Condition

#include 

int main() {
    int num = 10;

    if (num > 0)
        ;   // NULL statement

    printf("Program continues...");

    return 0;
}

Here, the if condition is checked, but no action is performed when the condition is true.

Advantages of NULL Statements

  • Useful when no action is required in a control structure.
  • Can simplify certain loop implementations.
  • Helps satisfy language syntax requirements.

Key Point

A NULL statement is an empty statement represented by a single semicolon (;). It performs no operation and is commonly used when a statement is required syntactically, but no action needs to be taken.