{"id":8683,"date":"2023-08-10T13:16:55","date_gmt":"2023-08-10T11:16:55","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=8683"},"modified":"2025-08-29T14:51:59","modified_gmt":"2025-08-29T12:51:59","slug":"loops","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/","title":{"rendered":"Loops"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#Basics\" >Basics<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#While_loop\" >While loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#For_loop\" >For loop<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#for_loop_declarations_in_C90_vs_C99_standard\" >for loop declarations in C90 vs C99 standard<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#Do_%E2%80%A6_while_loop\" >Do &#8230; while loop<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#Infinite_loops\" >Infinite loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#Nested_loops\" >Nested loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#Loops_with_no_body\" >Loops with no body<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#No_body_as_a_common_coding_mistake\" >No body as a common coding mistake<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/#No_body_as_deliberate_code\" >No body as deliberate code<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"Basics\"><\/span>Basics<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Loops are one of the most basic fundamentals in programming, that allow us to <strong>repeat a certain block of code<\/strong> or simply put, some statements in the code. This repeatable block of code is called the <strong>loop body<\/strong>. In C, we commonly surround the loop body in curly braces <span class=\"lang:c highlight:0 decode:true crayon-inline \">{ }<\/span> .<\/p>\n<p>Loops are executed until the <strong>condition set for the loop holds true<\/strong>. How we form those conditions is crucial to make sure our loops execute as intended, making our programs work as expected.<\/p>\n<p>Loops are generally divided into <strong>entry-controlled and exit-controlled loops<\/strong>. the control type defines <strong>when<\/strong> the loop condition is checked, meaning when the decision is made whether to execute the loop body or not.<\/p>\n<p>Different programming languages have different types of loops available. In C, we have <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span> , <span class=\"lang:c highlight:0 decode:true crayon-inline \">do &#8230; while<\/span>\u00a0 and <span class=\"lang:c highlight:0 decode:true crayon-inline \">for<\/span>\u00a0 loops.<\/p>\n<p>Each type of loop has certain scenarios where they are preferable\u00a0 over others due to convenience and clarity. This <strong>does not<\/strong>\u00a0mean, that the same cannot be achieved with a different type of loop.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"While_loop\"><\/span>While loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This is the most basic and common loop type available. <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 loops are entry-controlled &#8211; the loop body only executes if the loop condition holds true.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_while.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8710\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_while.png\" alt=\"\" width=\"340\" height=\"387\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_while.png 579w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_while-264x300.png 264w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p>The only mandatory component of a <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 loop structure is its condition &#8211; this controls if the loop body will be executed. It is checked <strong>on each iteration <\/strong>before executing the loop body.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">while (condition)\r\n{\r\n    \/\/ loop body\r\n}<\/pre>\n<p><span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 loops are commonly used when the number of repetitions is unknown at the time of writing the code. This includes scenarios where the loop will never execute or will execute infinitely.<\/p>\n<p><strong>Example:<\/strong> display all powers of 3 below the limit value.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true \">\/\/ Set up the calculator\r\nint multiplier = 3;\r\nint product = 1;\r\nint limit = 2500;\r\n\r\nprintf(\"Powers of %d under %d are\\n\", multiplier, limit); \r\n\r\n\/\/ Calculate the powers\r\nwhile (product &lt; limit)\r\n{\r\n    \/\/ print the current power\r\n    printf(\"%d\", product);\r\n\r\n    \/\/ calculate the next power\r\n    product = product * multiplier;\r\n}<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"For_loop\"><\/span>For loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span class=\"lang:c highlight:0 decode:true crayon-inline \">for<\/span>\u00a0 loop is another type of loop that is entry-controlled, thus allowing for the possibility of never executing the loop body. <span class=\"lang:c highlight:0 decode:true crayon-inline \">for<\/span>\u00a0 loops are most often used when when the number of repetitions is known beforehand.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_for.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8715\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_for.png\" alt=\"\" width=\"351\" height=\"407\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_for.png 608w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_for-259x300.png 259w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/a><\/p>\n<p>The structure of the loop has dedicated spots for initialization, condition and expression, separated by semicolons. This allows for clear and convenient use of counters. <strong>Make note of the location<\/strong> where each component of the loop is executed!<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">for (initialization; condition; expression)\r\n{\r\n    \/\/ loop body\r\n}<\/pre>\n<p><strong>Example:<\/strong> The following loop will count from 0 to 9. Note that this is in the style of C90. Check below for difference between C90 and C99 (and later) style of writing.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">int i;\r\n\r\nfor (i = 0; i &lt; 10; i++)\r\n{\r\n    printf(\"i is %d\\n\", i);\r\n}<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"for_loop_declarations_in_C90_vs_C99_standard\"><\/span>for loop declarations in C90 vs C99 standard<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>ISO C90 forbids mixed variable declarations and code &#8211; all variables must be declared at the top of the function before any other code statements.<\/p>\n<p>This forces you to declare loop iterators before the loop<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">void PrintIntArray(int nums[], int n)\r\n{\r\n    int i;\r\n    for (i = 0; i &lt; n; i++)\r\n    {\r\n        printf(\"%d\\n\", nums[i]);\r\n    }\r\n}<\/pre>\n<p>Starting from C99 standard, you are allowed to declare\u00a0 the loop iterator right inside of the loop, making the code look a little cleaner.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">void PrintIntArray(int nums[], int n)\r\n{\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        printf(\"%d\\n\", nums[i]);\r\n    }\r\n}<\/pre>\n<p><strong>Key takeaways<\/strong><\/p>\n<ul>\n<li>For this subject, you can use both C90 and C99 style declarations.<\/li>\n<li>For some hardware, only C90 compilers will be available, forbidding you to declare the variable in the loop. This will cause a mixed variable declarations and code compilation error.<\/li>\n<li>When declaring in the C99 style, the loop iterator will be scoped to the inside of the loop only &#8211; you cannot access the <span class=\"lang:c highlight:0 decode:true crayon-inline \">i<\/span>\u00a0 variable in the above example outside of the loop!<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Do_%E2%80%A6_while_loop\"><\/span>Do &#8230; while loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Do &#8230; while loop is the only loop in C that is exit-controlled, meaning that the body of this loop will always execute at least once, regardless of the condition.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_do_while.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8716\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_do_while.png\" alt=\"\" width=\"349\" height=\"355\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_do_while.png 624w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2023\/08\/loops_do_while-295x300.png 295w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><\/a><\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">do\r\n{\r\n    \/\/ loop body\r\n} \r\nwhile (condition);<\/pre>\n<p>This makes it convenient for tasks that need to be completed, but may need to repeat if some validation fails. An example of this would be user input, that needs to be validated before proceeding (i.e. user needs to enter a positive integer).<\/p>\n<p><strong>Example:<\/strong> The following loop repeats until the user enters <span class=\"lang:c highlight:0 decode:true crayon-inline \">0<\/span> . Notice that while <span class=\"lang:c highlight:0 decode:true crayon-inline \">userInp<\/span>\u00a0 is left uninitialized, it&#8217;s not a problem for the loop. This is because the loop condition is checked after the user needs to enter a value for the variable.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">int userInp;\r\ndo\r\n{\r\n    printf(\"Enter 0 to exit!\\n\");\r\n    scanf(\"%d\", &amp;userInp)\r\n} \r\nwhile (userInp != 0);<\/pre>\n<h1><span class=\"ez-toc-section\" id=\"Infinite_loops\"><\/span>Infinite loops<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Infinite loops are most commonly used when programming for embedded systems &#8211; they need to be repeating the same action infinitely over their lifetime. While again, any loop can be written as an infinite loop, the most clear way to do it is using the <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 loop with a condition of <span class=\"lang:c highlight:0 decode:true crayon-inline \">1<\/span> . Any non-zero value will be evaluated as <span class=\"lang:c highlight:0 decode:true crayon-inline \">true<\/span>\u00a0 in C. Typically you want to avoid using other types of loops when writing infinite loops!<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">\/\/ Infinite loop\r\nwhile (1)\r\n{\r\n    \/\/ loop body\r\n}<\/pre>\n<p>Sometimes you will also see infinite loops that allow for exit under certain scenario. For this, we use a conditional statement to the detect the event and a <span class=\"lang:c highlight:0 decode:true crayon-inline \">break<\/span>\u00a0 statement to stop the execution of the loop.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">\/\/ Infinite loop\r\nwhile (1)\r\n{\r\n    \/\/ loop body\r\n\r\n    \/\/ stop the loop\r\n    if (exit-condition)\r\n    {\r\n        break;\r\n    }\r\n}<\/pre>\n<p>Note, that if the breaking condition is only in the end of the loop body, they can be easily written as <span class=\"lang:c highlight:0 decode:true crayon-inline \">do &#8230; while<\/span>\u00a0 loops &#8211; <strong>this is preferred option in C<\/strong>! An example of this is provided below:<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">\/\/ Infinite loop\r\ndo \r\n{\r\n    \/\/ loop body\r\n} \r\nwhile (!exit-condition);<\/pre>\n<h1><span class=\"ez-toc-section\" id=\"Nested_loops\"><\/span>Nested loops<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Nested loops is the practice of putting one loop inside of another loop. there is no functional limit how many loops can be nested inside of each other, but there are practical limits where the code will become unmaintainable. To avoid this,\u00a0 you should avoid nesting more than two loops. If you require to nest more loops, you should restructure the code into more functions.<\/p>\n<p>There are also no requirements or limitations which types of loops can be mixed, i.e. you can put a <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 loop inside of a <span class=\"lang:c highlight:0 decode:true crayon-inline\">for<\/span>\u00a0 loop.<\/p>\n<p>In this example, we will nest two <span class=\"lang:c highlight:0 decode:true crayon-inline \">for<\/span>\u00a0 loops in the C99 style &#8211; again, not a requirement.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\nint main(void)\r\n{\r\n    int n = 5;\r\n    int m = 7;\r\n    \r\n    for (int i = 0; i &lt; n; i++) \r\n    {\r\n        printf(\"Start of the outer loop iteration i = %d\\n\", i);\r\n        for (int j = 0; j &lt; m; j++) \r\n        {\r\n            printf(\"i = %d; j = %d\\n\", i, j); \r\n        }\r\n        printf(\"End of the outer loop iteration i = %d\\n\\n\", i);\r\n    }\r\n}\r\n<\/pre>\n<p>Note, that the outer loop contains the two print statements and a <span class=\"lang:c highlight:0 decode:true crayon-inline \">for<\/span>\u00a0 loop. Both <span class=\"lang:c highlight:0 decode:true crayon-inline \">printf<\/span>\u00a0 statements and the <span class=\"lang:c highlight:0 decode:true crayon-inline \">for<\/span>\u00a0 loop are executed <span class=\"lang:c highlight:0 decode:true crayon-inline\">n<\/span>\u00a0 times.<\/p>\n<p>The inner loop uses a different counter <span class=\"lang:c highlight:0 decode:true crayon-inline \">j<\/span>\u00a0. The contents of the inner loop is executed <span class=\"lang:c highlight:0 decode:true crayon-inline \">m<\/span>\u00a0 times. This causes the inner loop to be executed a total of <span class=\"lang:c highlight:0 decode:true crayon-inline \">n * m<\/span>\u00a0 times.<\/p>\n<p>Be careful on getting the conditions and counters correct when nesting loops! It&#8217;s very easy to mix up <span class=\"lang:c highlight:0 decode:true crayon-inline \">i<\/span>\u00a0 and <span class=\"lang:c highlight:0 decode:true crayon-inline\">j<\/span> and cause a loop to never execute or run infinitely.<\/p>\n<p>Also note, that while using different counters and sometimes limits is common for many algorithms,\u00a0<strong>it is not a rule!<\/strong> It depends on the algorithm you are implementing (i.e. the desired outcome).<\/p>\n<h1><span class=\"ez-toc-section\" id=\"Loops_with_no_body\"><\/span>Loops with no body<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>In C, a semicolon denotes the end of a statement. A semicolon can be written after the <span class=\"lang:c highlight:0 decode:true crayon-inline\">for<\/span>\u00a0 or <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 loop header line, indicating, that the <strong>loop has no body<\/strong>. This can either be done on purpose, but it is also a very common mistake for beginner programmers.<\/p>\n<p>As an exception, <span class=\"lang:c highlight:0 decode:true crayon-inline \">do &#8230; while<\/span>\u00a0 loops require a semicolon after the loop condition. This section does not apply to <span class=\"lang:c highlight:0 decode:true crayon-inline \">do &#8230; while<\/span>\u00a0 loops.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"No_body_as_a_common_coding_mistake\"><\/span>No body as a common coding mistake<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When writing <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 loops, adding a semicolon after the condition will typically cause an unwanted infinite loop. In the example below, the loop iterator <span class=\"lang:c highlight:0 decode:true crayon-inline \">i<\/span>\u00a0 will infinitely equal to <span class=\"lang:c highlight:0 decode:true crayon-inline \">0<\/span> , as the increment operation <span class=\"lang:c highlight:0 decode:true crayon-inline \">i++<\/span>\u00a0 is not a part of the loop body. While it is in the code block indicated by the curly braces, this block is not the body of the loop, but rather what comes after the loop.<\/p>\n<pre class=\"toolbar:2 show-lang:2 lang:c mark:4 decode:true\">int i = 0;\r\n\r\n\/\/ Mistake here causes an infinite loop\r\nwhile (i &lt; 5);\r\n{\r\n    \/\/ This IS NOT the loop body due to the semicolon!\r\n    printf(\"i = %d\\n\", i);\r\n    i++;\r\n}<\/pre>\n<p>When writing for loops ending in a semicolon, we will similarly create a loop where the following code block is not its body. Most of the time it feels like either the loop is skipped or was only executed once, when in reality, it was executed in full.<\/p>\n<p>In the following example, the loop will execute 5 times, which is as expected. However the only statement in the loop body is the increment <span class=\"lang:c highlight:0 decode:true crayon-inline\">i++<\/span> . Due to this, the loop will terminate as expected, compared to a similarly written <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 loop. However there will be no printout in the loop, as that statement is not within the loop body. The print statement is located in the code block <strong>after<\/strong> the loop and thus will be executed once.<\/p>\n<pre class=\"toolbar:2 show-lang:2 lang:c mark:4 decode:true\">int i = 0;\r\n\r\n\/\/ Mistake here will cause the loop to run \"invisible\" to the programmer\r\nfor (i = 0; i &lt; 5; i++);\r\n{\r\n    \/\/ This IS NOT the loop body due to the semicolon!\r\n    printf(\"i = %d\\n\", i);\r\n}<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"No_body_as_deliberate_code\"><\/span>No body as deliberate code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In some cases, programmers try to shorten their LOC (lines of code) by writing loops on a single line. This can be done for both <span class=\"lang:c highlight:0 decode:true crayon-inline \">while<\/span>\u00a0 and <span class=\"lang:c highlight:0 decode:true crayon-inline \">for<\/span>\u00a0 loops. This is typically considered<strong> <span style=\"color: #ff0000;\">in bad taste<\/span><\/strong>. The issue is that this kind of code is thought to be <strong><span style=\"color: #ff0000;\">deliberately hiding intent <span style=\"color: #000000;\">(i.e. malicious code such as viruses)<\/span><\/span>.<\/strong> This will also cause issues with code review and debugging!<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">char str[] = \"What a wonderful day!\";\r\n\r\nint i = 0;\r\n\r\n\/\/ Loop to the character past the space\r\nwhile (str[i++] != ' ');\r\n\r\nprintf(\"The string from the second word: %s\\n\", &amp;str[i]);\r\n<\/pre>\n<p>While you should be aware of this, <strong>avoid<\/strong> writing code like this. Write the loop out in full, so that it&#8217;s easy to debug and maintain.<\/p>\n<p>Note that this code <strong>will also crash the program<\/strong> if the string doesn&#8217;t contain a space &#8211; so we also created a hidden bug!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basics Loops are one of the most basic fundamentals in programming, that allow us to repeat a certain block of code or simply put, some statements in the code. This repeatable block of code is called the loop body. In C, we commonly surround the loop body in curly braces { } . Loops are &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/coding-guides\/loops\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Loops<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4802,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-8683","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=8683"}],"version-history":[{"count":24,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8683\/revisions"}],"predecessor-version":[{"id":10424,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8683\/revisions\/10424"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4802"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=8683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}