If vs. else if vs. else statements?
Statement like if
, else
and else if
are used in almost all programming languages to take a decision by the machine or software like Chrome ,Firefox and some other software....
if
will be written initially in the if statement code.else if
will be executed if codeif
is not true.else
will be executed if none of them are true.
Below example will gives you more understanding about it.
if( something is true ){ // execute this code; }
else if( if previous condition is not true){ // then execute this code;}
else { //if none of the above are true finally execute this code. }
you can use number of else if
statements between if
and else
, like example shown above also in the below. And remember "if" statement should start with if
and ends with else
here I declared if
code in two different ways.
below examples written in JavaScript ( concept apply same with Python )
Remember :
`elif` in (python) --same as-- `else if` in ( Java Script ).
print() in (python) --and-- document.write() in ( Java Script ).
Example 1:
var a=10; // declared variable with value `10`
if(a==20){ document.write("Twenty"); }
//above code is false because "a" value is not 20
else if(a==10){ document.write("Ten"); }
//above is true output comes as "Ten" a==10 //true
else if(a==10){ document.write("Forty"); }
// above also true because "a" is equal to 10 but it won't print in console
else{ document.write("None of them are correct!"); } //also not printed.
In the code above we declared var a=10
and else if
a==10
is true in 2 cases, but "Ten" will be printed in console. And rest of the code will not be executed (or) run.
we can do it another way, we declare it with all if statements like below.
Example 2:
var a = 10;
if(a==10){ document.write('ten'); } // it will be printed because condition is `true`;
if(a==20){ document.write('twenty') } // not printed `false`
if(a==10){ document.write("hundred") } // this also `true` therefore printed in console.
else{ //document.write("none")} // not printed because `false`
Difference explained here.
in the " 1st example " we write code with if
and else if
statements , where code was terminated, because condition is true at-least one time. And rest of the code will not be executed even the condition is true
.
In the "2nd example" we write code with all if
statements, the code was executed in all cases and prints all true
conditions in console, but in the 1st example it was not printed.
The first one is different
if True:
print 'high' #printed
if True:
print 'low' #printed
than the second
if True:
print 'high' #printed
elif True:
print 'low' #not printed
and the third is invalid syntax
See tutorial.
No, they are not the same.
if statement:
if statement:
If the first statement is true, its code will execute. Also, if the second statement is true, its code will execute.
if statement:
elif statment:
The second block will only execute here if the first one did not, and the second check is true.
if statement:
else:
The first statement will execute if it is true, while the second will execute if the first is false.