In programming, it is necessary to execute statement depending on whether the condition is true or false. The if and if else are two decision making structures. Programming languages such as Java, C supports the decision-making structures such as if and if else. This article discusses the difference between if and if else. In both, the if contains the expression to evaluate. In if, the statements inside the if block will execute, if the condition is true and the control is passed to the next statement after the if block. In the if else, if the condition is true, the statements inside the if block will execute and if the condition is false the statements in the if else block will execute. That is the key difference between if and if else.
CONTENTS
1. Overview and Key Difference
2. What is if
3. What is if else
4. Similarities Between if and if else
5. Side by Side Comparison –if vs if else in Tabular Form
6. Summary
What is if?
The if statement consists of expressions. An expression can contain values, operators, constants or variables. If the evaluated expression is true, then the statements inside the if block execute. If the expression is false the control is passed to the very next statement after the if block. Most programming languages assume non-zero and non-null values as true and zero as false.
According to the above program, the number is a variable that can store integers. It contains the value 70. The expression in the if block is checked. As the number is greater than or equal to 50, the statement in the if block executes. After executing that, the control is passed in the next statement after the if block.
What is if else?
In if else, there are two blocks. The if statement contains an expression to evaluate. If the evaluated expression is true, then the statements inside the if block execute. At the end of the if block, the control is passed to the very next statement after the if block. If the expression is false, the control is passed to the else block and the statements of the else block execute. At the end of the else block, the control is passed to the next statement after the else block.
According to the above program, the number is a variable that can store integers. It contains the value 40. If the expression in the if statement is true, then the statement inside the if block will execute. Else the statement of the else block executes. The number is less than 50. Therefore, the else block executes. At the end of the else block, the control is passed to the next statement after the else block.
What are the Similarities Between if and if else?
- Both if and if else are decision making structures in programming.
- Both if and if else contains the if statement with a condition.
- In both if and if else, the if statement evaluates integers, character, floating point numbers or Boolean types.
- Both if and if else can evaluate the equality and the logical expressions.
What is the Difference Between if and if else?
if vs if else |
|
The if statement is a decision-making structure that consists of an expression followed by one or more statements. | The if else is a decision-making structure in which the if statement can be followed by an optional else statement that executes when the expression is false. |
Execution | |
In if, the statements inside the if block executes if the expression is true. If the expression is false the next statement after the if block executes. | In if else, the if block executes if the expression is true and if the expression is false the control is passed to the else block. |
Summary – if vs if else
There is various decision-making structure in programming. This article discussed two of them: if and if else. In if, the statements inside the if block will execute if the condition is true and the control is passed to the next statement after the if block. In the if else, if the condition is true, the statements inside the if block execute and if the condition is false the statements in the else block execute. That is the difference between if and if else.
Reference:
1.Point, Tutorials. “If statement in C.”, Tutorials Point, 8 Jan. 2018. Available here
2.Point, Tutorials. “If…else statement in C.”, Tutorials Point, 8 Jan. 2018. Available here