{"id":653,"date":"2025-07-21T12:11:52","date_gmt":"2025-07-21T12:11:52","guid":{"rendered":"https:\/\/tipcontrol.com\/?page_id=653"},"modified":"2025-07-21T12:11:52","modified_gmt":"2025-07-21T12:11:52","slug":"foreach-statement","status":"publish","type":"page","link":"https:\/\/tipcontrol.com\/?page_id=653","title":{"rendered":"foreach statement"},"content":{"rendered":"\n<p><code>foreach(a, b)<\/code>&nbsp;Loop Documentation<\/p>\n\n\n\n<p>The <code>foreach(a, b)<\/code> loop is a control structure used to iterate over all elements in a collection. It simplifies the iteration process by automatically handling the retrieval of elements. This construct is particularly useful for lists, arrays, and maps, where it provides direct access to elements or key-value pairs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Structure of the&nbsp;<code>foreach<\/code>&nbsp;Loop<\/h4>\n\n\n\n<p><strong>Components<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>a (Iterator)<\/strong>: The variable representing the current element or key-value pair. In the case of maps,\u00a0<code>a<\/code>\u00a0is structured as\u00a0<code>a.key<\/code>\u00a0and\u00a0<code>a.value<\/code>.<\/li>\n\n\n\n<li><strong>b (Collection)<\/strong>: The collection or iterable structure over which the loop iterates. This can be an array, list, map, or any container supporting iteration.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Simple Example<\/h4>\n\n\n\n<p>int main() {<\/p>\n\n\n\n<p>&nbsp; &nbsp; \/\/ Example with the drivers<\/p>\n\n\n\n<p>&nbsp;foreach(x,numbers) lib.log(\u201cNumber is \u201c+x);<\/p>\n\n\n\n<p>&nbsp; &nbsp; foreach(d,drv) {<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; lib.log(\u201cDriver \u201c+d.key+\u201d found\u201d);<\/p>\n\n\n\n<p>lib.log(\u201cDriver-Values are \u201c+d.value);<\/p>\n\n\n\n<p>&nbsp; &nbsp; };<\/p>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>List Iteration<\/strong>: The first\u00a0<code>foreach<\/code>\u00a0iterates over a list of numbers, with\u00a0<code>x<\/code>\u00a0representing each element. The loop prints each number.<\/li>\n\n\n\n<li><strong>Map Iteration<\/strong>: The second\u00a0<code>foreach<\/code>\u00a0iterates over the driver map, with\u00a0<code>d\u00a0<\/code>providing structured access to\u00a0<code>item.key<\/code>\u00a0(driver name is const) and\u00a0<code>item.value<\/code>\u00a0(its data).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Use<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Versatility<\/strong>: Works seamlessly with various collections, ensuring that you can iterate over simple lists or complex key-value mappings with equal ease.<\/li>\n\n\n\n<li><strong>Code Clarity<\/strong>: Makes the intent of iteration clear, as it doesn&#8217;t require explicit iterator setup or termination logic.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Considerations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Structure Access<\/strong>: In maps ensure you use the correct structure (<code>a.key<\/code>,\u00a0<code>a.value<\/code>) to access the iterator&#8217;s parts.<\/li>\n<\/ul>\n\n\n\n<p>The <code>foreach(a, b)<\/code> loop is a powerful tool for iterating over collections in a concise and understandable manner, enhancing code clarity and developer productivity, especially when handling complex data structures like maps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>foreach(a, b)&nbsp;Loop Documentation The foreach(a, b) loop is a control structure used to iterate over all elements in a collection. It simplifies the iteration process by automatically handling the retrieval of elements. This construct is particularly useful for lists, arrays, and maps, where it provides direct access to elements or key-value pairs. Structure of the&nbsp;foreach&nbsp;Loop [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-653","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/tipcontrol.com\/index.php?rest_route=\/wp\/v2\/pages\/653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tipcontrol.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tipcontrol.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tipcontrol.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/tipcontrol.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=653"}],"version-history":[{"count":1,"href":"https:\/\/tipcontrol.com\/index.php?rest_route=\/wp\/v2\/pages\/653\/revisions"}],"predecessor-version":[{"id":654,"href":"https:\/\/tipcontrol.com\/index.php?rest_route=\/wp\/v2\/pages\/653\/revisions\/654"}],"wp:attachment":[{"href":"https:\/\/tipcontrol.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}