21

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eed
  • 385
  • 1
  • 2
  • 7

6 Answers6

50

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.

Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
  • else if provides an elongation of the "if check" in that you can finally end it with else to cover up all remaining conditions – Prabhat Gaur Dec 15 '20 at 15:58
11

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.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
4

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....

  1. if will be written initially in the if statement code.

  2. else if will be executed if code if is not true.

  3. 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.

anynomus
  • 62
  • 2
  • `else if( if previous condition is not true){ // then execute this code;}` That is not true though, because in else if() you put the new condition you want to check for, and that one will only be checked if the previous one returned false. From your example, it's not clear that else if means you will be writing a new condition. – iBobb Dec 01 '21 at 12:15
2
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Krish R
  • 22,583
  • 7
  • 50
  • 59
1

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.

0

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 
UserName Name
  • 267
  • 2
  • 3