- 2067 Asar · 2 marks
Syntax of if...else
Short answer
if (condition) { statements; } else { statements; } executes the first block when condition is true, otherwise the second block. Exactly one branch runs.
Write the syntax used in C programming language for if...else.
Syntax:
if (condition)
{
statements executed when the condition is true;
}
else
{
statements executed when the condition is false;
}
Exactly one of the two blocks executes. A chain is written as if ... else if ... else.
Example:
#include <stdio.h>
int main(void)
{
int n = 7;
if (n % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
Output:
Odd
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Compare switch and if-else-if ladder · Computer Programming, chapter 5 · asked 2×
- Conditional operator vs if-else; digit sum · Computer Programming, chapter 5
- Define programming language · Computer Programming, chapter 1 · asked 3×
- Syntax of while loop · Computer Programming, chapter 5
- Syntax of static · Computer Programming, chapter 6
- Syntax of struct · Computer Programming, chapter 8
- 8086 assembly language operators · Microprocessor, chapter 3 · asked 3×
- Syntax of operator overloading · Object Oriented Programming, chapter 5 · asked 4×
- Class and function template with syntax · Object Oriented Programming, chapter 9 · asked 4×
- Default arguments and their syntax · Object Oriented Programming, chapter 3 · asked 2×