{"id":23244,"date":"2023-12-17T12:09:18","date_gmt":"2023-12-17T12:09:18","guid":{"rendered":"https:\/\/linuxways.net\/?p=23244"},"modified":"2024-03-25T02:10:28","modified_gmt":"2024-03-25T02:10:28","slug":"loops-c-language-examples","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/loops-c-language-examples\/","title":{"rendered":"Loops in C Language with Examples"},"content":{"rendered":"<p>In any programming language, loops are part of most of the functions and code that the programmer uses. For example, many text input and output functions consist of repeating loops that read or write the character after the character using basic C functions such as getchar() or putchar(). The C language provides three types of loops, each with properties suited to a particular purpose.<\/p>\n<p>In this Linux Ways article, you will learn everything about loops in C. We will show you each loop, its syntax, and explain how they work. Then, we will put them into practice with code and pictures using the \u201cwhile\u201d, \u201cdo-while\u201d, and \u201cfor\u201d statements. We&#8217;ll also show you how to create infinite loops for applications and how to use the escape and iteration forced statements.<\/p>\n<h2><a id=\"post-23244-_20pcko11qmk2\"><\/a>\u201cWhile\u201d Loop in C Language<\/h2>\n<p>The \u201cwhile\u201d loop repeats the code that is enclosed in curly braces while the escape condition returns a true result. The escape condition is determined by the result of a relational operation between two or more variables or by a constant and one or more variables and is analyzed at the beginning of each new cycle. When the relational operation returns a result that equals to false, the process is released from the loop. We will see the syntax of the while loop in the following:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>expresion<span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"co1\">\/\/code<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><\/div><\/div>\n<h2>How to Use the \u201cWhile\u201d Loop in C Language<\/h2>\n<p>In this example, we declare the integer \u201ca\u201d and assign it with the value of 0. This variable has two uses: it is the escape condition and the cycle counter.<\/p>\n<p>Then, we open a \u201cwhile\u201d loop in which the escape condition is that \u201ca\u201d equals 10. The following expression shows the relational operation which is the escape condition for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">!=<\/span> <span class=\"nu0\">10<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>Inside the loop, we use the printf() function to output the \u201cCycles elapsed\u201d message, followed by the value of \u201ca\u201d. Using the sleep() function, we set a delay of 1 second and then increment the variable \u201ca\u201d by 1. This cycle is repeated until the variable \u201ca\u201d reaches the value of 10. Lets\u2019 see the following code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">!=<\/span><span class=\"nu0\">10<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Elapsed cycles %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp;sleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp;a<span class=\"sy0\">++;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Lets\u2019 see the compilation and execution of this code with the messages issued in each cycle:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23252\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-1.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-1-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-1-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23244-_g81tx4249mtz\"><\/a>\u201cDo-While\u201d Loops in C Language<\/h2>\n<p>In certain cases where we use a \u201cwhile\u201d loop, the escape condition may occur before the program reaches it, preventing an entry into and the execution of the code within. However, there are cases where it is necessary for the code in a loop to execute at least once.<\/p>\n<p>The \u201cdo-while\u201d loop analyzes the escape condition at the end of each loop, while the \u201cwhile\u201d loop does so at the beginning. This guarantees that the program will execute the contained code at least once, whether the escape condition is given or not. Let\u2019s see the following syntax of the \u201cdo-while\u201d statement:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"kw1\">do<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/code<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>expresi\u00f3n<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<h2>How to Use the \u201cDo-While\u201d Loop in the C Language<\/h2>\n<p>In this example, we&#8217;ll show you what actions do the \u201cwhile\u201d and \u201cdo-while\u201d loops perform when the escape condition is specified before entering the loop, so we can compare their effects.<\/p>\n<p>To do this, we create the variable \u201ca\u201d, assign it with the value of 0, and set the relational operation of \u201ca!=0\u201d as the escape condition. Then, we create a \u201cwhile\u201d loop and a \u201cdo-while\u201d loop where we use the printf() function to print a message indicating which loop the program is in. Let\u2019s see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">!=<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Inside the while loop<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">do<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Inside the do-while loop<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">!=<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>As we can see in the following image that shows the execution of this code, the given escape condition prevented the program from entering the \u201cwhile\u201d loop, while it did so in the \u201cdo-while\u201d statement and executed the code in it once:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23256\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-2.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-2-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-2-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23244-_t0lhnhm43tzw\"><\/a>\u201cFor\u201d Loop in the C Language<\/h2>\n<p>The <em>\u201c<\/em>for\u201d loop works similarly to \u201cwhile\u201d loop in which its escape condition is determined by the false result of a relational operation and the iteration occurs at the beginning of the loop. Their main feature is that you can declare and initialize a variable in the same statement, perform the relational escape operation, and then operate on the same variable. This makes it very useful when we need to implement a simple loop that does not operate on already declared variables such as timeouts, counters, etc. Let\u2019s see the following syntax for this loop:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span>variable<span class=\"sy0\">;<\/span> relational operation<span class=\"sy0\">;<\/span> variable operation<span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/code<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The first field of this statement can take an already declared variable or declare a variable of any type and assign it with a value. The second field is the relational operation that determines the escape, and the third field performs an operation on the variable. The operation that the \u201cfor\u201d loop performs in the third field can be incremental, addition, subtraction, or any other type that is accepted in C. You can also call a function from there to operate on the variable.<\/p>\n<h2><a id=\"post-23244-_o9h6y2ckpojk\"><\/a>How to Use the \u201cFor\u201d Loop in the C Language<\/h2>\n<p>In this example, we will show you how to use the \u201cfor\u201d loop. The characteristics of the loop that we will create are the same as the example that we saw in the \u201cwhile\u201d statement in which it repeats the code 10 times. Let\u2019s see the following code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span> a<span class=\"sy0\">!=<\/span><span class=\"nu0\">10<\/span><span class=\"sy0\">;<\/span> a<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Elapsed for cycles %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; sleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>As we can see in the code, the function that is executed by the loop is the same as the \u201cwhile\u201d example, except that this statement allows us to create a more organized and reduced code. Let\u2019s see the following image that shows the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23260\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-3.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-3-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-3-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>The \u201cfor\u201d statement also takes a variable and a previously declared value in the first field:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu0\">5<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span> a<span class=\"sy0\">;<\/span> a<span class=\"sy0\">!=<\/span><span class=\"nu0\">10<\/span><span class=\"sy0\">;<\/span> a<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Elapsed for cycles %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp;sleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>As we can see in the figure, the \u201cfor\u201d statement took a variable and an already declared value and thus behaved similarly to the \u201cwhile\u201d statement.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23262\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-4.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-4-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-4-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23244-_u0fddfrb5wa1\"><\/a>Nested Loops in the C Language<\/h2>\n<p>In the C language, the \u201cwhile\u201d, \u201cdo-while\u201d, and \u201cfor\u201d statements can be nested within each other. Let\u2019s see an example with the nesting of two \u201cwhile\u201d statements with three cycles each. The result is three cycles of the main statement and nine of the nested statement.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;height:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> b<span class=\"sy0\">;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">!=<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Elapsed main cycles %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; sleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; a<span class=\"sy0\">++;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;b <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>b <span class=\"sy0\">!=<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Elapsed nested cycles %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b<span class=\"sy0\">++;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Let\u2019s see the following image with the execution of this nested loop code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23263\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-5.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-5-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-5-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23244-_h4f3o6gmtuoz\"><\/a>Infinite Loops in the C Language<\/h2>\n<p>Most of the user applications that we use on a daily basis consist of code that is wrapped in an infinite loop. This type of loop is usually executed with the \u201cwhile\u201d statement which uses an operation in the escape expression that always results in true such as (1==1) or simply while(1).<\/p>\n<p>In this way, an infinite loop is created in which the program is not released until the user closes the application with the \u201cCtrl+c\u201d shortcut key or via the graphical interface.<\/p>\n<h2><a id=\"post-23244-_qozt6oe4sli\"><\/a>How to Use Infinite Loops to Create Applications in the C Language<\/h2>\n<p>In this example, we will create a simple console application that calculates the sine and cosine of a variable. This application will be enclosed in an infinite loop while(1) and will perform its calculations by having the user input the operation and the variable to be processed via the scanf() function, perform the calculation, and return the result to the screen with printf() function. This cycle repeats infinitely until the user exits the program.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;math.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">float<\/span> b <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Enter the operation you want to perform<span class=\"es1\">\\n<\/span>sine&nbsp; [1]<span class=\"es1\">\\n<\/span>cosine &nbsp;[2]<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/scanf.html\"><span class=\"kw3\">scanf<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%i&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"sy0\">&amp;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Enter the angle in radians<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/scanf.html\"><span class=\"kw3\">scanf<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%i&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"sy0\">&amp;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">==<\/span> <span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;The sine is: %f<span class=\"es1\">\\n<\/span><span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/sin.html\"><span class=\"kw3\">sin<\/span><\/a><span class=\"br0\">&#40;<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">else<\/span> <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">==<\/span> <span class=\"nu0\">2<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;The cosine is: %f<span class=\"es1\">\\n<\/span><span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/cos.html\"><span class=\"kw3\">cos<\/span><\/a><span class=\"br0\">&#40;<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Let\u2019s see the compilation of this code which calls the mathematical library (-lm) and the execution of this application to calculate the sine and cosine, wrapped in an infinite loop:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23264\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-6.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-6-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-6-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23244-_1pd72hmtzim\"><\/a>Escape Sentence Break in C Conditional Loops<\/h2>\n<p>When we use a loop in programming, there are cases where we need to exit it even though the escape condition has not yet been met. This can happen for various reasons such as we no longer need the task that is performed by the loop or we have already obtained the desired result. It may also happen that the escape condition cannot occur for some reason, so the program continues to run indefinitely in the loop.<\/p>\n<p>For these cases, the C language provides the break statement which forces the exit of the loop and can be supplemented with an \u201cif\u201d condition to generate an escape.<\/p>\n<h2><a id=\"post-23244-_g0bp0uk3aleo\"><\/a>How to Use the Break Statement to Force Exit from a Loop in the C Language<\/h2>\n<p>In this example, we will see how to use the break statement to force exit from a loop. To do this, we use the code from the previous example and add an \u201cif\u201d condition after the last line of the code inside the loop.<\/p>\n<p>The input condition in this <em>\u201c<\/em>if\u201d condition is also the variable \u201ca\u201d that performs a relational equality operation with a value of 12. In this \u201cif\u201d condition, we open the curly braces and print the &#8220;Forced exit&#8221; message in it using the printf() function. Then, we execute the break statement and close the curly brace. Let\u2019s see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">!=<\/span><span class=\"nu0\">10<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Elapsed cycles %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; sleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; a<span class=\"sy0\">++;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">==<\/span> <span class=\"nu0\">12<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Forced exit<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw2\">break<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image shows the execution of this code. As we can see, the forced exit condition was not given because its condition is a=12, while the loop exit condition is a=10:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23265\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-7.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-7-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-7-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Now, let&#8217;s see what happens when we replace the value of 12 with 6 in the relational operation of the \u201cif\u201d condition:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23266\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-8.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-8-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-8-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we can see in the figure, the forced exit condition occurred before the escape condition of the \u201cwhile\u201d loop.<\/p>\n<h2><a id=\"post-23244-_1rk5nn427jel\"><\/a>Continue Statement in Loops of the C Language<\/h2>\n<p>The \u201ccontinue\u201d statement causes the loop code to iterate from the point at which it is called. When the program encounters this statement, it jumps to the beginning of the loop, analyzes the escape condition, and executes the code again. Let\u2019s see an example where a \u201cwhile\u201d loop iterates in cases where \u201ca\u201d is less than 3:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"kw1\">while<\/span> <span class=\"br0\">&#40;<\/span>a <span class=\"sy0\">!=<\/span><span class=\"nu0\">10<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;a<span class=\"sy0\">++;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;sleep<span class=\"br0\">&#40;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>a<span class=\"sy0\">&lt;=<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;continue statement: %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">continue<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Elapsed cycle: %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image shows the execution of this code. As we can see, the program jumps to the beginning of the loop when it finds the \u201ccontinue\u201d statement:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23267\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-9.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-9-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23244-9-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23244-_ls84i3epsetf\"><\/a>Conclusion<\/h2>\n<p>In this complete Linux Ways article, we showed you each of the three conditional loops that the C language provides. We&#8217;ve seen a description of each of these statements, showing their syntax, how they work, their properties, and a usage example. We also included two sections that explains how to use the optional forced escape and iteration statements so that you have a complete mastery in the use of these sentences.<\/p>","protected":false},"excerpt":{"rendered":"<p>Tutorial about loops in C, its syntax, how they work, how to create infinite loops for applications, and how to use the escape and iteration forced statements.<\/p>","protected":false},"author":110,"featured_media":23300,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-23244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=23244"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23244\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/23300"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=23244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=23244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=23244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}