{"id":1563,"date":"2020-03-31T08:51:16","date_gmt":"2020-03-31T06:51:16","guid":{"rendered":"http:\/\/www.ardpylab.fr\/?page_id=1563"},"modified":"2020-05-09T17:49:23","modified_gmt":"2020-05-09T15:49:23","slug":"structure-du-programme","status":"publish","type":"page","link":"https:\/\/www.ardpylab.fr\/?page_id=1563","title":{"rendered":"Structure du programme"},"content":{"rendered":"\n<p>&nbsp;<\/p>\n<p>Dans les fonctions de bases <strong>\u201d<\/strong><strong>setup()\u201d <\/strong>et <strong>\u201dloop()\u201d <\/strong>obligatoires pour tous les programmes ou dans toute autre fonction, on utilisera les structures de langage suivantes :<\/p>\n\n\n\n<h4 style=\"padding-left: 40px;\"><strong><a id=\"structure1\"><\/a><br><br>1. <u>Syntaxe de base<br><br><\/u><\/strong><\/h4>\n<h4><span style=\"color: #000000;\"><strong>. Le point-virgule&nbsp;;<\/strong><\/span><\/h4>\n<p>Il est obligatoire \u00e0 la fin de chaque instruction. Pour le compilateur, les sauts de lignes n&rsquo;ont pas de signification : c&rsquo;est le point-virgule qui marque la fin de ligne.<\/p>\n<p>Oublier le point-virgule en fin de ligne donnera une erreur de compilation.<\/p>\n<p><u>Exemple<\/u> :<\/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\"><span style=\"color: #000000;\">int a = 13;<\/span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: #808080;\">\/\/ le point-virgule indique la fin de l&rsquo;instruction<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><strong><a id=\"structure11\"><\/a><br><span style=\"color: #000000;\">. Les accolades&nbsp;{}<\/span><\/strong><\/h4>\n<p>Les accolades sont un \u00e9l\u00e9ment majeur de la programmation en langage C.<\/p>\n<p>Toute ouverture d&rsquo;une accolade d&rsquo;ouverture \u00ab\u00a0{\u00a0\u00bb doit obligatoirement \u00eatre accompagn\u00e9e dans le code d&rsquo;une accolade de fermeture \u00ab\u00a0}\u00a0\u00bb correspondante. On dit souvent qu&rsquo;il faut que les accolades soient \u00e9quilibr\u00e9es (c\u00e0d autant de\u00a0\u00bb{\u00a0\u00bb que de \u00ab\u00a0}\u00a0\u00bb).<\/p>\n<p>L\u2019Arduino IDE inclut une fonctionnalit\u00e9 pratique pour v\u00e9rifier la correspondance des accolades entre elles. Il suffit de s\u00e9lectionner une accolade, ou m\u00eame de cliquer juste apr\u00e8s une accolade, et l\u2019accolade d\u2019ouverture ou de fermeture associ\u00e9e sera mise en surbrillance.<\/p>\n<p>Les accolades sont utilis\u00e9es dans les fonctions, les boucles et les conditions<\/p>\n<p><u>Exemples<\/u>&nbsp;:<\/p>\n<p>&nbsp;&#8211; Avec des fonctions&nbsp;:<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 15px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 15px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">void myfunction(datatype argument){<\/span>&nbsp; &nbsp; &nbsp; \/<span style=\"color: #808080;\">\/ ouverture de la fonction<\/span><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style=\"color: #808080;\">\/\/ instructions<\/span><br><span style=\"color: #000000;\">}<\/span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/<span style=\"color: #808080;\">\/ fermeture de la fonction<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>&#8211; Dans des boucles&nbsp;:<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 10px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 10px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">while (boolean expression)<\/span><br><span style=\"color: #000000;\">{ <span style=\"color: #808080;\">\/\/ accolade d&rsquo;ouverture du code de la boucle while<\/span> <\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp; \/\/ instructions<\/span><br><span style=\"color: #000000;\">} <span style=\"color: #808080;\">\/\/ fermeture de la boucle<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">do<\/span><br><span style=\"color: #000000;\">{ <span style=\"color: #808080;\">\/\/ accolade d&rsquo;ouverture du code de la boucle do<\/span><\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp; \/\/ instructions<\/span><br><span style=\"color: #000000;\">} while (boolean expression); <span style=\"color: #808080;\">\/\/ fermeture de la boucle<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">for (initialisation; termination condition; incrementing expr)<\/span><br><span style=\"color: #000000;\">{ \/<span style=\"color: #808080;\">\/ ouverture du code de la boucle for<\/span><\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp;\/\/ instructions<\/span><br><span style=\"color: #000000;\">} <span style=\"color: #808080;\">\/\/ fermeture de la boucle<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>&#8211; Dans des conditions&nbsp;:<\/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 (boolean expression)<\/span><br><span style=\"color: #000000;\">{ <span style=\"color: #808080;\">\/\/ ouverture du code de la condition if<\/span><\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp;\/\/ instructions<\/span><br><span style=\"color: #000000;\">} <span style=\"color: #808080;\">\/\/ fermeture du code de la condition if<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">else if (boolean expression)<\/span><br><span style=\"color: #000000;\">{ <span style=\"color: #808080;\">\/\/ ouverture du code de l&rsquo;instruction else if<\/span><\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp;\/\/ instructions<\/span><br><span style=\"color: #000000;\">} <span style=\"color: #808080;\">\/\/ fermeture du code de l&rsquo;instruction else if<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">else<\/span><br><span style=\"color: #000000;\">{<span style=\"color: #808080;\"> \/\/ ouverture du code de l&rsquo;instruction else<\/span><\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp;\/\/ instructions<\/span><br><span style=\"color: #000000;\">} <span style=\"color: #808080;\">\/\/ fermeture du code de l&rsquo;instruction else<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><strong><a id=\"structure12\"><\/a><br><span style=\"color: #000000;\">. \/\/ Les commentaires<\/span><\/strong><\/h4>\n<p>Lorsqu&rsquo;on \u00e9crit dans le programme derri\u00e8re un double slash <strong>\/\/<\/strong>, c&rsquo;est pour donner des informations sur le programme. Les commentaires n&rsquo;ont aucune action sur le programme.<\/p>\n<p>On peut \u00e9crire un commentaire sur plusieurs lignes entre <strong>\/* et *\/ .<\/strong><\/p>\n<p>Exemple&nbsp;: \/*commentaire sur plusieurs lignes, commentaire sur plusieurs lignes, commentaire sur plusieurs lignes *\/<\/p>\n\n\n\n<h4 style=\"padding-left: 40px;\"><strong><a id=\"structure2\"><\/a><br><br><span style=\"color: #333333;\">2. <\/span><u><span style=\"color: #333333;\">Les op\u00e9rateurs arithm\u00e9tiques<\/span><br><br><\/u><\/strong><\/h4>\n<h4><span style=\"color: #000000;\"><strong>. op\u00e9rateur d&rsquo;assignement (signe \u00e9gal unique) =<\/strong><\/span><\/h4>\n<p>Cet op\u00e9rateur stocke la valeur \u00e0 droite du signe \u00e9gal dans la variable \u00e0 gauche du signe \u00e9gal.<\/p>\n<p>Le signe \u00e9gal dans le langage de programmation C est appel\u00e9 op\u00e9rateur d&rsquo;assignement. Il a un sens diff\u00e9rent de celui qu&rsquo;il a en alg\u00e8bre o\u00f9 il indique une \u00e9quivalence ou une \u00e9galit\u00e9. L&rsquo;op\u00e9rateur d&rsquo;assignement indique au microcontr\u00f4leur d&rsquo;\u00e9valuer la valeur ou l&rsquo;expression qui se trouve \u00e0 droite du signe \u00e9gal et de la stocker dans la variable \u00e0 gauche du signe \u00e9gal&nbsp;:<\/p>\n<p style=\"padding-left: 40px;\"><strong>variable = valeur ;<\/strong><\/p>\n<p><u>Exemple<\/u>&nbsp;:<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 24px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 24px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">int sensVal;<\/span>&nbsp; &nbsp;<span style=\"color: #808080;\">\/\/ d\u00e9clare une variable enti\u00e8re de 16 bits nomm\u00e9e senVal<\/span><\/p>\n<p><span style=\"color: #000000;\">senVal = analogRead(0)<\/span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #808080;\">\/\/ m\u00e9morise la valeur de la conversion analogique num\u00e9rique dans la variable &nbsp;<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><strong><a id=\"structure21\"><\/a><br><span style=\"color: #000000;\">. Addition + , Soustraction &#8211; , Multiplication * , et Division \/<\/span><\/strong><\/h4>\n<p>Ces op\u00e9rateurs renvoient respectivement la somme, la diff\u00e9rence, le produit ou le quotient entre deux op\u00e9randes (= entre deux termes).<\/p>\n<p>Cette op\u00e9ration est r\u00e9alis\u00e9e en utilisant le type des donn\u00e9es des op\u00e9randes. Ainsi par exemple, 9 \/ 4 donne 2 d\u00e8s lors que 9 et 4 sont de type int.<\/p>\n<p>Si les op\u00e9randes sont de deux types de donn\u00e9es diff\u00e9rents, le plus \u00ab\u00a0grand\u00a0\u00bb est utilis\u00e9 pour le calcul.<\/p>\n<p>Si un des nombres (op\u00e9randes) est du type float ou double, le calcul dit \u00ab\u00a0en virgule flottante\u00a0\u00bb est utilis\u00e9 pour le calcul (c\u00e0d que 9\/4 donne 2.25).<\/p>\n<p>&#8211; Syntaxe :<\/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;\">result = value1 + value2;<\/span><br><span style=\"color: #000000;\">result = value1 &#8211; value2;<\/span><br><span style=\"color: #000000;\">result = value1 * value2;<\/span><br><span style=\"color: #000000;\">result = value1 \/ value2;<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>&#8211; Exemple&nbsp;:<\/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;\">y = y + 3;<\/span><br><span style=\"color: #000000;\">x = x &#8211; 7;<\/span><br><span style=\"color: #000000;\">i = j * 6;<\/span><br><span style=\"color: #000000;\">r = r \/ 5;<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4 style=\"padding-left: 40px;\"><strong><a id=\"structure3\"><\/a><br><br><span style=\"color: #333333;\">1.3. <\/span><span style=\"text-decoration: underline;\"><span style=\"color: #333333; text-decoration: underline;\">Les op\u00e9rateurs de comparaison<\/span><br><br><\/span><\/strong><\/h4>\n<h4><strong><span style=\"color: #000000;\">==, !=, &lt;, &gt;<\/span> <\/strong><\/h4>\n<p><span style=\"color: #333333;\">sont des op\u00e9rateurs logiques de comparaison :<\/span><\/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;\">\u2022 x == y (x est \u00e9gal \u00e0 y)<\/span><br><span style=\"color: #000000;\">\u2022 x != y (x est diff\u00e9rent de y)<\/span><br><span style=\"color: #000000;\">\u2022 x &lt; y (x est inf\u00e9rieur \u00e0 y)<\/span><br><span style=\"color: #000000;\">\u2022 x &gt; y (x est sup\u00e9rieur \u00e0 y)<\/span><br><span style=\"color: #000000;\">\u2022 x &lt;= y (x est inf\u00e9rieur ou \u00e9gal \u00e0 y)<\/span><br><span style=\"color: #000000;\">\u2022 x &gt;= y (x est sup\u00e9rieur ou \u00e9gal \u00e0 y)<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4 style=\"padding-left: 40px;\"><strong><a id=\"structure4\"><\/a><br><br>1.4. <span style=\"text-decoration: underline;\">Les op\u00e9rateurs bool\u00e9ens<br><br><\/span><\/strong><\/h4>\n<p>Ces op\u00e9rateurs peuvent \u00eatre utilis\u00e9s \u00e0 l&rsquo;int\u00e9rieur de la condition d&rsquo;une instruction if pour associer plusieurs conditions (ou op\u00e9randes) \u00e0 tester.<br><br><\/p>\n<h4><span style=\"color: #000000;\"><strong>. &amp;&amp; (ET LOGIQUE)<\/strong><\/span><\/h4>\n<p>VRAI seulement si les deux op\u00e9randes sont VRAI, par exemple :<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 10px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 10px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">if (digitalRead(2) == HIGH &amp;&amp; digitalRead(3) == HIGH) { <span style=\"color: #808080;\">\/\/ lit l&rsquo;\u00e9tat de 2 entr\u00e9es<\/span><\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Instructions<\/span><br><span style=\"color: #000000;\">}<\/span><br><span style=\"color: #808080;\">\/\/ est VRAI seulement si les deux entr\u00e9es sont \u00e0 l&rsquo;\u00e9tat HAUT simultan\u00e9ment.<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><a id=\"structure41\"><\/a><br><span style=\"color: #000000;\"><strong>. || ( OU LOGIQUE)<\/strong><\/span><\/h4>\n<p>VRAI si l&rsquo;un des deux op\u00e9randes est VRAI, par exemple :<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 10px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 10px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">if (x &gt; 0 || y &gt; 0) { \/<span style=\"color: #808080;\">\/ si x sup\u00e9rieur \u00e0 0 ou si y sup\u00e9rieur \u00e0 0<\/span><\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp; \/\/ Instructions<\/span><br><span style=\"color: #000000;\">}<\/span><br><span style=\"color: #000000;\">\/<span style=\"color: #808080;\">\/ est VRAI si soit x, soit y est sup\u00e9rieur \u00e0 0.<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><strong><a id=\"structure42\"><\/a><br>.<span style=\"color: #000000;\"> ! (NON LOGIQUE)<\/span><\/strong><\/h4>\n<p>VRAI si l&rsquo;op\u00e9rande est FAUX, par exemple :<\/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 (!x) {<\/span><br><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp;\/\/ Instructions<\/span><br><span style=\"color: #000000;\">}<\/span><br><span style=\"color: #000000;\">\/<span style=\"color: #808080;\">\/ VRAI si la variable x est FAUSSE (c\u00e0d si x = 0)<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4>&nbsp;<\/h4>\n<h4 style=\"padding-left: 40px;\"><strong>1.5. <u>Les op\u00e9rateurs compos\u00e9s<br><a id=\"structure5\"><\/a><br><\/u><\/strong><\/h4>\n<h4><span style=\"color: #000000;\"><strong>. ++ (incr\u00e9ment) \/ &#8212; (d\u00e9cr\u00e9ment)<\/strong><\/span><\/h4>\n<p>Ces op\u00e9rateurs incr\u00e9mentent ou d\u00e9cr\u00e9mentent une variable enti\u00e8re de type int ou long (pouvant \u00eatre unsigned).<\/p>\n<p>&#8211; <u>Syntaxe<\/u>&nbsp;:<\/p>\n\n\n\n<figure class=\"wp-block-table\">\n<table style=\"width: 100%; height: 20px;\">\n<tbody>\n<tr style=\"height: 21px;\">\n<td style=\"height: 20px; padding-left: 40px;\" bgcolor=\"#FFFFCC\">\n<p><span style=\"color: #000000;\">x++; <span style=\"color: #808080;\">\/\/ incr\u00e9mente x de un, sans modifier x<\/span><\/span><br><span style=\"color: #000000;\">++x; <span style=\"color: #808080;\">\/\/ incr\u00e9mente x de un et modifie x<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">x&#8211; ; <span style=\"color: #808080;\">\/\/ d\u00e9cr\u00e9ment x de un, sans modifier x<\/span><\/span><br><span style=\"color: #000000;\">&#8211;x ; <span style=\"color: #808080;\">\/\/ d\u00e9cr\u00e9mente x de un, et modifie x<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>&#8211; <u>Exemples<\/u>&nbsp;:<\/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;\">x = 2; <span style=\"color: #808080;\">\/\/ variable x = 2<\/span><\/span><br><span style=\"color: #000000;\">y = ++x; \/<span style=\"color: #808080;\">\/ x contient 3 et y contient 3<\/span><\/span><br><span style=\"color: #000000;\">y = x++; <span style=\"color: #808080;\">\/\/ x contient toujours 2, y contient 3<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><strong><a id=\"structure51\"><\/a><br><span style=\"color: #000000;\">. += , -= , *= , \/=<\/span><\/strong><\/h4>\n<p>Ces op\u00e9rateurs r\u00e9alisent une op\u00e9ration math\u00e9matique entre une variable <strong>x<\/strong> et une autre variable ou une constante <strong>y<\/strong>. Les op\u00e9rateurs <strong>+<\/strong>,<strong>&#8211;<\/strong> ,<strong>*<\/strong> et <strong>\/<\/strong> sont juste utiles pour raccourcir la forme compl\u00e8te des op\u00e9rations math\u00e9matiques list\u00e9es ci-dessous&nbsp;:<\/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;\">x += y; <span style=\"color: #808080;\">\/\/ \u00e9quivaut \u00e0 l&rsquo;expression x = x+y<\/span><\/span><br><span style=\"color: #000000;\">x -= y; <span style=\"color: #808080;\">\/\/ \u00e9quivaut \u00e0 l&rsquo;expression x = x- y<\/span><\/span><br><span style=\"color: #000000;\">x *= y; <span style=\"color: #808080;\">\/\/ \u00e9quivaut \u00e0 l&rsquo;expression x = x * y<\/span><\/span><br><span style=\"color: #000000;\">x \/= y; <span style=\"color: #808080;\">\/\/ \u00e9quivaut \u00e0 l&rsquo;expression x= x \/ y<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>&#8211; <u>Exemples<\/u>&nbsp;:<\/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;\">x = 2;<\/span><br><span style=\"color: #000000;\">x += 4; <span style=\"color: #808080;\">\/\/ x contient 6<\/span><\/span><br><span style=\"color: #000000;\">x -= 3; <span style=\"color: #808080;\">\/\/ x contient 3<\/span><\/span><br><span style=\"color: #000000;\">x *= 10; <span style=\"color: #808080;\">\/\/ x contient 30<\/span><\/span><br><span style=\"color: #000000;\">x \/= 2; <span style=\"color: #808080;\">\/\/ x contient 15<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4>&nbsp;<\/h4>\n<h4 style=\"padding-left: 40px;\"><strong><span style=\"color: #333333;\">1.6. <\/span><span style=\"text-decoration: underline;\"><span style=\"color: #333333; text-decoration: underline;\">Structures de contr\u00f4le<\/span><br><a id=\"structure6\"><\/a><br><\/span><\/strong><\/h4>\n<h4><span style=\"color: #000000;\"><strong>. if (condition)<\/strong><\/span><\/h4>\n<p>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, par exemple si la mesure d&rsquo;une entr\u00e9e analogique est bien sup\u00e9rieure \u00e0 une certaine valeur.<br><br>Le format d&rsquo;un test if 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 (uneVariable &gt; 50) <\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #808080;\">\/\/ Action<\/span><br><span style=\"color: #000000;\">}<\/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.<\/p>\n<p>Autrement dit, si l&rsquo;\u00e9tat du test entre les parenth\u00e8ses est vrai, les instructions comprises entre les accolades sont ex\u00e9cut\u00e9es. Sinon, le programme se poursuit sans ex\u00e9cuter ces instructions.<\/p>\n\n\n\n<p>Les accolades peuvent \u00eatre omises apr\u00e8s une instruction if. Dans ce cas, la suite de la ligne (qui se termine par un point-virgule) devient la seule instruction de la condition.<\/p>\n<p>Tous les exemples suivants sont corrects :<\/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 (x &gt; 120) digitalWrite(LEDpin, HIGH);<\/span><\/p>\n<p><span style=\"color: #000000;\">if (x &gt; 120)<\/span><br><span style=\"color: #000000;\">digitalWrite(LEDpin, HIGH);<\/span><\/p>\n<p><span style=\"color: #000000;\">if (x &gt; 120){ digitalWrite(LEDpin, HIGH); }<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><strong><a id=\"structure61\"><\/a><br><span style=\"color: #000000;\">. if \/ else<\/span><\/strong><\/h4>\n<p>L&rsquo;instruction if\/else (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<p>Par exemple, une entr\u00e9e analogique peut-\u00eatre test\u00e9e et une action r\u00e9alis\u00e9e si l&rsquo;entr\u00e9e est inf\u00e9rieure \u00e0 500, et une autre action r\u00e9alis\u00e9e si l&rsquo;entr\u00e9e est sup\u00e9rieure ou \u00e9gale \u00e0 500. Le code ressemblera \u00e0 cela :<\/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 (valsensor &lt; 500)<\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #808080;\">\/\/ action A<\/span><br><span style=\"color: #000000;\">}<\/span><br><span style=\"color: #000000;\">else<\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #808080;\">\/\/ action B<\/span><br><span style=\"color: #000000;\">}<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>L\u2019instruction <strong>else<\/strong> peut contenir un autre test <strong>if<\/strong>, et donc des tests multiples et exclusifs peuvent \u00eatre r\u00e9alis\u00e9s en m\u00eame temps.<\/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\/else<\/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>else if<\/strong> peut \u00eatre utilis\u00e9 avec ou sans bloc de conclusion <strong>else.<\/strong><\/p>\n<p>Un nombre illimit\u00e9 de branches <strong>else if<\/strong> est autoris\u00e9.<\/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 (valsensor &lt; 500) <\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #808080;\">\/\/ action A<\/span><br><span style=\"color: #000000;\">}<\/span><br><span style=\"color: #000000;\">else if (valsensor &gt;= 1000)<\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #808080;\">\/\/ action B<\/span><br><span style=\"color: #000000;\">}<\/span><br><span style=\"color: #000000;\">else<\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #808080;\">\/\/ action C<\/span><br><span style=\"color: #000000;\">}<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><strong><a id=\"structure62\"><\/a><br><span style=\"color: #000000;\">. Boucle for<\/span><\/strong><\/h4>\n<p>L&rsquo;instruction <strong>for<\/strong> est utilis\u00e9e pour r\u00e9p\u00e9ter l&rsquo;ex\u00e9cution d&rsquo;un bloc d&rsquo;instructions regroup\u00e9es entre des accolades. <br>Un compteur incr\u00e9mental est habituellement utilis\u00e9 pour incr\u00e9menter et finir la boucle. <br>L&rsquo;instruction <strong>for<\/strong> est tr\u00e8s utile pour toutes les op\u00e9rations r\u00e9p\u00e9titives et est souvent utilis\u00e9es en association avec des tableaux de variables pour agir sur un ensemble de donn\u00e9es ou broches.<\/p>\n<p>Il y a 3 parties dans l&rsquo;ent\u00eate d&rsquo;une boucle for :<\/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;\">for (initialisation; condition; incrementation) {<br><\/span><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp; \/\/instructions<br><\/span><span style=\"color: #000000;\">}<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>L&rsquo;initialisation a lieu en premier et une seule fois. A chaque ex\u00e9cution de la boucle, la condition est test\u00e9e. <br>Si elle est VRAIE, le bloc d&rsquo;instructions et l&rsquo;incr\u00e9mentation sont ex\u00e9cut\u00e9s, puis la condition est test\u00e9e de nouveau. Lorsque la condition devient FAUSSE, la boucle stoppe.<\/p>\n<p>Exemple&nbsp;:<\/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;\">for (int i=0; i &lt;= 255; i++){&nbsp; <span style=\"color: #808080;\">\/\/ boucle incr\u00e9mentant la variable i de 0 \u00e0 255, de 1 en 1<\/span><\/span><\/p>\n<p><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp;\/\/instructions<\/span><\/p>\n<p><span style=\"color: #000000;\">} <span style=\"color: #808080;\">\/\/ fin de la boucle for<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4><strong><a id=\"structure63\"><\/a><br><span style=\"color: #000000;\">. Boucle while<\/span><\/strong><\/h4>\n<p>Les boucles <strong>while<\/strong> (\u00ab\u00a0tant que\u00a0\u00bb en anglais) bouclent sans fin, et ind\u00e9finiment, jusqu&rsquo;\u00e0 ce que la condition ou l&rsquo;expression entre les parenth\u00e8ses ( ) devienne fausse. <br>Quelque chose doit modifier la variable test\u00e9e, sinon la boucle <strong>while<\/strong> ne se terminera jamais. Cela peut \u00eatre dans votre code, soit une variable incr\u00e9ment\u00e9e, ou \u00e9galement une condition externe comme le test d&rsquo;un capteur.<\/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;\">while(expression){ <span style=\"color: #808080;\">\/\/ tant que l&rsquo;expression est vraie<br><\/span><\/span><span style=\"color: #808080;\">&nbsp; &nbsp; &nbsp;\/\/ instructions<br><\/span><span style=\"color: #000000;\">}<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>Avec<strong> expression, <\/strong>une instruction (bool\u00e9enne) qui renvoie un r\u00e9sultat VRAI ou FAUX<\/p>\n<p>Exemple&nbsp;:<\/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;\">var = 0;<\/span><br><span style=\"color: #000000;\">while(var &lt; 200){ <span style=\"color: #808080;\">\/\/ tant que la variable est inf\u00e9rieur \u00e0 200<\/span><\/span><\/p>\n<p><span style=\"color: #808080;\">\/\/ fait quelque chose 200 fois de suite&#8230;<\/span><\/p>\n<p><span style=\"color: #000000;\">var++; <span style=\"color: #808080;\">\/\/ incr\u00e9mente la variable<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">}<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h4>&nbsp;<\/h4>\n<h4><span style=\"color: #000000;\"><strong><a id=\"structure64\"><\/a>. boucle do \u2013 while<\/strong><\/span><\/h4>\n<p>La boucle <strong>do \/ while<\/strong> (\u00ab\u00a0faire tant que\u00a0\u00bb en anglais) fonctionne de la m\u00eame fa\u00e7on que la boucle <strong>while<\/strong>, \u00e0 la diff\u00e9rence pr\u00e8s que la condition est test\u00e9e \u00e0 la fin de la boucle, et par cons\u00e9quent la boucle do sera toujours ex\u00e9cut\u00e9e au moins une fois.<\/p>\n<p>&#8211; <u>Syntaxe<\/u>&nbsp;:<\/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;\">do <span style=\"color: #808080;\">\/\/ faire&#8230;<\/span><\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #000000;\">&nbsp; &nbsp;<span style=\"color: #808080;\">\/\/ instructions<br><\/span><\/span><span style=\"color: #000000;\">} while (condition); <span style=\"color: #808080;\">\/\/ tant que la condition est vraie<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>&#8211; Exemple&nbsp;:<\/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;\">do <span style=\"color: #808080;\">\/\/ faire&#8230;<\/span><\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #000000;\">x = analogRead(A0); \/<span style=\"color: #808080;\">\/ lit la valeur de la tension d\u2019un capteur<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">} while (x &lt; 100); <span style=\"color: #808080;\">\/\/ &#8230;tant que x est inf\u00e9rieur \u00e0 100<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p><u><br>Remarque<\/u>&nbsp;:<\/p>\n<p>L&rsquo;instruction <strong>break<\/strong> est utilis\u00e9e pour sortir d&rsquo;une boucle do, for ou while, en passant outre le d\u00e9roulement normal de la boucle.<\/p>\n<p>Exemple&nbsp;:<\/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;\">for (int i=0; i &lt;= 100; i++){ \/<span style=\"color: #808080;\">\/ boucle incr\u00e9mentant la variable i de 0 \u00e0 100, de 1 en 1<br><\/span><\/span><span style=\"color: #000000;\">x = analogRead(A0); \/<span style=\"color: #808080;\">\/ lit la valeur de la tension d\u2019un capteur<\/span><\/span><br><span style=\"color: #000000;\">if (x &lt; 100) <\/span><br><span style=\"color: #000000;\">{<\/span><br><span style=\"color: #000000;\">break ; \/<span style=\"color: #808080;\">\/ si la mesure est inf\u00e9rieure \u00e0 un seuil, on sort de la boucle<\/span><\/span><br><span style=\"color: #000000;\">}<\/span><br><span style=\"color: #000000;\">}<span style=\"color: #808080;\"> \/\/ fin de la boucle for<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/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=1533\"><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=1610\"><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; Dans les fonctions de bases \u201dsetup()\u201d et \u201dloop()\u201d obligatoires pour tous les programmes ou dans toute autre fonction, on utilisera les structures de langage suivantes : 1. Syntaxe de base . Le point-virgule&nbsp;; Il est obligatoire \u00e0 la fin de chaque instruction. Pour le compilateur, les sauts de lignes n&rsquo;ont pas de signification : [&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-1563","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=\/wp\/v2\/pages\/1563","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=1563"}],"version-history":[{"count":0,"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=\/wp\/v2\/pages\/1563\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ardpylab.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}