# Difference Between If Else and Conditional Operator (Part-2)

## Difference Between If Else and Conditional Operator

This post explain some points about if-else and difference between if else and conditional operator.

### Some important points about If else –

```1) Test Condition of if statement may be relational or logical statement
(a statement that results into either true or false).

2) Test condition of if must be enclosed in parenthesis.

3) The executable part of if or else must be enclosed in curly braces{..}
if the statement is compound statement (i.e. more than one statement).
If there is only one statement, then no need of curly braces {..}

4) Placement of semicolon(;) is also important. Do not put semicolon in
the line having test condition, as it changes the meaning.
For example -

5) A problem on nested if-else- The nested if-else statement introduces
some ambiguity which is called dangling-else problem. This problem
arises when in a nested if statement, the no. of ifs is more than the
no. of else clauses.
For example,

6) The ? alternate to if- In c++ there is an operator that can be used as
an alternative to if statement which is called conditional operator or
ternary operator (?:).
The general form of if-else is -

if(expression-1)
statement-1;
else
statement-2;

This form of if can be replaced by conditional operator(?:) as -

expression-1 ? statement-1 : statement-2```

### Comparison of if-else and conditional operator –

Following are some difference between if else and conditional operator –

 Basis Conditional Operator(? : ) If-else Provides Concise and clean code No Yes Provides compact code Yes No Flexible No Yes Compound statement or single statement Used for single expression Used for single as well as compound statement and expression. Expression or statement The (?:) produces an expression and returns a value. If then else is a statement. If can have multiple statements, multiple assignments and expression it its body Complexity Very complex when using in nested form and so, difficult to understand it. Not very much Complex Replacement of code Every conditional operator can be replaced with if-else code but every if else code can’t be replaced with if else as if else can have multiple statements.

### Incoming search terms:

• difference between if else and conditional operator in c
• compare an if and a ? : operator in c
• compare the use of if else constru
• difference between conditional operator and if else statement
• difference between if and conditional operator in tabular form
• difference between if and conditional operator in c
• difference between if and conditional operator
• Difference between conditional operators in C
• Difference between conditional oper
• write a c program using if else replaced by conditional operator