Question
Download Solution PDFWhich of the following is the correct syntax of an if statement in C?
Answer (Detailed Solution Below)
Detailed Solution
Download Solution PDFExplanation:
If Statement in C
Definition: The `if` statement is a fundamental control structure in the C programming language. It is used to evaluate a condition and execute a block of code if the condition is true. If the condition evaluates to false, the code block inside the `if` statement is skipped.
Correct Syntax: The correct syntax for an `if` statement in C is:
if (condition) { statement; }
Here:
- Condition: This is an expression that evaluates to either `true` (non-zero value) or `false` (zero value).
- Statement: This is the block of code that will execute if the condition evaluates to `true`. It can be a single statement or multiple statements enclosed in curly braces `{ }`.
Working Principle: The `if` statement in C works as follows:
- The condition inside the parentheses `( )` is evaluated.
- If the condition is `true` (non-zero value), the code inside the curly braces `{ }` is executed.
- If the condition is `false` (zero value), the code inside the curly braces `{ }` is skipped, and the program continues with the next statement after the `if` block.
Example:
#includeint main() { int number = 5; / If statement to check if the number is greater than 0 if (number > 0) { printf("The number is positive.\n"); } return 0; }
In this example:
- The condition
number > 0
is evaluated. - Since `number` is 5 (which is greater than 0), the condition is `true`, and the statement
printf("The number is positive.\n");
is executed.
Correct Option Analysis:
The correct option is:
Option 2: if (condition) { statement; }
This option correctly represents the syntax of an `if` statement in C. The condition is enclosed in parentheses `( )`, and the statement(s) to be executed if the condition is true are enclosed in curly braces `{ }`. This syntax adheres to the rules of the C programming language and is universally recognized as the standard format for `if` statements
Last updated on Jul 21, 2025
-> The RRB JE CBT 2 Result 2025 has been released on July 21, 2025 for all RRBs Zones (Ahmedabad, Bengaluru, Jammu-Srinagar, Kolkata, Malda, Mumbai, Ranchi, Secunderabad, and Thiruvananthapuram).
-> RRB JE CBT 2 Scorecard 2025 has been released along with cut off Marks.
-> RRB JE CBT 2 answer key 2025 for June 4 exam has been released at the official website.
-> Check Your Marks via RRB JE CBT 2 Rank Calculator 2025
-> RRB JE CBT 2 admit card 2025 has been released.
-> RRB JE CBT 2 city intimation slip 2025 for June 4 exam has been released at the official website.
-> RRB JE CBT 2 Cancelled Shift Exam 2025 will be conducted on June 4, 2025 in offline mode.
-> RRB JE CBT 2 Exam Analysis 2025 is Out, Candidates analysis their exam according to Shift 1 and 2 Questions and Answers.
-> The RRB JE Notification 2024 was released for 7951 vacancies for various posts of Junior Engineer, Depot Material Superintendent, Chemical & Metallurgical Assistant, Chemical Supervisor (Research) and Metallurgical Supervisor (Research).
-> The selection process includes CBT 1, CBT 2, and Document Verification & Medical Test.
-> The candidates who will be selected will get an approximate salary range between Rs. 13,500 to Rs. 38,425.
-> Attempt RRB JE Free Current Affairs Mock Test here
-> Enhance your preparation with the RRB JE Previous Year Papers.