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 12pt/inherit;;inherit;;transparent>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 10pt/inherit;;inherit;;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> | print("branche if") |
elif n < 5 : #Non testée | elif n <5 : #Non testée |
<font inherit/inherit;;inherit;;inherit>print("branche elif")</font> | print("branche elif") |
else : | else : |
<font inherit/inherit;;inherit;;inherit>print("branche else")</font> | print("branche else") |
print("fin") | 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) |
| |
<font 12pt/inherit;;inherit;;transparent>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> | <code python> |
| n = 4 |
<code> | |
<font 10pt/inherit;;inherit;;inherit>n = 4 | |
print("Début") | print("Début") |
if n > 10 or n < 0 : #False | if n> 10 or n <0 : #False |
print("branche if")</font> | print("branche if") |
elif n < 5 : #True | elif n <5 : #True |
<font inherit/inherit;;inherit;;inherit>print("branche elif")</font> | print("branche elif") |
else : | else : |
<font inherit/inherit;;inherit;;inherit>print("branche else")</font> | print("branche else") |
print("fin") | 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) |
| |
<font 12pt/inherit;;inherit;;transparent>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> | <code python> |
| n = 8 |
<code> | |
<font 10pt/inherit;;inherit;;inherit>n = 8 | |
print("Début") | print("Début") |
if n > 10 or n < 0 : #False | if n> 10 or n <0 : #False |
print("branche if")</font> | print("branche if") |
elif n < 5 : #False | elif n <5 : #False |
<font inherit/inherit;;inherit;;inherit>print("branche elif")</font> | print("branche elif") |
else : | else : |
<font inherit/inherit;;inherit;;inherit>print("branche else")</font> | print("branche else") |
print("fin") | 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: |
| |
<font 12pt/inherit;;inherit;;transparent>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:</font> | <code python> |
| n = 12 |
| |
<code> | |
<font 10pt/inherit;;inherit;;inherit>n = 12 | |
print("Début") | print("Début") |
if n > 10 or n < 0 : | |
print</font><font inherit/inherit;;inherit;;inherit>(</font><font inherit/inherit;;inherit;;inherit>"branche if"</font><font inherit/inherit;;inherit;;inherit>)</font> | if n> 10 or n <0 : |
| |
| print("branche if") |
| |
print("Fin") | print("Fin") |
</code> | </code> |
| |
<font 12pt/inherit;;inherit;;transparent>Ce bloc affiche:</font> | Ce bloc affiche: |
| |
<code> | <code python> |
<font 10pt/inherit;;inherit;;inherit>Début | Début |
branche if | branche if |
Fin</font> | Fin |
</code> | </code> |
| |
| mais ce bloc |
| |
<font 12pt/inherit;;inherit;;transparent>mais ce bloc</font> | <code python> |
| n = 6 |
| |
<code> | |
<font 10pt/inherit;;inherit;;inherit>n = 6 | |
print("Début") | print("Début") |
if n > 10 or n < 0 : | |
print</font><font inherit/inherit;;inherit;;inherit>(</font><font inherit/inherit;;inherit;;inherit>"branche if"</font><font inherit/inherit;;inherit;;inherit>)</font> | if n> 10 or n <0 : |
| |
| print("branche if") |
| |
print("Fin") | print("Fin") |
</code> | </code> |
| |
<font 12pt/inherit;;inherit;;transparent>n'affiche que:</font> | n'affiche que: |
| |
| <code python> |
| Début |
| Fin |
| |
<code> | |
<font 10pt/inherit;;inherit;;inherit>Début | |
Fin</font> | |
</code> | </code> |
| |
| |