What is the function of the if-else
statement in C?
a) To execute a block of code based on a condition
b) To declare a variable
c) To iterate over a block of code
d) To perform arithmetic operations
Answer:
a) To execute a block of code based on a condition
Explanation:
The if-else
statement in C is used to execute a block of code depending on whether a specific condition is true or false. If the condition evaluates to true, the code inside the if
block is executed. If it is false, the code inside the else
block (if present) is executed instead. This allows for conditional execution, which is crucial for controlling the flow of a program.
Understanding how to use if-else
statements effectively is key to implementing decision-making logic in your programs, allowing your code to respond dynamically to different inputs and conditions.