Skip to study material
← Question index

QUESTION 04 · Selection

Explain the simple if statement with syntax and an example.

5 marks

Answer in brief

The simple if statement executes a statement or block only when a condition is true.

Syntax

if (condition) {
    /* statements executed when true */
}

Working

  1. Evaluate the condition.
  2. If it is non-zero, execute the body.
  3. If it is zero, skip the body. Continue after the if statement in either case.

Example explanation

Suppose a student receives a certificate when marks are at least 90. For marks = 92, marks >= 90 is true, so the certificate message is printed. For marks = 70, that message is skipped. The final message still prints because it is outside the if block.

Important points

Use braces for a multi-statement body and preferably for a single statement too. Write == to test equality and = to assign a value. Do not put an accidental semicolon after if (condition): that creates an empty body, and a following block executes independently. An if statement makes a decision once; it does not repeat its body. For this fixed-size example, the comparison and optional message require constant work, O(1).

Flowchart

Control flow for if with labelled true and false paths.
Explain the simple if statement with syntax and an example. · Download diagram

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    int marks = 92;
    if (marks >= 90) {
        puts("Certificate awarded");
    }
    puts("Check complete");
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

Certificate awarded
Check complete

Related exam questions

Related questions in the selection category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • March/April 2023 · Q3(a) · 5 marks

    Discuss decision-making statements with an example.

    Paper P02 · page 1
  • June 2022 · Q2(b) · 8 marks

    Illustrate forms of if statements with syntax and examples.

    Paper P03 · page 1

View this category in the exam question bank →