Difference between else if and switch Structures in Java

Java Conditional Statements (if statement, switch statement, else if, nested if, positive conditions and negative conditions), Java Loop Statements (for loop, while loop, do while loop and enhanced for loop) and Java Branching Statements (break,continue and return).
Post Reply
Gowtham Ramesh
Posts: 66
Joined: Thu Nov 22, 2018 5:23 am

Difference between else if and switch Structures in Java

Post by Gowtham Ramesh » Wed Dec 05, 2018 6:19 am

What is the difference between else if and switch Structures in Java Programming?

kishore
Posts: 55
Joined: Thu Jan 03, 2019 3:57 am

Re: Difference between else if and switch Structures in Java

Post by kishore » Sat Jan 05, 2019 6:09 am

Key Differences Between if-else and switch:
====================================
-->Expression inside if statement decide whether to execute the statements inside if block or under else block.
On the other hand, expression inside switch statement decide which case to execute.
-->You can have multiple if statement for multiple choice of statements.
In switch you only have one expression for the multiple choices.
-->If-esle statement checks for equality as well as for logical expression .
On the other hand, switch checks only for equality.
-->The if statement evaluates integer, character, pointer or floating-point type or boolean type.
On the other hand, switch statement evaluates only character or a integer datatype.
-->Sequence of execution is like either statement under if block will execute or statements under else block statement will execute.
On the other hand the expression in switch statement decide which case to execute and if you do not apply a break statement after each case it will execute till the end of switch statement.
-->If expression inside if turn outs to be false, statement inside else block will be executed.
If expression inside switch statement turn out to be false then default statements is executed.
-->It is difficult to edit if-else statements as it is tedious to trace where the correction is required.
On the other hand it is easy to edit switch statements as they are easy to trace.

srini
Posts: 85
Joined: Thu Jan 03, 2019 3:58 am

Re: Difference between else if and switch Structures in Java

Post by srini » Thu Jan 10, 2019 4:28 am

If-else” and “switch” both are selection statements.
The selection statements, transfer the flow of the program to the particular block of statements based upon whether the condition is “true” or “false”
The fundamental difference between if-else and switch statements is that the if-else statement.
selects the execution of the statements based upon the evaluation of the expression in if statements”
The switch statements select the execution of the statement often based on a keyboard command”

Post Reply

Return to “Java Control Flow”