Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente |
python:conditions [2019/10/20 15:19] – physix | python:conditions [2020/07/24 03:31] (Version actuelle) – modification externe 127.0.0.1 |
---|
====== Conditions ====== | ====== Conditions ====== |
| |
<code> | <code python> |
n = 6 | n = 6 |
print("Début") | print("Début") |
</code> | </code> |
| |
<font 14px font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;/ ;;inherit;;rgb(51, 51, 51) font-family: "open sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;>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> | 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) |
| |
<code> | <code python> |
<font inherit/inherit;;red;;inherit>n = 12 | n = 12 |
print("Début") | print("Début") |
if n > 10 or n < 0 : #True | if n> 10 or n <0 : #True |
print("branche if")</font>elif n < 5 : #Non testée | print("branche if") |
print("branche elif") | elif n <5 : #Non testée |
| print("branche elif") |
else : | else : |
print("branche else") | print("branche else") |
<font inherit/inherit;;red;;inherit>print("fin")</font> | print("fin") |
</code> | </code> |
| |
| |
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) | 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) |
| |
<code> | <code python> |
<font inherit/inherit;;red;;inherit>n = 4 | n = 4 |
print("Début") | print("Début") |
if n > 10 or n < 0 : #False</font> print("branche if") | if n> 10 or n <0 : #False |
<font inherit/inherit;;red;;inherit>elif n < 5 : #True | print("branche if") |
print("branche elif")</font>else : | elif n <5 : #True |
print("branche else") | print("branche elif") |
<font inherit/inherit;;red;;inherit>print("fin")</font> | else : |
| print("branche else") |
| print("fin") |
</code> | </code> |
| |
| |
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) | 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) |
| |
<code> | <code python> |
<font inherit/inherit;;red;;inherit>n = 8 | n = 8 |
print("Début") | print("Début") |
if n > 10 or n < 0 : #False</font> print("branche if") | if n> 10 or n <0 : #False |
<font inherit/inherit;;red;;inherit>elif n < 5 : #False</font> print("branche elif") | print("branche if") |
<font inherit/inherit;;red;;inherit>else : | elif n <5 : #False |
print("branche else") | print("branche elif") |
print("fin")</font> | else : |
| print("branche else") |
| print("fin") |
</code> | </code> |
| |
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: | 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: |
| |
<code> | <code python> |
| n = 12 |
| |
</code> | print("Début") |
| |
n <font inherit/inherit;;rgb(51, 153, 51);;inherit>=</font><font inherit/inherit;;rgb(204, 102, 204);;inherit>12</font>\\ | if n> 10 or n <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>"Début"</font><font inherit/inherit;;rgb(0, 153, 0);;inherit>)</font>\\ | |
<font inherit/inherit;;rgb(177, 177, 0);;inherit>if</font>n <font inherit/inherit;;rgb(51, 153, 51);;inherit>></font><font inherit/inherit;;rgb(204, 102, 204);;inherit>10</font>or n <font inherit/inherit;;rgb(51, 153, 51);;inherit><</font><font inherit/inherit;;rgb(204, 102, 204);;inherit>0</font><font inherit/inherit;;rgb(51, 153, 51);;inherit>:</font>\\ | print("branche if") |
<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>\\ | |
<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>"Fin"</font><font inherit/inherit;;rgb(0, 153, 0);;inherit>)</font> | print("Fin") |
| |
| </code> |
| |
Ce bloc affiche: | Ce bloc affiche: |
| |
<code> | <code python> |
Début | Début |
branche if | branche if |
mais ce bloc | mais ce bloc |
| |
<code> | <code python> |
| n = 6 |
| |
</code> | print("Début") |
| |
n <font inherit/inherit;;rgb(51, 153, 51);;inherit>=</font><font inherit/inherit;;rgb(204, 102, 204);;inherit>6</font>\\ | if n> 10 or n <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>"Début"</font><font inherit/inherit;;rgb(0, 153, 0);;inherit>)</font>\\ | |
<font inherit/inherit;;rgb(177, 177, 0);;inherit>if</font>n <font inherit/inherit;;rgb(51, 153, 51);;inherit>></font><font inherit/inherit;;rgb(204, 102, 204);;inherit>10</font>or n <font inherit/inherit;;rgb(51, 153, 51);;inherit><</font><font inherit/inherit;;rgb(204, 102, 204);;inherit>0</font><font inherit/inherit;;rgb(51, 153, 51);;inherit>:</font>\\ | print("branche if") |
<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>\\ | |
<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>"Fin"</font><font inherit/inherit;;rgb(0, 153, 0);;inherit>)</font> | print("Fin") |
| |
| </code> |
| |
n'affiche que: | n'affiche que: |
| |
<code> | <code python> |
Début | Début |
Fin | Fin |