conditional statements FAQ in C,conditional statements Frequently asked questions in C Language

Q4. Can we skip braces around the body of the if else block in C?
A. Yes, if the if else block contains only one statement, we can skip the {} brackets around the text in the if-else block. However, because a block contains more than one statement, curly braces are needed to ensure that all statements in the condition are executed. In general, it is recommended to use curly braces even for individual statements to provide greater code freedom and avoid possible errors.


Q5. What are different types of if-else statements in C?
C has different types of if-else statements, which are defined as follows:
1. if Statement
2. if-else Statement
3. if-else-if Ladder
4. nested if else


Q6. what do you know about the syntax of the if-else statement?
A. The syntax of the if-else statement in C is:
if(condition)
{
// execute the block of code if given condition is true
}
else
{
// execute this block of code if condition is false
}


Q7. How do you avoid or simplify use of nested if-else statements in C?
In some cases, you can simplify or avoid using nested if-else statements by using logical operators (such as || for the "or" condition and && for the "and" condition). Reorganizing the code structure or using logical operators helps achieve the same functionality with a lower level of nesting, which ultimately increases code readability.


Q9.What are Advantages and Disadvantages of Switch Case Statements in C?
Switch case statements, like all software design constructs, have their Advantages or strengths and disadvantages or weaknesses. Accepting these advantages and disadvantages will help you determine when it is best to use switch case statements and when other alternatives, such as if-else statements, are appropriate.
Advantages:
1.Develops code readability and organisation.
2.Quicker execution due to compiler optimisation using jump table.
3.Shortens complex if-else chains.
4.It is ideal for multi-choice and menu-driven programming systems.
Disadvantages:
1.Supports only integer and character data types.
2.Comparison operators are not supported.
3.Cases can fall through if not terminated with a break
4.A variable or expression cannot be used as a case constant.


Q10.Give some real life Use Cases for Switch Case Statements.
Menu-Driven Programs:
  Switch cases are typically used to create menu-driven programs where user selections can lead to different pieces of code. It makes available a simple and stylish solution for executing different blocks of code based on user input.
State Machine Logic:
  Switch case statements can denote state machine logic very easily. For example, using the switch case statement, you can show different game states (loading, pause, running, game over, etc.).
Multiple Choices Scenarios:
  Switching case statements can be an efficient solution when you have multiple options based on the same variable or expression.
This is cleaner and more structured than using a series of if-else statements. In a variable subject statement, we can have multiple case labels. This means that we can have the same code for many cases.


Q11.Can I use Switch Case with float data types in C?
No, it's not possible to use switch case statement with float data type variables in C .It can only be used with integer and characters data types. Switch case also does not support double data types.


Q12.What is conditional operator in c?
(condition) ? expr1 : expr2
the conditional operator in C evaluates a condition and translates the result to a Boolean value. If the condition is true, expression expr1 will execute; Otherwise, expression expr2 will be executed. The result of the operator is either the result of expression1 or expression2. Only one of the two expressions is evaluated, while the other is ignored.


Q13.What are the advantages of using conditional statements?
The conditional statement offers the benefit of reducing code length and complexity, particularly in cases where the condition and resulting statement are simple. This can make code more readable, concise and clear by eliminating the need for explicit if-else blocks.


Q14. Is it possible to replace if statement with conditional operator or statement in C?
No, a conditional statement or opearator cannot directly replace an if statement because it requires three operations that must include the others.


Previous Topic:-->>Operator FAQ in C. || Next topic:-->>Loops FAQ in C.