{"id":1998,"date":"2020-04-12T18:45:51","date_gmt":"2020-04-12T16:45:51","guid":{"rendered":"http:\/\/www.ardpylab.fr\/?page_id=1998"},"modified":"2020-05-10T15:41:16","modified_gmt":"2020-05-10T13:41:16","slug":"structures-conditionnelles","status":"publish","type":"page","link":"https:\/\/www.ardpylab.fr\/?page_id=1998","title":{"rendered":"Structures conditionnelles"},"content":{"rendered":"\n<h4>&nbsp;<\/h4>\n<h4><span style=\"color: #0000ff;\"><strong><a id=\"if1\"><\/a>&#8211; <span style=\"text-decoration: underline;\">Condition if&nbsp;<\/span><\/strong><\/span><\/h4>\n<p>L&rsquo;instruction <strong>if<\/strong> (\u00ab\u00a0si\u00a0\u00bb en fran\u00e7ais), utilis\u00e9e avec un op\u00e9rateur logique de comparaison, permet de tester si une condition est vraie.<\/p>\n<p>Le format d&rsquo;un test <strong>if<\/strong> est le suivant :<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 39px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 21px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">if&nbsp; uneVariable &gt; 50&nbsp;:<\/span><\/p>\n<p><span style=\"color: #000000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #808080;\"># Instructions&nbsp; (<em>attention aux tabulations !<\/em>)<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>Dans cet exemple, le programme va tester si la variable <strong>uneVariable<\/strong> est sup\u00e9rieure \u00e0 50. Si c&rsquo;est le cas, le programme va r\u00e9aliser une action particuli\u00e8re. Autrement dit, si l&rsquo;\u00e9tat du test est vrai, le bloc d\u2019instructions (ligne d\u2019instructions de m\u00eame indentation) apr\u00e8s le caract\u00e8re&nbsp;\u201d<strong>:<\/strong>\u201d&nbsp; est ex\u00e9cut\u00e9.<\/p>\n<p><u>Exemple<\/u>&nbsp;:<\/p>\n<p>Une valeur est donn\u00e9e \u00e0 une variable et si cette valeur est sup\u00e9rieure \u00e0 5, alors celle-ci est incr\u00e9ment\u00e9e de 1 et affich\u00e9e.<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.ardpylab.fr\/downloads\/python\/structure\/if.py\" data-wplink-url-error=\"true\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"131\" class=\"wp-image-1987 aligncenter\" style=\"border: 3px solid #eeeeee; padding: 3px; margin: 3px;\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript16.png\" alt=\"\" srcset=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript16.png 684w, https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript16-300x57.png 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/a><\/figure>\n<\/div>\n<p><u>R\u00e9sultat <\/u><u>dans la fen\u00eatre Python Shell<\/u>&nbsp;:<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"59\" class=\"wp-image-2002 aligncenter\" style=\"border: 3px solid #eeeeee; padding: 3px; margin: 3px;\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript17-1.png\" alt=\"\" srcset=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript17-1.png 685w, https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript17-1-300x26.png 300w\" sizes=\"auto, (max-width: 685px) 100vw, 685px\" \/><\/figure>\n<\/div>\n<p>&nbsp;<\/p>\n\n\n\n<p><u>Rappel des op\u00e9rateurs logiques de comparaison<\/u>&nbsp;:<\/p>\n<ul>\n<li>x&nbsp;==&nbsp;y&nbsp;est vrai quand x est \u00e9gal \u00e0 y,<\/li>\n<li>x&nbsp;!=&nbsp;y&nbsp;est vrai quand x est diff\u00e9rent de y,<\/li>\n<li>x&nbsp;&gt;&nbsp;y&nbsp;est vrai quand x est strictement sup\u00e9rieur \u00e0 y,<\/li>\n<li>x&nbsp;&lt;&nbsp;y&nbsp;est vrai quand x est strictement inf\u00e9rieur \u00e0 y,<\/li>\n<li>x&nbsp;&gt;=&nbsp;y&nbsp;est vrai quand x est sup\u00e9rieur ou \u00e9gal \u00e0 y,<\/li>\n<li>x&nbsp;&lt;=&nbsp;y&nbsp;est vrai quand x est inf\u00e9rieur ou \u00e9gal \u00e0 y.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #0000ff;\"><strong><a id=\"if2\"><\/a>&#8211;<\/strong> <span style=\"text-decoration: underline;\"><strong>Condition if \/ else<\/strong><\/span><\/span><\/h4>\n<p>L&rsquo;instruction <strong>if \/ else<\/strong> (si\/sinon en fran\u00e7ais) permet un meilleur contr\u00f4le du d\u00e9roulement du programme que la simple instruction if, en permettant de grouper plusieurs tests ensemble.<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 21px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 21px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">if var &gt; 10:<\/span><br><span style=\"color: #000000;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #808080;\">&nbsp;#action A<\/span><\/span><br><span style=\"color: #000000;\">else:<\/span><br><span style=\"color: #000000;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #808080;\">&nbsp; #action B<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p><u>Exemple<\/u>&nbsp;:<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.ardpylab.fr\/downloads\/python\/structure\/ifelse.py\" data-wplink-url-error=\"true\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"192\" class=\"wp-image-1990 aligncenter\" style=\"border: 3px solid #eeeeee; padding: 3px; margin: 3px;\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript18.png\" alt=\"\" srcset=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript18.png 686w, https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript18-300x84.png 300w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/a><\/figure>\n<\/div>\n<p><u>R\u00e9sultat <\/u><u>dans la fen\u00eatre Python Shell<\/u>&nbsp;:<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"835\" height=\"67\" class=\"wp-image-2000 aligncenter\" style=\"border: 3px solid #eeeeee; padding: 3px; margin: 3px;\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript22.png\" alt=\"\" srcset=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript22.png 835w, https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript22-300x24.png 300w, https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript22-768x62.png 768w\" sizes=\"auto, (max-width: 835px) 100vw, 835px\" \/><\/figure>\n<\/div>\n<p>&nbsp;<\/p>\n\n\n\n<h4><span style=\"color: #0000ff;\"><strong><a id=\"if3\"><\/a>&#8211; <span style=\"text-decoration: underline;\">Condition elif<\/span><\/strong><\/span><\/h4>\n<p>L\u2019instruction <strong>else<\/strong> peut contenir un autre test <strong>if<\/strong>, et donc des tests multiples, mutuellement exclusifs peuvent \u00eatre r\u00e9alis\u00e9s en m\u00eame temps. On peut alors utiliser le mot cl\u00e9 <strong>elif <\/strong>\u00e0 la place de<strong> else&nbsp;: if &#8230;<\/strong>.<\/p>\n<p>Chaque test sera r\u00e9alis\u00e9 apr\u00e8s le suivant jusqu&rsquo;\u00e0 ce qu&rsquo;un test VRAI soit rencontr\u00e9. <br>Quand une condition vraie est rencontr\u00e9e, les instructions associ\u00e9es sont r\u00e9alis\u00e9es, puis le programme continue son ex\u00e9cution \u00e0 la ligne suivant l&rsquo;ensemble de la construction <strong>if\/elif<\/strong>.<br>Si aucun test n&rsquo;est VRAI, le bloc d&rsquo;instructions par d\u00e9faut <strong>else<\/strong> est ex\u00e9cut\u00e9, s\u2019il est pr\u00e9sent, d\u00e9terminant ainsi le comportement par d\u00e9faut.<\/p>\n<p>Un bloc <strong>elif<\/strong> peut \u00eatre utilis\u00e9 avec ou sans bloc de conclusion <strong>else<\/strong>. <br>Un nombre illimit\u00e9 de branches <strong>elif<\/strong> est autoris\u00e9.<\/p>\n<p><u>Exemple<\/u>&nbsp;:<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.ardpylab.fr\/downloads\/python\/structure\/Elif.py\" data-wplink-url-error=\"true\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"194\" class=\"wp-image-1992 aligncenter\" style=\"border: 3px solid #eeeeee; padding: 3px; margin: 3px;\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript19.png\" alt=\"\" srcset=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript19.png 686w, https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript19-300x85.png 300w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/a><\/figure>\n<\/div>\n<p>&nbsp;<\/p>\n\n\n\n<h4><strong><span style=\"color: #0000ff;\"><a id=\"if4\"><\/a>&#8211; <span style=\"text-decoration: underline;\">AND \/ OR<\/span><\/span><\/strong><\/h4>\n<p>Il est possible d&rsquo;affiner une condition avec les mots cl\u00e9 <strong>AND<\/strong> qui signifie \u00a0\u00bb ET \u00a0\u00bb et <strong>OR<\/strong> qui signifie \u00a0\u00bb OU \u00ab\u00a0.<br>Ces op\u00e9rateurs peuvent \u00eatre utilis\u00e9s \u00e0 l&rsquo;int\u00e9rieur de la condition d&rsquo;une instruction <strong>if<\/strong> pour associer plusieurs conditions \u00e0 tester.<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 21px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 21px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">if var &gt;= 5 and var &lt;= 10 : <span style=\"color: #808080;\"># est VRAI seulement si var appartient \u00e0 l\u2019intervalle [5;10]<\/span><\/span><\/p>\n<p style=\"padding-left: 40px;\"><span style=\"color: #000000;\"><span style=\"color: #808080;\"># bloc d\u2019instructions<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">else:<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span style=\"color: #000000;\"><span style=\"color: #808080;\"># bloc d\u2019instructions<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 21px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 21px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">if var1 &gt; 0 or var2 &gt; 0 : <span style=\"color: #808080;\"># est vrai si var1 sup\u00e9rieur \u00e0 0 ou si var2 sup\u00e9rieur \u00e0 0<\/span><\/span><\/p>\n<p style=\"padding-left: 40px;\"><span style=\"color: #808080;\"># bloc d\u2019instructions<\/span><\/p>\n<p><span style=\"color: #000000;\">else:<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span style=\"color: #808080;\"># bloc d\u2019instructions<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p><u>Remarque<\/u>&nbsp;:<\/p>\n<p>Python permet aussi l\u2019encha\u00eenement des comparaisons \u00e0 l\u2019aide d\u2019une double in\u00e9galit\u00e9.<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.ardpylab.fr\/downloads\/python\/structure\/doubleinegal.py\" data-wplink-url-error=\"true\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"134\" class=\"wp-image-1994 aligncenter\" style=\"border: 3px solid #eeeeee; padding: 3px; margin: 3px;\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript20.png\" alt=\"\" srcset=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript20.png 686w, https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript20-300x59.png 300w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/a><\/figure>\n<\/div>\n<p>R\u00e9sultat dans la fen\u00eatre Python Shell :<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"51\" class=\"wp-image-1995 aligncenter\" style=\"border: 3px solid #eeeeee; padding: 3px; margin: 3px;\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript21.png\" alt=\"\" srcset=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript21.png 686w, https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/04\/pythonscript21-300x22.png 300w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/figure>\n<\/div>\n<p>&nbsp;<\/p>\n\n\n<div  class=\"grids-section grids-is-stretch\" style=\"--_gs-gap-desktop:0px 0px;--_gs-m-desktop:0 0 0 0;--_gs-p-desktop:0 0 0 0;--_gs-bg-desktop:transparent none;--_gs-bg-xp-desktop:0px;--_gs-zi-desktop:auto;--_gs-d-desktop:block;--_gs-mw-desktop:calc(100% - 0 - 0);--_gs-gap-tablet:0px 0px;--_gs-m-tablet:0 0 0 0;--_gs-p-tablet:0 0 0 0;--_gs-bg-tablet:transparent none;--_gs-bg-xp-tablet:0px;--_gs-zi-tablet:auto;--_gs-d-tablet:block;--_gs-mw-tablet:calc(100% - 0 - 0);--_gs-gap-mobile:0px 0px;--_gs-m-mobile:0 0 0 0;--_gs-p-mobile:0 0 0 0;--_gs-bg-mobile:transparent none;--_gs-bg-xp-mobile:0px;--_gs-zi-mobile:auto;--_gs-d-mobile:block;--_gs-mw-mobile:calc(100% - 0 - 0);--_gs-columns:2;--_gs-rows:1\"><div class=\"grids-s-w_i\">\n<div class=\"grids-area\" style=\"--_ga-column:1\/2;--_ga-row:1\/2;--_ga-m-desktop:0 0 0 0;--_ga-p-desktop:0 0 0 0;--_ga-bg-desktop:transparent none;--_ga-zi-desktop:auto;--_ga-d-desktop:flex;--_ga-mw-desktop:calc(100% - 0 - 0);--_ga-m-tablet:0 0 0 0;--_ga-p-tablet:0 0 0 0;--_ga-bg-tablet:transparent none;--_ga-zi-tablet:auto;--_ga-d-tablet:flex;--_ga-mw-tablet:calc(100% - 0 - 0);--_ga-m-mobile:0 0 0 0;--_ga-p-mobile:0 0 0 0;--_ga-bg-mobile:transparent none;--_ga-zi-mobile:auto;--_ga-d-mobile:flex;--_ga-mw-mobile:calc(100% - 0 - 0)\">\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><a href=\"https:\/\/www.ardpylab.fr\/?page_id=1959\"><img loading=\"lazy\" decoding=\"async\" width=\"50\" height=\"28\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/03\/moins.jpg\" alt=\"\" class=\"wp-image-107\"\/><\/a><\/figure><\/div>\n\n<\/div>\n\n<div class=\"grids-area\" style=\"--_ga-column:2\/3;--_ga-row:1\/2;--_ga-m-desktop:0 0 0 0;--_ga-p-desktop:0 0 0 0;--_ga-bg-desktop:transparent none;--_ga-zi-desktop:auto;--_ga-d-desktop:flex;--_ga-mw-desktop:calc(100% - 0 - 0);--_ga-m-tablet:0 0 0 0;--_ga-p-tablet:0 0 0 0;--_ga-bg-tablet:transparent none;--_ga-zi-tablet:auto;--_ga-d-tablet:flex;--_ga-mw-tablet:calc(100% - 0 - 0);--_ga-m-mobile:0 0 0 0;--_ga-p-mobile:0 0 0 0;--_ga-bg-mobile:transparent none;--_ga-zi-mobile:auto;--_ga-d-mobile:flex;--_ga-mw-mobile:calc(100% - 0 - 0)\">\n\n<div class=\"wp-block-image\"><figure class=\"alignright size-large\"><a href=\"https:\/\/www.ardpylab.fr\/?page_id=2024\"><img loading=\"lazy\" decoding=\"async\" width=\"50\" height=\"28\" src=\"https:\/\/www.ardpylab.fr\/wp-content\/uploads\/2020\/03\/plus.jpg\" alt=\"\" class=\"wp-image-106\"\/><\/a><\/figure><\/div>\n\n<\/div>\n<\/div><\/div>\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &#8211; Condition if&nbsp; L&rsquo;instruction if (\u00ab\u00a0si\u00a0\u00bb en fran\u00e7ais), utilis\u00e9e avec un op\u00e9rateur logique de comparaison, permet de tester si une condition est vraie. Le format d&rsquo;un test if est le suivant : if&nbsp; uneVariable &gt; 50&nbsp;: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Instructions&nbsp; (attention aux tabulations !) Dans cet exemple, le programme va tester si la variable uneVariable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1998","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=\/wp\/v2\/pages\/1998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1998"}],"version-history":[{"count":0,"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=\/wp\/v2\/pages\/1998\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}