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
Evaluate the condition.
If it is non-zero, execute the body.
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
Explain the simple if statement with syntax and an example. · Download diagram