{"id":2957,"date":"2024-05-21T07:05:06","date_gmt":"2024-05-21T07:05:06","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=2957"},"modified":"2024-05-21T07:05:07","modified_gmt":"2024-05-21T07:05:07","slug":"program-structures","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/05\/21\/program-structures\/","title":{"rendered":"Program Structures"},"content":{"rendered":"\n<p>Before we study basic building blocks of the Pascal programming language, let us look a bare minimum Pascal program structure so that we can take it as a reference in upcoming chapters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pascal Program Structure<\/h2>\n\n\n\n<p>A Pascal program basically consists of the following parts \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Program name<\/li>\n\n\n\n<li>Uses command<\/li>\n\n\n\n<li>Type declarations<\/li>\n\n\n\n<li>Constant declarations<\/li>\n\n\n\n<li>Variables declarations<\/li>\n\n\n\n<li>Functions declarations<\/li>\n\n\n\n<li>Procedures declarations<\/li>\n\n\n\n<li>Main program block<\/li>\n\n\n\n<li>Statements and Expressions within each block<\/li>\n\n\n\n<li>Comments<\/li>\n<\/ul>\n\n\n\n<p>Every pascal program generally has a heading statement, a declaration and an execution part strictly in that order. Following format shows the basic syntax for a Pascal program \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program {name of the program}\nuses {comma delimited names of libraries you use}\nconst {global constant declaration block}\nvar {global variable declaration block}\n\nfunction {function declarations, if any}\n{ local variables }\nbegin\n...\nend;\n\nprocedure { procedure declarations, if any}\n{ local variables }\nbegin\n...\nend;\n\nbegin { main program block starts}\n...\nend. { the end of main program block }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Pascal Hello World Example<\/h2>\n\n\n\n<p>Following is a simple pascal code that would print the words &#8220;Hello, World!&#8221; \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program HelloWorld;\nuses crt;\n\n(* Here the main program block starts *)\nbegin\n   writeln('Hello, World!');\n   readkey;\nend. <\/code><\/pre>\n\n\n\n<p>This will produce following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, World!\n<\/code><\/pre>\n\n\n\n<p>Let us look various parts of the above program \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first line of the program\u00a0<strong>program HelloWorld;<\/strong>\u00a0indicates the name of the program.<\/li>\n\n\n\n<li>The second line of the program\u00a0<strong>uses crt;<\/strong>\u00a0is a preprocessor command, which tells the compiler to include the crt unit before going to actual compilation.<\/li>\n\n\n\n<li>The next lines enclosed within begin and end statements are the main program block. Every block in Pascal is enclosed within a\u00a0<strong>begin<\/strong>\u00a0statement and an\u00a0<strong>end<\/strong>\u00a0statement. However, the end statement indicating the end of the main program is followed by a full stop (.) instead of semicolon (;).<\/li>\n\n\n\n<li>The\u00a0<strong>begin<\/strong>\u00a0statement of the main program block is where the program execution begins.<\/li>\n\n\n\n<li>The lines within\u00a0<strong>(*&#8230;*)<\/strong>\u00a0will be ignored by the compiler and it has been put to add a\u00a0<strong>comment<\/strong>\u00a0in the program.<\/li>\n\n\n\n<li>The statement\u00a0<strong>writeln(&#8216;Hello, World!&#8217;);<\/strong>\u00a0uses the writeln function available in Pascal which causes the message &#8220;Hello, World!&#8221; to be displayed on the screen.<\/li>\n\n\n\n<li>The statement\u00a0<strong>readkey;<\/strong>\u00a0allows the display to pause until the user presses a key. It is part of the crt unit. A unit is like a library in Pascal.<\/li>\n\n\n\n<li>The last statement\u00a0<strong>end.<\/strong>\u00a0ends your program.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Compile and Execute Pascal Program<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open a text editor and add the above-mentioned code.<\/li>\n\n\n\n<li>Save the file as\u00a0<em>hello.pas<\/em><\/li>\n\n\n\n<li>Open a command prompt and go to the directory, where you saved the file.<\/li>\n\n\n\n<li>Type fpc hello.pas at command prompt and press enter to compile your code.<\/li>\n\n\n\n<li>If there are no errors in your code, the command prompt will take you to the next line and would generate\u00a0<strong>hello<\/strong>\u00a0executable file and\u00a0<strong>hello.o<\/strong>\u00a0object file.<\/li>\n\n\n\n<li>Now, type\u00a0<strong>hello<\/strong>\u00a0at command prompt to execute your program.<\/li>\n\n\n\n<li>You will be able to see &#8220;Hello World&#8221; printed on the screen and program waits till you press any key.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ fpc hello.pas\nFree Pascal Compiler version 2.6.0 &#91;2011\/12\/23] for x86_64\nCopyright (c) 1993-2011 by Florian Klaempfl and others\nTarget OS: Linux for x86-64\nCompiling hello.pas\nLinking hello\n8 lines compiled, 0.1 sec\n\n$ .\/hello\nHello, World!<\/code><\/pre>\n\n\n\n<p>Make sure that free pascal compiler&nbsp;<strong>fpc<\/strong>&nbsp;is in your path and that you are running it in the directory containing source file hello.pas.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before we study basic building blocks of the Pascal programming language, let us look a bare minimum Pascal program structure so that we can take it as a reference in upcoming chapters. Pascal Program Structure A Pascal program basically consists of the following parts \u2212 Every pascal program generally has a heading statement, a declaration [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[226],"tags":[],"class_list":["post-2957","post","type-post","status-publish","format-standard","hentry","category-06-pascal"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=2957"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2957\/revisions"}],"predecessor-version":[{"id":2958,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2957\/revisions\/2958"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=2957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=2957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=2957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}