Ceci est une ancienne révision du document !
Conditions
n = 6 print("Début") if n> 10 or n <0 : print("branche if") elif n <5 : print("branche elif") else : print("branche else") print("fin")
donne
Début branche else fin
Si n = 12, l'expression du “if” est vraie. Le programme exécute alors la ou les lignes de ce bloc puis passe à la suite (lignes rouges)
<font inherit/inherit;;red;;inherit>n = 12 print("Début") if n > 10 or n < 0 : #True print("branche if")</font>elif n < 5 : #Non testée print("branche elif") else : print("branche else") <font inherit/inherit;;red;;inherit>print("fin")</font>
Si n = 4, l'expression du “if” est fausse. Le programme teste donc l'expression du “elif” qui est vraie. Le programme exécute donc la ou les lignes de ce bloc puis passe à la suite (lignes rouges)
<font inherit/inherit;;red;;inherit>n = 4 print("Début") if n > 10 or n < 0 : #False</font> print("branche if") <font inherit/inherit;;red;;inherit>elif n < 5 : #True print("branche elif")</font>else : print("branche else") <font inherit/inherit;;red;;inherit>print("fin")</font>
Enfin, si n = 8, l'expression du “if” est fausse. Le programme teste donc l'expression du “elif” qui est fausse aussi. Il n'y a donc plus de conditions à tester, toutes les conditions sont fausses, le programme exécute alors la ou les lignes du bloc “else” puis passe à la suite (lignes rouges)
<font inherit/inherit;;red;;inherit>n = 8 print("Début") if n > 10 or n < 0 : #False</font> print("branche if") <font inherit/inherit;;red;;inherit>elif n < 5 : #False</font> print("branche elif") <font inherit/inherit;;red;;inherit>else : print("branche else") print("fin")</font>
Si toutes les expressions booléennes testées sont fausses et qu'il n'y a pas de else, le bloc conditionnel ne fait rien. Ex:
n =12
print(“Début”)
ifn >10or n «/font><font inherit/inherit;;rgb(204, 102, 204);;inherit>0:
<font inherit/inherit;;rgb(177, 177, 0);;inherit>print</font><font inherit/inherit;;rgb(0, 153, 0);;inherit>(</font><font inherit/inherit;;rgb(0, 0, 255);;inherit>"branche if"</font><font inherit/inherit;;rgb(0, 153, 0);;inherit>)</font>\\
print(“Fin”)
Ce bloc affiche:
Début branche if Fin
mais ce bloc
n =6
print(“Début”)
ifn >10or n «/font><font inherit/inherit;;rgb(204, 102, 204);;inherit>0:
<font inherit/inherit;;rgb(177, 177, 0);;inherit>print</font><font inherit/inherit;;rgb(0, 153, 0);;inherit>(</font><font inherit/inherit;;rgb(0, 0, 255);;inherit>"branche if"</font><font inherit/inherit;;rgb(0, 153, 0);;inherit>)</font>\\
print(“Fin”)
n'affiche que:
Début Fin