Are:
if statement:
if statement:
the same as
if statement:
elif statment:
and
if statement:
else statement:
the same? If not, what's the difference?
Are:
if statement:
if statement:
the same as
if statement:
elif statment:
and
if statement:
else statement:
the same? If not, what's the difference?
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.
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.
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 code if
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.
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`
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.
if statement:
if statement:
It is like individual conditions; each if
statement is checked one after another.
The same as:
if statement:
elif statment:
It is like: the first if
condition failed then check the next after the condition.
And:
if statement:
else statement:
It is like: Check the first if
condition, and then execute the else
block.
no, not the same.
if statement:
if statement:
second if executes whether first if executed or not.
if statement:
elif statment:
elif
only executes if first if
passes the statement to it. you can have any number of elif
statements.
if statement:
else statement:
this is nearly same as if
and elif
statement. if first if
condition doesn't satisfy the requirements, then it passes to the else
which can be happen if the conditions not satisfied.
They are not the same. if
executes if the condition is True, elif
executes if the if
is false and the elif
is true, and else
executes if the if
is false.
Example:
if True:
print('This will be printed') #This will be printed
if True:
print('This will also be printed') #This will also be printed
if True:
print('This will be printed') #This will be printed
elif True:
print('This will not be printed')
if False:
print('This will not be printed')
else:
print('This will be printed') #This will be printed