{"id":5724,"date":"2023-12-02T08:56:28","date_gmt":"2023-12-02T07:56:28","guid":{"rendered":"https:\/\/msxitalia.org\/?p=5724"},"modified":"2023-12-02T08:57:50","modified_gmt":"2023-12-02T07:57:50","slug":"turbo-pascal-msx-lezione-5","status":"publish","type":"post","link":"https:\/\/msxitalia.org\/en\/turbo-pascal-msx-lezione-5\/","title":{"rendered":"Turbo Pascal MSX \u2013 Lesson 5"},"content":{"rendered":"<h1><img decoding=\"async\" data-src=\"https:\/\/msxitalia.org\/wp-content\/uploads\/2023\/12\/copertina-300x249.jpg\" class=\"aligncenter wp-image-5727 lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" alt=\"\" width=\"142\" height=\"118\" \/><noscript><img decoding=\"async\" class=\"aligncenter wp-image-5727\" src=\"https:\/\/msxitalia.org\/wp-content\/uploads\/2023\/12\/copertina-300x249.jpg\" alt=\"\" width=\"142\" height=\"118\"><\/noscript><\/h1>\n<h1 style=\"text-align: center\">MSX Turbo Pascal Column by Stefano Roperto (part 5)<\/h1>\n<h2>Lesson 5<\/h2>\n<h3>Control loops and instructions<\/h3>\n<p>In turbo Pascal there are the usual iterative structures present in all high-level languages, the FOR DO loop and the REPEAT UNTIL and WHILE DO structures. Let&#039;s look at them in detail and comment on them.<\/p>\n<p><strong>FOR loop:<\/strong><\/p>\n<p>Program For loop;<br \/>\nvar i:integer;<br \/>\nbegin<br \/>\n<strong>FOR<\/strong> i:=0 to 10 <strong><em>DO<\/em><\/strong><br \/>\nwriteln(i);<br \/>\nend;<br \/>\n\u2026.<br \/>\n\u2026.<br \/>\nEnd.<\/p>\n<p>The FOR loop does not have many differences, except in the syntax, compared to the basic MSX and is a loop used when it is necessary to repeat a series of instructions a known number of times<\/p>\n<p><strong>REPEAT cycle<\/strong><\/p>\n<p>Program cycleRepeat;<br \/>\nvar i,j,k:integer;<br \/>\nbegin<br \/>\nj:=100;<br \/>\nk:=0<br \/>\n<strong><em>REPEAT<\/em><\/strong><br \/>\ni:=i+k*2;<br \/>\nk:=k+1;<br \/>\nwriteln(i);<br \/>\n<strong><em>UNTIL<\/em><\/strong> (k=j);<br \/>\nend;<br \/>\n\u2026<br \/>\n\u2026<br \/>\nEnd;<\/p>\n<p>The REPEAT loop is an iterative structure that allows a series of instructions to be executed until (UNTIL) a certain condition occurs, so it is not known a priori how many times the instructions will be executed.<\/p>\n<p><strong>WHILE loop<\/strong><\/p>\n<p>Program loopWhile;<br \/>\nvar i,j,k:integer;<br \/>\nbegin<br \/>\nj:=100;<br \/>\nk:=0<br \/>\n<strong><em>WHILE <\/em><\/strong>(k=j) C<br \/>\nBegin<br \/>\ni:=i+k*2;<br \/>\nk:=k+1;<br \/>\nwriteln(i);<br \/>\nend;the loop<br \/>\n\u2026<br \/>\n\u2026<br \/>\nEnd.<\/p>\n<p>The WHILE loop is similar to the REPEAT loop, but in this case the loop instructions are executed while (WHILE) the condition is verified and also in this case the number of iterations is not known a priori.<br \/>\nAs you can see, there are apparently no differences between the REPEAT structure and the WHILE structure, but in reality there is a fundamental difference that determines when to use one or the other structure. In the REPEAT loop the statements in the loop are executed at least once, in fact the control statement is at the end of the loop, while in principle the statements in the WHILE loop may never be executed as the control statement is at the beginning of the cycle and, obviously this makes the difference in choosing the structure to use in accordance with your algorithm.<br \/>\nAs regards turbo control instructions, Pascal provides the IF \u2013 THEN \u2013 ELSE instruction and the CASE OF instruction. Let&#039;s also see a variety of examples here:<\/p>\n<p><strong>IF instruction<\/strong><\/p>\n<p>program statementIf;<br \/>\nvar i: integer;<br \/>\nbegin<br \/>\nreadln(i);<br \/>\n<strong><em>IF<\/em><\/strong> (i=1 )<strong><em>THEN<\/em><\/strong><br \/>\ninstructions;<br \/>\n<strong><em>ELSE<\/em><\/strong><br \/>\ninstructions;<br \/>\nend;<br \/>\n\u2026<br \/>\n\u2026<br \/>\nEnd.<\/p>\n<p>Let&#039;s consider another slightly more complex example:<\/p>\n<p><strong>IF statement (version 2)<\/strong><\/p>\n<p>program statementIf;<br \/>\nvar i: integer;<br \/>\nbegin<br \/>\nreadln(i);<br \/>\n<strong><em>IF<\/em><\/strong> (i=1) <strong><em>THEN<\/em><\/strong><br \/>\ninstructions;<br \/>\n<strong><em>IF (<\/em><\/strong>i= 2) <strong><em>THEN<\/em><\/strong><br \/>\ninstructions;<br \/>\n<strong><em>IF (<\/em><\/strong>i= 3) <strong><em>THEN<\/em><\/strong><br \/>\ninstructions;<br \/>\n<strong><em>ELSE<\/em><\/strong><br \/>\nInstructions;<br \/>\nend;<br \/>\n\u2026<br \/>\n\u2026<br \/>\nEnd.<\/p>\n<p>Here we have a complex control which, although correct from a syntactic point of view, is impractical from a practical point of view. To overcome problems like this turbo Pascal provides the CASE OF control statement:<\/p>\n<p><strong>CASE OF statement<\/strong><\/p>\n<p><strong>\u00a0<\/strong>program instructionCase;<br \/>\nvar i: integer;<br \/>\nbegin<br \/>\nreadln(i);<br \/>\n<strong><em>HOMES <\/em><\/strong>the <strong><em>OF<\/em><\/strong><br \/>\n<strong><em>1: <\/em><\/strong>instructions1;<br \/>\n2: begin<br \/>\nInstructions2;<br \/>\ninstructions2;<br \/>\ninstructions2;<br \/>\nend;<br \/>\n3:instructions3;<br \/>\n<strong><em>ELSE <\/em><\/strong>begin<br \/>\nElse_statements;<br \/>\n<strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/em><\/strong><em>end;<\/em><br \/>\nend;<br \/>\n\u2026<br \/>\n\u2026<br \/>\nEnd.<\/p>\n<p>The CASE statement therefore provides some flexibility to control structures and allows you to elegantly resolve situations in which it is necessary to control many different values of the same variable. As we can see, turbo Pascal provides the programmer with complete and efficient iterative and control structures.<\/p>","protected":false},"excerpt":{"rendered":"<p>MSX Turbo Pascal Rubrica a cura di Stefano Roperto (parte 5) Lezione 5 Cicli e istruzioni di controllo In turbo Pascal sono presenti le consuete strutture iterative presenti in tutti i linguaggi di alto livello il ciclo FOR DO e le strutture REPEAT UNTIL e WHILE DO. Vediamoli nel dettaglio e\u00a0 commentiamoli. Ciclo FOR: Program [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[3,5],"tags":[],"dipi_cpt_category":[],"class_list":["post-5724","post","type-post","status-publish","format-standard","hentry","category-programmazione","category-turbo-pascal"],"_links":{"self":[{"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/posts\/5724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/comments?post=5724"}],"version-history":[{"count":4,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/posts\/5724\/revisions"}],"predecessor-version":[{"id":5729,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/posts\/5724\/revisions\/5729"}],"wp:attachment":[{"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/media?parent=5724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/categories?post=5724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/tags?post=5724"},{"taxonomy":"dipi_cpt_category","embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/dipi_cpt_category?post=5724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}