Difference Between Similar Terms and Objects

Difference Between If and Else If

If vs Else If

Programs do not always flow in a sequential manner. Situations do arise when there is a decision to be made or a piece of code that needs to be repeated. Control structures aid this by defining what needs to be done in the program and under what conditions. These are known as conditional statements that judge a statement on Boolean outputs of true or false.

“If” clause – It is popular among a lot of programming languages. The main syntax may differ, but the pseudo code remains the same. A small code snippet is given below:

if (condition)

then

action

end if

The condition always returns a Boolean result. Say the condition is sunny and the action is to go outside. So if it is sunny outside (true), then a person goes for a walk. Otherwise, we reach the end if constructed without performing any action.

The above construct is popular in structure programming languages like: C, C++, JAVA, Visual Basic, and C #

The “else if” construct helps you in combining many conditions. If the first condition is found to be true, then the rest are skipped. Let’s take a look at the pseudo code –

if (condition)

then

action

else if (condition)

then

action

else

action

end if

A condition still returns a Boolean output. An “else if” block leads to a further level of nesting. In case the “if” condition is false, then the “else if” condition is evaluated in a sequential manner till a match is found. In case all conditions fail, then the action defined in the “else” clause is executed. To understand it better, let’s continue with the above rainy condition. The “if” condition remains the same. Lets add the “else if” condition to be “stormy,” and the action to be “going downstairs.” The “else” clause says “staying inside.” So if it’s stormy outside, then the first “if” condition evaluates to be false. We go to the “else if” condition and find that it is evaluating to be true. So the person goes downstairs.

Differences between “if” and “else if”:

1. The “if” condition is mandatory for a conditional construct. An “else if” condition

cannot exist without a preceding “if” block.

2. You can have only one “if” block but multiple “else if” blocks.

Summary:

1. Both “if” and “else if” helps to branch the code. The program moves out of its linear

execution.

2. Both “if” and “else if” are conditional expressions that help in the decision-making

process.

3. All high-level programming languages employ “if” and “else if” with different syntax.

Sharing is caring!


Search DifferenceBetween.net :




Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


3 Comments

  1. What’s up to every , for the reason that I am genuinely keen of reading this web site’s post to be updated regularly.
    It carries pleasant information.

  2. This is over complicating a simple concept.

    “else if” is just “else “.
    In this case, = “if”.

    “else” is executed if the condition fails (false).
    In this case the next “if” is executed… and so on..

  3. I don’t really have an opinion on this to be fair

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about :
Protected by Copyscape Plagiarism Finder