What are the differences between if, else and else if?

I'm trying to understand the difference between

if else else if 

When do you use them, and when not?

I have homework with tons of instances, and I am facing a code error due to ignorance of the differences between them.

Can anyone determine how to use them?

+6
c
source share
17 answers
 **IF** you are confused read the c# spec **ELSE IF** you are kind of confused read some books **ELSE** everything should be OK. 

:)

+20
source share

The if follows this structure:

 if (condition) { // executed only if "condition" is true } else if (other condition) { // executed only if "condition" was false and "other condition" is true } else { // executed only if both "condition" and "other condition" were false } 

The if part is the only block that is absolutely mandatory. else if allows you to say "ok, if the previous condition was not true, then if this condition is true ...". Another says: "If none of the above conditions were true ..."

You can have several else if blocks, but only one if block and only one (or zero) else .

+53
source share

If-elseif-else can be written as a nested if-else. They are (logically speaking) equivalent:

 if (A) { doA(); } else if (B) { doB(); } else if (C) { doC(); } else { doX(); } 

matches with:

 if (A) { doA(); } else { if (B) { doB(); } else { if (C) { doC(); } else { doX(); } } } 

As a result, only one of doA , doB , doC or doX will be evaluated.

+28
source share

If, else and else if are all constructs that help fork out the code. Basically, you use them when you want to make a decision.

An example would be 'if it is sunny, I will go outside. Otherwise I will stay inside'

In code (ignoring unnecessary things)

 if (sunny) { goOutside(); } else { stayInside(); } 

You can use the else if statement if you want to add additional conditions. Extending the previous example: “if it is sunny, I will go outside. If it is stormy, I will go to the basement, otherwise I will stay inside”

In code

 if (sunny) { goOutside(); } else if (stormy) { goDownstairs(); } else { stayInside(); } 

EDIT Section:

Here's how you can write a few if and as and conditions. The following example can be written in at least two ways:

“If it's sunny and warm, go outside. If it's sunny and cold, don't do anything.”

 if (sunny) { if (warm) { goOutside(); } else if (cold) { doNothing(); } } 

OR

 if (sunny && warm) { goOutside(); } else if (sunny && cold) { doNothing(); } 
+13
source share

No " else if ". You have the following:

 if (condition) statement or block 

Or:

 if (condition) statement or block else statement or block 

In the first case, the statement or block is satisfied if the condition is true (other than 0). In the second case, if the condition is true, the first statement or block is executed, otherwise the second statement or block is executed.

So, when you write " else if ", it is an " else statement ", where the second statement is the if . You may have problems if you try to do this:

 if (condition) if (condition) statement or block else statement or block 

The problem here is that you want the " else " to refer to the first " if ", but you are actually referring to the second. You will fix this by doing:

 if (condition) { if (condition) statement or block } else statement or block 
+9
source share

Dead Simple Pseudo-Code Explanation:

 /* If Example */ if(condition_is_true){ do_this } now_do_this_regardless_of_whether_condition_was_true_or_false /* If-Else Example */ if(condition_is_true){ do_this }else{ do_this_if_condition_was_false } now_do_this_regardless_of_whether_condition_was_true_or_false /* If-ElseIf-Else Example */ if(condition_is_true){ do_this }else if(different_condition_is_true){ do_this_only_if_first_condition_was_false_and_different_condition_was_true }else{ do_this_only_if_neither_condition_was_true } now_do_this_regardless_of_whether_condition_was_true_or_false 
+5
source share

I think this helps to think of “else” as the word OTHERWISE.

so you read it like this:

 if (something is true) { // do stuff } otherwise if (some other thing is true) { // do some stuff } otherwise { // do some other stuff :) } 
+3
source share
 if (condition) { thingsToDo().. } else if (condition2) { thingsToDoInTheSecondCase().. } else { thingsToDoInOtherCase().. } 
+3
source share
 if (numOptions == 1) return "if"; else if (numOptions > 2) return "else if"; else return "else"; 
+2
source share

They mean what they mean in English.

IF condition is true, do something, ELSE (otherwise) If another condition is true, do something, ELSE do it when everything else fails.

Note that there is nothing if you intentionally construct it, only if else, but the syntax allows you to put else and, if together, the convention will not build them deeper when you do this. For example:

 if( x ) { ... } else if( y ) { ... } else { ... } 

Syntactically identical:

 if( x ) { ... } else { if( y ) { ... } else { ... } } 

The syntax in both cases is:

 if *<statment|statment-block>* else *<statment|statment-block>* 

and if it is itself a status, so only the syntax supports using else if

+2
source share

else if can be used in conjunction with 'if' and 'else' to further disrupt the logic

 //if less than zero if( myInt < 0){ //do something }else if( myInt > 0 && myInt < 10){ //else if between 0 and 10 //do something }else{ //else all others //do something } 
+1
source share

If statement syntax

 if(condition) something; // executed, when condition is true else otherthing; // otherwise this part is executed 

So, basically, else is part of the if construct (something and the other are often compound statements enclosed in {} and else , the part is essentially optional). And else if is a combination of two if s, where otherthing is if itself.

 if(condition1) something; else if(condition2) otherthing; else totallydifferenthing; 
+1
source share

These are the main decisions that you make in most programming languages; this will help you determine the flow of actions that your program will do. if it tells the compiler that you have a question and the question is between brackets

 if (condition) { thingsToDo().. } 

the else part is in addition to this structure to tell the compiler what to do if the condition is false

 if (condition) { thingsToDo().. } else { thingsToDoInOtherCase().. } 

you can combine them to form an else if when the first condition is false, but you want to ask another question before deciding what to do.

 if (condition) { thingsToDo().. } else if (condition2) { thingsToDoInTheSecondCase().. }else { thingsToDoInOtherCase().. } 
+1
source share

The if statement uses the results of a logical expression to decide whether one of the two code blocks will be executed.

Using this code

 if (logical expression) { code block 1; } else { code block 2; } 

if the logical expression is true, only code 1 statements will be executed; if false, only instructions in code block 2.

If it is necessary to conduct several similar tests (for example, if we test a number less than zero, equal to zero or greater than zero), then the second test can be placed as the first statement block code else.

 if (logical expression 1) { code block 1; } else { if (logical expression 2) { code block 2; } else { code block 3; } } 

In this case, code block 1 is executed if logical expression 1 is true; code block 2, if logical expression 1 is false and logical expression 2 is true; code 3 if both logical expressions are false.

Obviously, this can be repeated with another if statement as the first code statement 3.

The else if statement is just a reformatted version of this code.

 if (logical expression 1) { code block 1; } else if (logical expression 2) { code block 2; } else { code block 3; } 
+1
source share

If both, if both are used to verify conditions.

I take the case of If and else ..

If the compiler checks all cases of Wether, this is true or false. if no block executes, then the other part will execute.

in the case of else, if the compiler stops the program flow when it receives a false value. he does not read the whole program. The best performance we use otherwise.

But both matter depending on the situation.

Let’s take an example of an ordering menu if I use it differently, if then it will fit well because the user can only check one. and this will give an error so I use if here ..

  StringBuilder result=new StringBuilder(); result.append("Selected Items:"); if(pizza.isChecked()){ result.append("\nPizza 100Rs"); totalamount+=100; } if(coffe.isChecked()){ result.append("\nCoffe 50Rs"); totalamount+=50; } if(burger.isChecked()){ result.append("\nBurger 120Rs"); totalamount+=120; } result.append("\nTotal: "+totalamount+"Rs"); //Displaying the message on the toast Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show(); } now else if case if (time < 12) { greeting = "Good morning"; } else if (time < 22) { greeting = "Good day"; } else { greeting = "Good evening"; } 

here only one condition is satisfied. and in case several conditions can be fulfilled ...

0
source share

you can think of if and else as a pair that satisfies two actions for one given condition .
ex: if is raining, take an umbrella else go without an umbrella.
there are two actions

  • go with an umbrella
  • without umbrella

and both of these actions are associated with one condition, ie is it raining?

Now consider a scenario in which there are several conditions and actions associated with each other.
ex: if you are hungry and you have not broken, enjoy your meal at kfc, else if you are hungry but you have broken, try to compromise, else if you are not hungry, but you just want to chat in a cafe, try startbucks, else do everything just don't ask me about hunger or food. I have great things to worry about.
else if to combine all actions that are between the if and else .

0
source share

What if says:

I believe or not, always check other conditions.

What else if says:

Only check other conditions if I was not right.

0
source share

All Articles